You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
vue-shop-admin-work/public2/ueditor/dialogs/emotion/emotion.js

212 lines
13 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 当页面加载完成后执行的函数,用于初始化页面中与表情相关的各种设置、元素创建以及事件绑定等操作
window.onload = function () {
// 设置编辑器editor可能是自定义的富文本编辑器对象的配置选项将 'emotionLocalization' 设置为 false表示表情相关资源的本地化属性为否具体影响后续表情图片等资源的获取路径
editor.setOpt({
emotionLocalization: false
});
// 根据编辑器的 'emotionLocalization' 选项值来确定表情图片的路径SmileyPath如果为 true则从本地相对路径 'images/' 获取,否则从指定的网络路径 "http://img.baidu.com/hi/" 获取
emotion.SmileyPath = editor.options.emotionLocalization === true? 'images/' : "http://img.baidu.com/hi/";
// 创建一个包含指定数量emotion.tabNum的空数组对象用于存储不同面板tab下的相关信息比如每个面板对应的表情图片文件名等每个属性名以 'tab' 加数字的形式表示(如 'tab0'、'tab1' 等)
emotion.SmileyBox = createTabList(emotion.tabNum);
// 创建一个包含指定数量emotion.tabNum的数组数组元素初始值都为 0用于标记每个面板tab是否已经创建或存在相关内容后续根据这个标记来决定是否需要重新创建对应面板的内容
emotion.tabExist = createArr(emotion.tabNum);
// 初始化表情图片名称相关的数据比如根据已有的图片前缀名等信息生成完整的图片文件名列表填充到对应的面板数组emotion.SmileyBox
initImgName();
// 初始化事件处理函数,为特定 id此处传入 'tabHeads')对应的元素及其子元素绑定点击等事件监听器,实现切换面板等交互功能
initEvtHandler("tabHeads");
};
// 初始化表情图片名称相关数据的函数用于根据已有的表情图片前缀名emotion.SmilmgName 中存储的信息和数量信息生成完整的表情图片文件名并填充到对应的面板数组emotion.SmileyBox
function initImgName() {
// 遍历 emotion.SmilmgName 对象的每个属性pro每个属性对应一个面板如 'tab0'、'tab1' 等)
for (var pro in emotion.SmilmgName) {
var tempName = emotion.SmilmgName[pro],
tempBox = emotion.SmileyBox[pro],
tempStr = "";
// 如果当前面板对应的数组tempBox已经有元素了即长度大于 0则直接返回不进行重复处理可能是避免多次初始化同一面板的数据
if (tempBox.length) return;
// 根据当前面板对应的图片前缀名tempName[0]和数量tempName[1]信息循环生成完整的图片文件名并添加到当前面板对应的数组tempBox
for (var i = 1; i <= tempName[1]; i++) {
tempStr = tempName[0];
if (i < 10) tempStr = tempStr + '0';
tempStr = tempStr + i + '.gif';
tempBox.push(tempStr);
}
}
}
// 初始化事件处理函数,用于为指定 idconId对应的元素及其子元素绑定点击等事件监听器实现切换面板等交互功能
function initEvtHandler(conId) {
// 通过 $G 函数(可能是自定义的获取 DOM 元素的函数)获取指定 idconId对应的元素此处应该是获取页面中用于切换面板的头部元素集合如包含各个面板标题的 span 元素的父元素)
var tabHeads = $G(conId);
// 遍历头部元素集合的每个子节点childNodes为符合条件的元素绑定点击事件监听器
for (var i = 0, j = 0; i < tabHeads.childNodes.length; i++) {
var tabObj = tabHeads.childNodes[i];
// 只对节点类型为元素节点nodeType == 1的子节点进行操作过滤掉文本节点、注释节点等非元素类型的节点
if (tabObj.nodeType == 1) {
// 使用 domUtils.on 函数可能是自定义的事件绑定工具函数为当前元素tabObj绑定点击事件监听器点击时执行一个立即执行函数返回的函数这个函数会调用 switchTab 函数并传入当前面板的索引j作为参数实现点击切换对应面板的功能同时 j 自增,用于记录下一个面板的索引
domUtils.on(tabObj, "click", (function (index) {
return function () {
switchTab(index);
};
})(j));
j++;
}
}
// 初始时默认切换到第一个面板(索引为 0调用 switchTab 函数显示第一个面板的内容
switchTab(0);
// 将用于显示表情预览的元素id 为 'tabIconReview')隐藏起来,初始状态下不显示预览内容
$G("tabIconReview").style.display = 'none';
}
// 插入表情图片到编辑器中的函数,根据传入的图片 URLurl以及触发的事件对象evt创建一个包含图片源地址src等信息的对象obj然后通过编辑器的命令execCommand插入图片并且如果不是按下 Ctrl 键触发的操作则隐藏相关的弹出对话框popup.hide
function InsertSmiley(url, evt) {
var obj = {
src: editor.options.emotionLocalization? editor.options.UEDITOR_HOME_URL + "dialogs/emotion/" + url : url
};
obj._src = obj.src;
editor.execCommand('insertimage', obj);
if (!evt.ctrlKey) {
dialog.popup.hide();
}
}
// 切换面板显示的函数根据传入的面板索引index执行调整面板高度autoHeight、创建面板内容如果不存在以及切换显示对应面板内容和隐藏其他面板内容等操作
function switchTab(index) {
// 根据传入的面板索引调整对应面板的高度相关样式,比如设置 iframe 和其父元素的高度值,确保合适的显示布局
autoHeight(index);
// 如果当前面板还不存在(对应 emotion.tabExist 数组中该索引位置的值为 0则标记该面板已存在设置为 1并调用 createTab 函数创建该面板的具体内容(如添加表情图片表格等元素)
if (emotion.tabExist[index] == 0) {
emotion.tabExist[index] = 1;
createTab('tab' + index);
}
// 获取用于切换面板的头部元素集合(包含各个面板标题的 span 元素)和主体元素集合(包含各个面板具体内容的 div 元素)
var tabHeads = $G("tabHeads").getElementsByTagName("span"),
tabBodys = $G("tabBodys").getElementsByTagName("div"),
i = 0, L = tabHeads.length;
// 循环遍历头部和主体元素集合将所有元素的样式设置为初始状态即隐藏主体元素display="none"清除头部元素的类名className=""),用于清除之前可能设置的选中类名等样式
for (; i < L; i++) {
tabHeads[i].className = "";
tabBodys[i].style.display = "none";
}
// 将当前要显示的面板对应的头部元素添加特定的类名(可能用于设置选中样式,如背景色等变化),并显示其对应的主体元素内容
tabHeads[index].className = "focus";
tabBodys[index].style.display = "block";
}
// 根据传入的面板索引index调整对应面板的高度相关样式的函数通过设置 iframe 及其父元素的高度值,为不同面板设置不同的合适高度,确保良好的显示布局效果
function autoHeight(index) {
var iframe = dialog.getDom("iframe"),
parent = iframe.parentNode.parentNode;
switch (index) {
case 0:
iframe.style.height = "380px";
parent.style.height = "392px";
break;
case 1:
iframe.style.height = "220px";
parent.style.height = "232px";
break;
case 2:
iframe.style.height = "260px";
parent.style.height = "272px";
break;
case 3:
iframe.style.height = "300px";
parent.style.height = "312px";
break;
case 4:
iframe.style.height = "140px";
parent.style.height = "152px";
break;
case 5:
iframe.style.height = "260px";
parent.style.height = "272px";
break;
case 6:
iframe.style.height = "230px";
parent.style.height = "242px";
break;
default:
}
}
// 创建特定面板tabName内容的函数主要用于生成包含表情图片、相关提示信息以及鼠标交互事件绑定的表格元素并添加到对应的面板 div 元素中,实现表情图片的展示和交互功能
function createTab(tabName) {
var faceVersion = "?v=1.1", //版本号
tab = $G(tabName), //获取将要生成的 Div 句柄,即对应面板的 div 元素对象,用于后续往里面添加生成的表格内容
imagePath = emotion.SmileyPath + emotion.imageFolders[tabName], //获取显示表情和预览表情的路径通过拼接表情图片的基础路径SmileyPath和当前面板对应的文件夹路径imageFolders[tabName])得到完整路径
positionLine = 11 / 2, //中间数,可能用于判断图片在表格中的位置(比如用于控制背景图片的定位等情况,结合后续代码看与鼠标悬停显示预览相关逻辑有关)
iWidth = iHeight = 35, //图片长宽,设置表情图片在页面上显示的宽度和高度均为 35 像素
iColWidth = 3, //表格剩余空间的显示比例,可能用于设置表格单元格的宽度占比等布局相关参数,影响表情图片在表格中的排列方式
tableCss = emotion.imageCss[tabName],
cssOffset = emotion.imageCssOffset[tabName],
textHTML = ['<table class="smileytable">'],
i = 0, imgNum = emotion.SmileyBox[tabName].length, imgColNum = 11, faceImage,
sUrl, realUrl, posflag, offset, infor;
// 循环遍历当前面板对应的表情图片数量imgNum生成表格的行tr和列td元素将表情图片、相关提示信息以及鼠标交互事件绑定添加到表格中
for (; i < imgNum;) {
textHTML.push('<tr>');
for (var j = 0; j < imgColNum; j++, i++) {
faceImage = emotion.SmileyBox[tabName][i];
if (faceImage) {
sUrl = imagePath + faceImage + faceVersion;
realUrl = imagePath + faceImage;
posflag = j < positionLine? 0 : 1;
offset = cssOffset * i * (-1) - 1;
infor = emotion.SmileyInfor[tabName][i];
textHTML.push('<td class="' + tableCss + '" border="1" width="' + iColWidth + '%" style="border-collapse:collapse;" align="center" bgcolor="transparent" onclick="InsertSmiley(\'' + realUrl.replace(/'/g, "\\'") + '\',event)" onmouseover="over(this,\'' + sUrl + '\',\'' + posflag + '\')" onmouseout="out(this)">');
textHTML.push('<span>');
textHTML.push('<img style="background-position:left ' + offset + 'px;" title="' + infor + '" src="' + emotion.SmileyPath + (editor.options.emotionLocalization? '0.gif" width="' : 'default/0.gif" width="') + iWidth + '" height="' + iHeight + '"></img>');
textHTML.push('</span>');
} else {
textHTML.push('<td width="' + iColWidth + '%" bgcolor="#FFFFFF">');
}
textHTML.push('</td>');
}
textHTML.push('</tr>');
}
textHTML.push('</table>');
textHTML = textHTML.join("");
tab.innerHTML = textHTML;
}
// 鼠标悬停在表情图片所在单元格td上时执行的函数用于改变单元格背景色、显示表情预览图片以及显示表情预览元素tabIconReview等操作实现鼠标悬停时的交互效果
function over(td, srcPath, posFlag) {
td.style.backgroundColor = "#ACCD3C";
$G('faceReview').style.backgroundImage = "url(" + srcPath + ")";
if (posFlag == 1) $G("tabIconReview").className = "show";
$G("tabIconReview").style.display = 'block';
}
// 鼠标移出表情图片所在单元格td时执行的函数用于恢复单元格背景色为透明、隐藏表情预览元素tabIconReview以及清除其相关类名等操作还原到初始状态
function out(td) {
td.style.backgroundColor = "transparent";
var tabIconRevew = $G("tabIconReview");
tabIconRevew.className = "";
tabIconRevew.style.display = 'none';
}
// 创建一个包含指定数量tabNum的空数组对象的函数每个属性名以 'tab' 加数字的形式表示(如 'tab0'、'tab1' 等),用于后续存储不同面板相关的信息,初始时每个数组都是空的
function createTabList(tabNum) {
var obj = {};
for (var i = 0; i < tabNum; i++) {
obj["tab" + i] = [];
}
return obj;
}
// 创建一个包含指定数量tabNum的数组数组元素初始值都为 0 的函数用于标记每个面板tab是否已经创建或存在相关内容后续根据这个标记来决定是否需要重新创建对应面板的内容
function createArr(tabNum) {
var arr = [];
for (var i = 0; i < tabNum; i++) {
arr[i] = 0;
}
return arr;
}