$(document).ready(function() {
space.ueditor.toolbar.init.myFace();// 初始化,自定义百度编辑器的《表情》工具按钮
space.ueditor.toolbar.init.commonLanguage();// 初始化,自定义百度编辑器的《常用语》工具按钮
});
space.ueditor = {
toolbar: {}
}
space.ueditor.toolbar = {
/**
* 模板
*/
template: {
// 气泡模板
airbubbles: '
',
// 微标
badge: '',
},
/**
* 关闭所有弹层
*/
closeAllBubbleLayer: function() {
$(".bubble-layer").fadeOut(100);
},
/**
* 注册按钮事件
* @param {Object} btnId
* @param {Object} callback
*/
regClickDocument: function(btnId, callback) {
// 全局注册点击事件
document.onclick = function(e) {
var len = $(e.target).parents("div[id='" + btnId + "']").length;
if(e.target.id != btnId && len == 0) {
space.ueditor.toolbar.closeAllBubbleLayer();
}
if(callback) {
callback(e)
}
}
},
/**
* 追加内容
* @param {Object} elId 元素 id
* @param {Object} html 内容
*/
appendHtml: function(elId, html) {
$("#" + elId).find('.bubble-layer-content').append(html);
},
editorAppendHtml: function(editor, html) {
editor.execCommand('inserthtml', html);
},
/**
* 重置气泡大小
*/
resetAirbubblesSize: function(editor, btn, id) {
// 编辑器的坐标
var ediOffSet = $(editor.iframe).parent().offset();
// 初始化坐标
var btnOffSet = $(btn.target).offset();
var airbubblesLeft = 0;// 气泡窗口,左边距离
var arrowsLeft = 30;// 三角箭头
var btnLeft = (btnOffSet.left - 26);
// 计算左边的距离
if (ediOffSet.left <= 0 && btnLeft < ediOffSet.left) {
airbubblesLeft = btnOffSet.left;
arrowsLeft = btnOffSet.left - 4;
}
else {
airbubblesLeft = btnLeft;
}
var airbubblesTop = (btnOffSet.top - $(id).height() - 16);
var airbubblesHeight = 112;
// 计算高度
if (airbubblesHeight > airbubblesTop) {
airbubblesHeight = airbubblesHeight - (airbubblesHeight - btnOffSet.top + 16)
}
if (airbubblesTop < 0) {
airbubblesTop = 0;
}
// 气泡窗口
$(id).css({
"top": airbubblesTop + "px",
"left": airbubblesLeft + "px",
"height": airbubblesHeight + "px",
});
// 三角箭头
$(id).find('.bubble-layer-arrows').css({
"left": arrowsLeft + "px"
})
},
init: {
/**
* 表情
*/
myFace: function() {
var id = space.utils.randomString(8);
$('body').append(space.ueditor.toolbar.template.airbubbles.replace("{airbubbles}", id));
$("#" + id).css({
'width': '300px',
'height': '112px'
});
for(var i = 1; i <= 32; i++) {
var html = '
';
space.ueditor.toolbar.appendHtml(id, html);
}
space.plugin.ueditor.registerUeditorToolBar.button({
title: '表情',
uiName: 'myFace',
icon: '/apps/space/images/ueditor/MyFace.png',
click: function(editor, btn, param) {
space.ueditor.toolbar.closeAllBubbleLayer();
var id = "#" + param.id;
// 如果已经显示了,再次点击就隐藏掉
if(!$(id).is(':hidden')) {
return;
}
space.ueditor.toolbar.resetAirbubblesSize(editor, btn, id);
$(id).fadeIn(200);
space.ueditor.toolbar.regClickDocument(btn.id, function(e) {
if(e.target.nodeName == "IMG" && $(e.target).parents("div[id='" + param.id + "']").length == 1) {
space.ueditor.toolbar.editorAppendHtml(editor, "
");
}
});
},
param: {
id: id
}
})
},
/**
* 常用语
*/
commonLanguage: function() {
var id = space.utils.randomString(8);
$('body').append(space.ueditor.toolbar.template.airbubbles.replace("{airbubbles}", id));
$("#" + id).css({
'width': '300px',
'height': '112px'
});
$.ajax({
type: "get",
url: "/apps?ark=personCustomPhrase_list&app=personnel",
async: false,
dataType: 'json',
success: function(data) {
if(data.code == 'success') {
var list = data.personCustomPhraseList;
if(!list || list.length == 0) {
var noDataHtml = ' 未设置常用语
';
space.ueditor.toolbar.appendHtml(id, noDataHtml);
return;
}
var html = '';
for (var i = 0;i < list.length;i++) {
html += '- ' + list[i].content + '
';
}
html += '
';
space.ueditor.toolbar.appendHtml(id, html);
}
}
});
space.plugin.ueditor.registerUeditorToolBar.button({
title: '常用语',
uiName: 'commonLanguage',
icon: '/apps/space/images/ueditor/CommonLanuage.png',
click: function(editor, btn, param) {
space.ueditor.toolbar.closeAllBubbleLayer();
var id = "#" + param.id;
// 如果已经显示了,再次点击就隐藏掉
if(!$("#" + param.id).is(':hidden')) {
return;
}
space.ueditor.toolbar.resetAirbubblesSize(editor, btn, id);
$(id).fadeIn(200);
space.ueditor.toolbar.regClickDocument(btn.id, function(e) {
if(e.target.nodeName == "LI" && $(e.target).parents("div[id='" + param.id + "']").length == 1 &&
$(e.target).parent("ul.ul-list-mobile").length == 1) {
// 插入内容
space.ueditor.toolbar.editorAppendHtml(editor, e.target.innerText);
}
});
},
param: {
id: id
}
});
}
}
}