PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/war/src/main/webapp/js/lib/KindEditor/plugins/emoticons/emoticons.js

http://github.com/b3log/b3log-solo
JavaScript | 129 lines | 120 code | 1 blank | 8 comment | 12 complexity | 0d7cd0a0c4c209c66a479723a594f845 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1/*******************************************************************************
  2* KindEditor - WYSIWYG HTML Editor for Internet
  3* Copyright (C) 2006-2011 kindsoft.net
  4*
  5* @author Roddy <luolonghao@gmail.com>
  6* @site http://www.kindsoft.net/
  7* @licence http://www.kindsoft.net/license.php
  8*******************************************************************************/
  9
 10KindEditor.plugin('emoticons', function(K) {
 11	var self = this, name = 'emoticons',
 12		path = (self.emoticonsPath || self.pluginsPath + 'emoticons/images/'),
 13		allowPreview = self.allowPreviewEmoticons === undefined ? true : self.allowPreviewEmoticons,
 14		currentPageNum = 1;
 15	self.clickToolbar(name, function() {
 16		var rows = 5, cols = 9, total = 135, startNum = 0,
 17			cells = rows * cols, pages = Math.ceil(total / cells),
 18			colsHalf = Math.floor(cols / 2),
 19			wrapperDiv = K('<div class="ke-plugin-emoticons"></div>'),
 20			elements = [],
 21			menu = self.createMenu({
 22				name : name,
 23				beforeRemove : function() {
 24					removeEvent();
 25				}
 26			});
 27		menu.div.append(wrapperDiv);
 28		var previewDiv, previewImg;
 29		if (allowPreview) {
 30			previewDiv = K('<div class="ke-preview"></div>').css('right', 0);
 31			previewImg = K('<img class="ke-preview-img" src="' + path + startNum + '.gif" />');
 32			wrapperDiv.append(previewDiv);
 33			previewDiv.append(previewImg);
 34		}
 35		function bindCellEvent(cell, j, num) {
 36			if (previewDiv) {
 37				cell.mouseover(function() {
 38					if (j > colsHalf) {
 39						previewDiv.css('left', 0);
 40						previewDiv.css('right', '');
 41					} else {
 42						previewDiv.css('left', '');
 43						previewDiv.css('right', 0);
 44					}
 45					previewImg.attr('src', path + num + '.gif');
 46					K(this).addClass('ke-on');
 47				});
 48			} else {
 49				cell.mouseover(function() {
 50					K(this).addClass('ke-on');
 51				});
 52			}
 53			cell.mouseout(function() {
 54				K(this).removeClass('ke-on');
 55			});
 56			cell.click(function(e) {
 57				self.insertHtml('<img src="' + path + num + '.gif" border="0" alt="" />').hideMenu().focus();
 58				e.stop();
 59			});
 60		}
 61		function createEmoticonsTable(pageNum, parentDiv) {
 62			var table = document.createElement('table');
 63			parentDiv.append(table);
 64			if (previewDiv) {
 65				K(table).mouseover(function() {
 66					previewDiv.show();
 67				});
 68				K(table).mouseout(function() {
 69					previewDiv.hide();
 70				});
 71				elements.push(K(table));
 72			}
 73			table.className = 'ke-table';
 74			table.cellPadding = 0;
 75			table.cellSpacing = 0;
 76			table.border = 0;
 77			var num = (pageNum - 1) * cells + startNum;
 78			for (var i = 0; i < rows; i++) {
 79				var row = table.insertRow(i);
 80				for (var j = 0; j < cols; j++) {
 81					var cell = K(row.insertCell(j));
 82					cell.addClass('ke-cell');
 83					bindCellEvent(cell, j, num);
 84					var span = K('<span class="ke-img"></span>')
 85						.css('background-position', '-' + (24 * num) + 'px 0px')
 86						.css('background-image', 'url(' + path + 'static.gif)');
 87					cell.append(span);
 88					elements.push(cell);
 89					num++;
 90				}
 91			}
 92			return table;
 93		}
 94		var table = createEmoticonsTable(currentPageNum, wrapperDiv);
 95		function removeEvent() {
 96			K.each(elements, function() {
 97				this.unbind();
 98			});
 99		}
100		var pageDiv;
101		function bindPageEvent(el, pageNum) {
102			el.click(function(e) {
103				removeEvent();
104				table.parentNode.removeChild(table);
105				pageDiv.remove();
106				table = createEmoticonsTable(pageNum, wrapperDiv);
107				createPageTable(pageNum);
108				currentPageNum = pageNum;
109				e.stop();
110			});
111		}
112		function createPageTable(currentPageNum) {
113			pageDiv = K('<div class="ke-page"></div>');
114			wrapperDiv.append(pageDiv);
115			for (var pageNum = 1; pageNum <= pages; pageNum++) {
116				if (currentPageNum !== pageNum) {
117					var a = K('<a href="javascript:;">[' + pageNum + ']</a>');
118					bindPageEvent(a, pageNum);
119					pageDiv.append(a);
120					elements.push(a);
121				} else {
122					pageDiv.append(K('@[' + pageNum + ']'));
123				}
124				pageDiv.append(K('@&nbsp;'));
125			}
126		}
127		createPageTable(currentPageNum);
128	});
129});