-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.smiledesignersmile.js
167 lines (138 loc) · 4.1 KB
/
jquery.smiledesignersmile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* smileDesignerSmile Plugin V0.7.2
*
* Copyright (c) 2009 Sebastian Deutsch <[email protected]>
* Licensed under the MIT (MIT-LICENSE.txt) license.
*
*/
jQuery.fn.smileDesignerSmile = function(imgUrl, options) {
// detect the really filthy browser
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
// get defaults
var defaults = {
backgroundPosition : '0px 0px',
opacity : '0.5',
logging : true,
displayOnLoad : false
};
var opts = jQuery.extend(defaults, options);
// check for firebug
if (!window.console || !window.console.firebugVersion)
{
var names = ["log", "debug", "info", "warn", "error",
"assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd",
"count", "trace", "profile", "profileEnd"];
window.console = {};
for (i in names)
{
window.console[names[i]] = function() {};
}
}
// get the object
var dummy = jQuery(this);
// if we are on the document let's create a helper div
if( dummy.get(0) == document ) {
jQuery('<div id="smd_helper_div__"></div').appendTo('body');
dummy = jQuery('#smd_helper_div__');
}
// set some styles
dummy.css('position','absolute');
dummy.css('top','0px');
dummy.css('left','0px');
dummy.css('backgroundColor','transparent');
dummy.css('backgroundImage', 'url(' + imgUrl + ')');
dummy.css('backgroundPosition', opts.backgroundPosition );
dummy.css('backgroundRepeat','no-repeat');
// set width and height and opacity
if(IE6) {
dummy.css('width', screen.width);
dummy.css('height', screen.height);
dummy.css('filter', 'Alpha(opacity=' + Math.floor(opts.opacity*100) + ')');
} else {
dummy.css('width','100%');
dummy.css('height','250%');
dummy.css('opacity', opts.opacity);
}
// do we want to show
if(!opts.displayOnLoad) {
dummy.css('display','none');
}
// make it unselectable
dummy.css('MozUserSelect','none'); // FF
dummy.css('KhtmlUserSelect','none'); // SF + CHROME
dummy.attr('unselectable', 'on') // IE + OPERA
dummy.data('is_hidden', true);
jQuery(document).dblclick(function() {
if(dummy.data('is_hidden')) {
dummy.show();
dummy.data('is_hidden', !dummy.data('is_hidden'));
} else {
dummy.hide();
dummy.data('is_hidden', !dummy.data('is_hidden'));
}
});
$(document).keyup(function(event){
if(!dummy.data('is_hidden')) {
var dx = 0;
var dy = 0;
if(event.keyCode == 37) { // cursor left
dx = -1; dy = 0;
}
if(event.keyCode == 38) { // cursor top
dx = 0; dy = -1;
}
if(event.keyCode == 39) { // cursor right
dx = 1; dy = 0;
}
if(event.keyCode == 40) { // cursor down
dx = 0; dy = 1;
}
var oldpos = dummy.css('backgroundPosition');
var cx = oldpos.split(' ')[0];
var cy = oldpos.split(' ')[1];
var ox = parseInt(cx.substring(0, cx.length-2));
var oy = parseInt(cy.substring(0, cy.length-2));
var x = ox+dx;
var y = oy+dy;
var newpos = x + 'px ' + y + 'px';
dummy.css('backgroundPosition', newpos);
if(opts.logging) {
console.log(newpos);
}
}
});
dummy.mouseup(function(e) {
if(!dummy.data('is_hidden')) {
dummy.data('pressed', false);
dummy.data('endX', e.clientX);
dummy.data('endY', e.clientY);
}
});
dummy.mousedown(function(e) {
if(!dummy.data('is_hidden')) {
dummy.data('pressed', true);
dummy.data('startX', e.clientX);
dummy.data('startY', e.clientY);
}
});
dummy.mousemove(function(e) {
if(!dummy.data('is_hidden')) {
if(dummy.data('pressed')) {
var oldpos = dummy.css('backgroundPosition');
var cx = oldpos.split(' ')[0];
var cy = oldpos.split(' ')[1];
var ox = parseInt(cx.substring(0, cx.length-2));
var oy = parseInt(cy.substring(0, cy.length-2));
var x = ox- (dummy.data('startX')-e.clientX);
var y = oy- (dummy.data('startY')-e.clientY);
dummy.data('startX', e.clientX);
dummy.data('startY', e.clientY);
var newpos = x + 'px ' + y + 'px';
dummy.css('backgroundPosition', newpos);
if(opts.logging) {
console.log(newpos);
}
}
}
});
}