插入表情
时间:2010-09-09 来源:流_浪
1 function $E(oID) {
2 var node = typeof oID == "string" ? document.getElementById(oID) : oID;
3 if (node != null) {
4 return node
5 } else {}
6 return null
7 }
8 var Core={};
9 Core.Events={};
10 Core.Events.addEvent = function(elm, func, evType, useCapture) {
11 var _el = $E(elm);
12 if (_el == null) {
13 trace("addEvent 找不到对象:" + elm);
14 return
15 }
16 if (typeof useCapture == "undefined") {
17 useCapture = false
18 }
19 if (typeof evType == "undefined") {
20 evType = "click"
21 }
22 if (_el.addEventListener) {
23 _el.addEventListener(evType, func, useCapture);
24 return true
25 } else {
26 if (_el.attachEvent) {
27 var r = _el.attachEvent("on" + evType, func);
28 return true
29 } else {
30 _el["on" + evType] = func
31 }
32 }
33 };
34 Core.Events.removeEvent = function(oElement, fHandler, sName) {
35 var _el = $E(oElement);
36 if (_el == null) {
37 trace("removeEvent 找不到对象:" + oElement);
38 return
39 }
40 if (typeof fHandler != "function") {
41 return
42 }
43 if (typeof sName == "undefined") {
44 sName = "click"
45 }
46 if (_el.addEventListener) {
47 _el.removeEventListener(sName, fHandler, false)
48 } else {
49 if (_el.attachEvent) {
50 _el.detachEvent("on" + sName, fHandler)
51 }
52 }
53 fHandler[sName] = null
54 };
55 Core.Base={};
56 (function() {
57 var Detect = function() {
58 var ua = navigator.userAgent.toLowerCase();
59 this.$IE = /msie/.test(ua);
60 this.$OPERA = /opera/.test(ua);
61 this.$MOZ = /gecko/.test(ua);
62 this.$IE5 = /msie 5 /.test(ua);
63 this.$IE55 = /msie 5.5/.test(ua);
64 this.$IE6 = /msie 6/.test(ua);
65 this.$IE7 = /msie 7/.test(ua);
66 this.$SAFARI = /safari/.test(ua);
67 this.$winXP = /windows nt 5.1/.test(ua);
68 this.$winVista = /windows nt 6.0/.test(ua);
69 this.$FF2 = /Firefox\/2/i.test(ua)
70 };
71 Core.Base.detect = new Detect()
72 })();
73 Core.Events.getEvent = function() {
74 return window.event
75 };
76 if (!Core.Base.detect.$IE) {
77 Core.Events.getEvent = function() {
78 if (window.event) {
79 return window.event
80 }
81 var o = arguments.callee.caller;
82 var e;
83 var n = 0;
84 while (o != null && n < 40) {
85 e = o.arguments[0];
86 if (e && (e.constructor == Event || e.constructor == MouseEvent)) {
87 return e
88 }
89 n++;
90 o = o.caller
91 }
92 return e
93 }
94 }
95 Core.Events.stopEvent = function(el) {
96 var ev = el ? el: Core.Events.getEvent();
97 ev.cancelBubble = true;
98 ev.returnValue = false
99 };
100 if (!Core.Base.detect.$IE) {
101 Core.Events.stopEvent = function(el) {
102 var ev = el ? el: Core.Events.getEvent();
103 ev.preventDefault();
104 ev.stopPropagation()
105 }
106 }
107 Core.Events.fixEvent = function(e) {
108 if (typeof e == "undefined") {
109 e = window.event
110 }
111 if (!e.target) {
112 e.target = e.srcElement;
113 e.pageX = e.x;
114 e.pageY = e.y
115 }
116 if (typeof e.layerX == "undefined") {
117 e.layerX = e.offsetX
118 }
119 if (typeof e.layerY == "undefined") {
120 e.layerY = e.offsetY
121 }
122 return e
123 };
124 Core.Dom={};
125 Core.Dom.getXY = function(el) {
126 if ((el.parentNode == null || el.offsetParent == null || Core.Dom.getStyle(el, "display") == "none") && el != document.body) {
127 return false
128 }
129 var parentNode = null;
130 var pos = [];
131 var box;
132 var doc = el.ownerDocument;
133 box = el.getBoundingClientRect();
134 var scrollPos = Core.System.getScrollPos(el.ownerDocument);
135 return [box.left + scrollPos[1], box.top + scrollPos[0]];
136 parentNode = el.parentNode;
137 while (parentNode.tagName && !/^body|html$/i.test(parentNode.tagName)) {
138 if (Core.Dom.getStyle(parentNode, "display").search(/^inline|table-row.*$/i)) {
139 pos[0] -= parentNode.scrollLeft;
140 pos[1] -= parentNode.scrollTop
141 }
142 parentNode = parentNode.parentNode
143 }
144 return pos
145 };
146 Core.Dom.getStyle = function(el, property) {
147 switch (property) {
148 case "opacity":
149 var val = 100;
150 try {
151 val = el.filters["DXImageTransform.Microsoft.Alpha"].opacity
152 } catch(e) {
153 try {
154 val = el.filters("alpha").opacity
155 } catch(e) {}
156 }
157 return val / 100;
158 case "float":
159 property = "styleFloat";
160 default:
161 var value = el.currentStyle ? el.currentStyle[property] : null;
162 return (el.style[property] || value)
163 }
164 };
165 Core.Dom.contains = function(oParentNode, oNode) {
166 return oParentNode.contains(oNode)
167 };
168 if (!Core.Base.detect.$IE) {
169 Core.Dom.contains = function(oParentNode, oNode) {
170 do {
171 if (oParentNode == oNode) {
172 return true
173 }
174 } while ( oNode = oNode . parentNode );
175 return false
176 }
177 }
178 Core.System={};
179 Core.System.getScrollPos = function(oDocument) {
180 oDocument = oDocument || document;
181 var dd = oDocument.documentElement;
182 var db = oDocument.body;
183 return [Math.max(dd.scrollTop, db.scrollTop), Math.max(dd.scrollLeft, db.scrollLeft), Math.max(dd.scrollWidth, db.scrollWidth), Math.max(dd.scrollHeight, db.scrollHeight)]
184 };
185
186
187
188 var App={};
189 (function(proxy) {
190 var iniTarget, srcInput, iconLayer, iconText;
191 proxy.showFaces = function(target, input, dX, dY,layerWidth) {
192 if (!iconLayer) {
193 iconLayer = makeFaces(layerWidth)
194 }
195 var position = Core.Dom.getXY(target);
196 position[1] += target.offsetHeight;
197 if (Core.Base.detect.$IE) {
198 position[0] += -10
199 }
200 if (!isNaN(dX)) {
201 position[0] += dX
202 }
203 if (!isNaN(dY)) {
204 position[1] += +dY
205 }
206 iconLayer.style.cssText = "left:" + position[0] + "px;top:" + position[1] + "px;" + "position:absolute;z-index:1700;display:'';zoom:1;";
207 iniTarget = target;
208 srcInput = input;
209 iconText = null;
210 Core.Events.addEvent(document.body ,hideFace, "click", false);
211 return iconLayer
212 };
213 proxy.hideFaces = function() {
214 iconLayer && (iconLayer.style.display = "none");
215 Core.Events.removeEvent(document.body, hideFace, "click");
216
217 return iconLayer
218 };
219 proxy.getFace = function() {
220 return iconText
221 };
222 function hideFace(e) {
223 var t = e.target || e.srcElement;
224 if (t != iniTarget && !Core.Dom.contains(iconLayer, t)) {
225 proxy.hideFaces()
226 }
227 }
228 function makeFaces(layerWidth) {
229 layerWidth = layerWidth || "360px";
230 var list = [];
231 var faces=[
232 {icon:'足球',value:'[足球]',src:'football.gif'},
233 {icon:'哨子',value:'[哨子]',src:'shao.gif'},
234 {icon:'红牌',value:'[红牌]',src:'redcard.gif'},
235 {icon:'黄牌',value:'[黄牌]',src:'yellowcard.gif'},
236 {icon:'哈哈',value:'[哈哈]',src:'laugh.gif'},
237 {icon:'呵呵',value:'[呵呵]',src:'smile.gif'},
238 {icon:'泪', value:'[泪]', src:'cry.gif'},
239 {icon:'汗', value:'[汗]', src:'sweat.gif'},
240 {icon:'爱你',value:'[爱你]',src:'love.gif'},
241 {icon:'嘻嘻',value:'[嘻嘻]',src:'tooth.gif'},
242 {icon:'哼', value:'[哼]', src:'hate.gif'},
243 {icon:'心', value:'[心]', src:'heart.gif'},
244 {icon:'晕', value:'[晕]', src:'dizzy.gif'},
245 {icon:'怒', value:'[怒]', src:'angry.gif'},
246 {icon:'抓狂',value:'[抓狂]',src:'crazy.gif'},
247 {icon:'困', value:'[困]', src:'sleepy.gif'},
248 {icon:'太阳',value:'[太阳]',src:'sun.gif'},
249 {icon:'下雨',value:'[下雨]',src:'rain.gif'},
250 {icon:'伤心',value:'[伤心]',src:'sad.gif'},
251 {icon:'月亮',value:'[月亮]',src:'moon.gif'},
252 {icon:'猪头',value:'[猪头]',src:'pig.gif'}
253
254 ]
255 var imgURI = "images/face/";
256 if (Core.Base.detect.$IE) {
257 setTimeout(function() {
258 var oContainer = document.getElementById("face_icons");
259 for (var i = 0; i < faces.length; i++) {
260 (function(i) {
261 var oImg = new Image();
262 oImg.onload = function() {
263 var oLi = document.createElement("LI");
264 oLi.innerHTML = '<a href="javascript:void(0)" onclick="App._onFaceIconClicked_(this)" ' + 'value="' + faces[i].value + '" title="' + faces[i].icon + '">' + '<img src="' + imgURI + faces[i].src + '" alt="' + faces[i].icon + '" /></a>';
265 oContainer.appendChild(oLi);
266 oImg.onload = null;
267 oImg = null
268 };
269 oImg.src = imgURI + faces[i].src
270 })(i)
271 }
272 },
273 100)
274 } else {
275
276 for (var i = 0; i < faces.length; i++) {
277 list.push('<li><a href="javascript:void(0)" onclick="App._onFaceIconClicked_(this)" ' + 'value="' + faces[i].value + '" title="' + faces[i].icon + '">' + '<img src="' + imgURI + faces[i].src + '" alt="' + faces[i].icon + '" /></a></li>')
278 }
279 }
280 var icons = '<table class="mBlogLayer"><tbody><tr><td class="top_l"></td><td class="top_c"></td><td class="top_r"></td></tr><tr><td class="mid_l"></td><td class="mid_c"><div class="layerBox"><div class="layerBoxCon1" style="width:' + layerWidth + '"><div class="faceItem clearFix"><div class="layerArrow"></div><div class="layerMedia_close"><a class="close" href="javascript:void(App.hideFaces())" title="关闭"></a></div><div class="faceItemPicbg"><ul id="face_icons">' + list.join("") + '</ul></div></div></div></div></td><td class="mid_r"></td></tr><tr><td class="bottom_l"></td><td class="bottom_c"></td><td class="bottom_r"></td></tr></tbody></table>';
281 var box = document.createElement("div");
282 box.innerHTML = icons;
283 box.style.display = "none";
284 document.body.appendChild(box);
285 return box
286 }
287 App._onFaceIconClicked_ = function(el) {
288 iconText = el.getAttribute("value");
289 if (iconText) {
290 iconLayer.style.display = "none";
291 if (srcInput) {
292 srcInput.focus();
293 setTimeout(function() {
294 insertText(srcInput, iconText)
295 },
296 10)
297 }
298 }
299 };
300 function insertText(oInput, text) {
301 if (Core.Base.detect.$IE) {
302 var range = oInput.createTextRange();
303 range.collapse(true);
304 if (oInput.caretPos) {
305 var caretPos = oInput.caretPos;
306 caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == " " ? text + " ": text;
307 range.moveStart("character", oInput.value.indexOf(text) + 1);
308 range.moveEnd("character", text.length - 2)
309 } else {
310 var sel = document.selection.createRange();
311 document.selection.empty();
312 sel.text = text
313 }
314 } else {
315 if (oInput.setSelectionRange) {
316 var start = oInput.selectionStart;
317 var end = oInput.selectionEnd;
318 var str1 = oInput.value.substring(0, start);
319 var str2 = oInput.value.substring(end);
320 var v = str1 + text,
321 len = v.length;
322 oInput.value = v + str2;
323 oInput.setSelectionRange(len, len)
324 } else {
325 oInput.value += text
326 }
327 }
328 }
329 })(App);
相关阅读 更多 +