forked from alswl/jEmotion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQuery.jEmotion.js
215 lines (195 loc) · 5.5 KB
/
jQuery.jEmotion.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* jQuery jEmotion
*
* @version: 0.3
* @update: 2011.11.04
* @author: alswl
* @website: http://log4d.com/2011/11/jemotion
* @demo: http://lab.log4d.com/javascript/jEmotion/demo.html
* @source: https://github.com/alswl/jEmotion
*
* Dual licensed under the and GPL licenses:
* http://www.gnu.org/licenses/gpl.html
*
*/
/**
* 定义文本图片对应关系
*/
var smilesMaps = {
"呵呵":"smile.gif",
"嘻嘻":"tooth.gif",
"哈哈":"/laugh.gif",
"爱你":"love.gif",
"晕":"dizzy.gif",
"泪":"sad.gif",
"馋嘴":"cz_thumb.gif",
"抓狂":"crazy.gif",
"哼":"hate.gif",
"抱抱":"bb_thumb.gif",
"可爱":"tz_thumb.gif",
"怒":"angry.gif",
"汗":"sweat.gif",
"困":"sleepy.gif",
"害羞":"shame_thumb.gif",
"睡觉":"sleep_thumb.gif",
"钱":"money_thumb.gif",
"偷笑":"hei_thumb.gif",
"酷":"cool_thumb.gif",
"衰":"cry.gif",
"吃惊":"cj_thumb.gif",
"闭嘴":"bz_thumb.gif",
"鄙视":"bs2_thumb.gif",
"挖鼻屎":"kbs_thumb.gif",
"花心":"hs_thumb.gif",
"鼓掌":"gz_thumb.gif",
"失望":"sw_thumb.gif",
"思考":"sk_thumb.gif",
"生病":"sb_thumb.gif",
"亲亲":"qq_thumb.gif",
"怒骂":"nm_thumb.gif",
"太开心":"mb_thumb.gif",
"懒得理你":"ldln_thumb.gif",
"右哼哼":"yhh_thumb.gif",
"左哼哼":"zhh_thumb.gif",
"嘘":"x_thumb.gif",
"委屈":"wq_thumb.gif",
"吐":"t_thumb.gif",
"可怜":"kl_thumb.gif",
"打哈气":"k_thumb.gif",
"顶":"d_thumb.gif",
"疑问":"yw_thumb.gif",
"做鬼脸":"zgl_thumb.gif",
"握手":"ws_thumb.gif",
"耶":"ye_thumb.gif",
"强":"good_thumb.gif",
"弱":"sad_thumb.gif",
"不要":"no_thumb.gif",
"好的":"ok_thumb.gif",
"赞":"z2_thumb.gif",
"来":"come_thumb.gif",
"蛋糕":"cake.gif",
"心":"heart.gif",
"伤心":"unheart.gif",
"钟":"clock_thumb.gif",
"猪头":"pig.gif",
"话筒":"m_thumb.gif",
"月亮":"moon.gif",
"太阳":"sun.gif",
"下雨":"rain.gif"};
var jEmotion = function (settings_) {
var jemo = new Object();
jemo.settings = settings_;
jemo._this = $(jemo.settings.input);
jemo.handler = $("#" + jemo.settings.handler);
/**
* 插件设定
settings: {
input: null, //input textarea
handler: null, //触发按钮
imagePath: 'images', //图片相对路径
converts: '' //启动转换的元素 TODO: 捕捉body会出现问题
},*/
/**
* 初始化对向
*/
jemo.init = function() {
if (jemo.settings.handler == null || jemo.settings.imagePath == null ||
jemo.settings.input == null) {
alert('jEmotion load error setting.');
return false;
}
jemo.bindHandlerClickEvent();
jemo.bindEmotionClickEvent();
$("html").click(function(){
$(".je_emotions").hide();
});
return jemo;
};
jemo.textId = function(){
return 'emo_'+jemo.settings.input;
};
/**
* 插入html到页面
*/
jemo.insertHtml = function() {
var html = '<style>.je_emotions{width:364px; position:absolute;border:1px solid #aaa;border-top:none;z-index:9999; text-align:center;padding:3px;padding-bottom:6px;background:#fff;} .je_emotions a img{float:left;cursor:pointer;margin:1px 1px; border:#cacaca 1px solid;visibility:visible;} .je_emotions a:hover img{border:1px solid #f51d69} </style>';
html += '<div class="je_emotions" id="'+jemo.textId()+'" >';
for (i in smilesMaps) {
html += '<a href="javascript:;">' +
jemo.code2img(i) + '</a>';
}
html += '</div>'
$("body").append(html);
};
/*
* 设定表情框位置
*/
jemo.setPosition = function() {
var top = jemo.handler.offset().top;
var left = jemo.handler.offset().left;
$('#' + jemo.textId()).css("top", parseInt(top) + "px");
$('#' + jemo.textId()).css("left", parseInt(left) + "px");
};
/**
* 绑定触发点击事件
*/
jemo.bindHandlerClickEvent = function() {
jemo.handler.live('click', function(event) {
if ($('#' + jemo.textId()).size() > 0) { //如果存在就切换显示
$('#' + jemo.textId()).toggle();
} else {
jemo.insertHtml();
}
jemo.setPosition();
});
};
/**
* 绑定触发点击事件
*/
jemo.bindEmotionClickEvent = function() {
$("#" + jemo.textId() +" img").live('click', (function() {
jemo.insertToCursor('[' + $(this).attr('title') +']');
}));
};
/**
* 替换表情到文件名
*/
jemo.code2html = function(text) {
var match = /\[([\u4e00-\u9fa5]*)\]/g;
var e;
while(e = match.exec(text)){
text = text.replace(e[0], jemo.code2img(e[1]));
}
return text;
};
/**
* 转换 哈哈 -> <img/>
*/
jemo.code2img = function(code) {
return '<img src="' + jemo.settings.imagePath + smilesMaps[code] +
'" title="' + code +'" />';
};
/**
* 光标处插入 //TODO 光标返回原来位置
*/
jemo.insertToCursor = function(text) {
var input = $("#" + jemo.settings.input);
var length = input.val().length;
input.focus();
if(typeof document.selection !="undefined") {
document.selection.createRange().text = text;
document.selection.createRange().select();
} else {
input.val(input.val().substr(0, input[0].selectionStart) + text +
input.val().substring(input[0].selectionStart, length));
}
};
return jemo;
};
$.fn.jEmotion = function(settings_){
//jEmotion.settings.input = $(this);
//$.extend(jEmotion.settings, settings);
//jEmotion.init();
//return $(this);
return new jEmotion(settings_).init();
};