admin.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. // +----------------------------------------------------------------------
  2. // | ThinkAdmin
  3. // +----------------------------------------------------------------------
  4. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  5. // +----------------------------------------------------------------------
  6. // | 官方网站: https://thinkadmin.top
  7. // +----------------------------------------------------------------------
  8. // | 开源协议 ( https://mit-license.org )
  9. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. /*! 数组兼容处理 */
  15. if (typeof Array.prototype.some !== 'function') {
  16. Array.prototype.some = function (callable) {
  17. for (var i in this) if (callable(this[i], i, this) === true) {
  18. return true;
  19. }
  20. return false;
  21. };
  22. }
  23. if (typeof Array.prototype.every !== 'function') {
  24. Array.prototype.every = function (callable) {
  25. for (var i in this) if (callable(this[i], i, this) === false) {
  26. return false;
  27. }
  28. return true;
  29. };
  30. }
  31. if (typeof Array.prototype.forEach !== 'function') {
  32. Array.prototype.forEach = function (callable, context) {
  33. typeof context === "undefined" ? context = window : null;
  34. for (var i in this) callable.call(context, this[i], i, this)
  35. };
  36. }
  37. /*! 脚本应用根路径 */
  38. window.appRoot = (function (src) {
  39. return src.pop(), src.pop(), src.join('/') + '/';
  40. })(document.scripts[document.scripts.length - 1].src.split('/'));
  41. /*! 静态插件库路径 */
  42. window.baseRoot = (function (src) {
  43. return src.substring(0, src.lastIndexOf("/") + 1);
  44. })(document.scripts[document.scripts.length - 1].src);
  45. /*! 动态插件库路径 */
  46. window.tapiRoot = window.tapiRoot || window.appRoot + "admin";
  47. /*! 配置 layui 插件 */
  48. layui.config({base: baseRoot + 'plugs/layui_exts/'});
  49. /*! 挂载 layui & jquery 对象 */
  50. if (typeof jQuery === 'undefined') window.$ = window.jQuery = layui.$;
  51. window.form = layui.form, window.layer = layui.layer, window.laydate = layui.laydate;
  52. /*! 配置 require 参数 */
  53. require.config({
  54. waitSeconds: 60,
  55. baseUrl: baseRoot,
  56. map: {'*': {css: baseRoot + 'plugs/require/css.js'}},
  57. paths: {
  58. 'vue': ['plugs/vue/vue.min'],
  59. 'md5': ['plugs/jquery/md5.min'],
  60. 'json': ['plugs/jquery/json.min'],
  61. 'xlsx': ['plugs/jquery/xlsx.min'],
  62. 'excel': ['plugs/jquery/excel.xlsx'],
  63. 'base64': ['plugs/jquery/base64.min'],
  64. 'upload': [tapiRoot + '/api.upload/index?'],
  65. 'angular': ['plugs/angular/angular.min'],
  66. 'cropper': ['plugs/cropper/cropper.min'],
  67. 'echarts': ['plugs/echarts/echarts.min'],
  68. 'ckeditor': ['plugs/ckeditor/ckeditor'],
  69. 'websocket': ['plugs/socket/websocket'],
  70. 'pcasunzips': ['plugs/jquery/pcasunzips'],
  71. 'sortablejs': ['plugs/sortable/sortable.min'],
  72. 'vue.sortable': ['plugs/sortable/vue.draggable.min'],
  73. 'jquery.ztree': ['plugs/ztree/ztree.all.min'],
  74. 'jquery.masonry': ['plugs/jquery/masonry.min'],
  75. 'jquery.cropper': ['plugs/cropper/cropper.min'],
  76. 'jquery.autocompleter': ['plugs/jquery/autocompleter.min'],
  77. },
  78. shim: {
  79. 'excel': {deps: [baseRoot + 'plugs/layui_exts/excel.js']},
  80. 'websocket': {deps: [baseRoot + 'plugs/socket/swfobject.min.js']},
  81. 'cropper': {deps: ['css!' + baseRoot + 'plugs/cropper/cropper.min.css']},
  82. 'vue.sortable': {deps: ['vue']}, 'angular.sortable': {deps: ['angular']},
  83. 'jquery.ztree': {deps: ['jquery', 'css!' + baseRoot + 'plugs/ztree/zTreeStyle/zTreeStyle.css']},
  84. 'jquery.autocompleter': {deps: ['jquery', 'css!' + baseRoot + 'plugs/jquery/autocompleter.css']},
  85. }
  86. });
  87. /*! 注册 jquery 组件 */
  88. define('jquery', [], function () {
  89. return layui.$;
  90. });
  91. $(function () {
  92. window.$body = $('body');
  93. /*! 注册单次事件 */
  94. function onEvent(event, select, callable) {
  95. return $body.off(event, select).on(event, select, callable);
  96. }
  97. /*! 读取 data-rule 绑定 table 值 */
  98. function applyRuleValue(elem, data) {
  99. // 新 tableId 规则兼容处理
  100. if (elem.dataset.tableId && elem.dataset.rule) {
  101. var idx1, idx2, temp, regx, field, rule = {};
  102. var json = layui.table.checkStatus(elem.dataset.tableId).data;
  103. layui.each(elem.dataset.rule.split(';'), function (idx, item, attr) {
  104. (attr = item.split('#', 2)), rule[attr[0]] = attr[1];
  105. });
  106. for (idx1 in rule) {
  107. temp = [], regx = new RegExp(/^{(.*?)}$/);
  108. if (regx.test(rule[idx1]) && (field = rule[idx1].replace(regx, '$1'))) {
  109. for (idx2 in json) if (json[idx2][field]) temp.push(json[idx2][field]);
  110. if (temp.length < 1) return $.msg.tips('请选择需要更改的数据!'), false;
  111. data[idx1] = temp.join(',');
  112. } else {
  113. data[idx1] = rule[idx1];
  114. }
  115. }
  116. return data;
  117. } else {
  118. var value = elem.dataset.value || (function (rule, array) {
  119. $(elem.dataset.target || 'input[type=checkbox].list-check-box').map(function () {
  120. this.checked && array.push(this.value);
  121. });
  122. return array.length > 0 ? rule.replace('{key}', array.join(',')) : '';
  123. })(elem.dataset.rule || '', []) || '';
  124. if (value.length < 1) return $.msg.tips('请选择需要更改的数据!'), false;
  125. return value.split(';').forEach(function (item) {
  126. data[item.split('#')[0]] = item.split('#')[1];
  127. }), data;
  128. }
  129. }
  130. /*! 消息组件实例 */
  131. $.msg = new function () {
  132. var that = this;
  133. this.idx = [], this.shade = [0.02, '#000'];
  134. /*! 关闭消息框 */
  135. this.close = function (index) {
  136. if (index !== null) return layer.close(index);
  137. for (var i in this.idx) that.close(this.idx[i]);
  138. this.idx = [];
  139. };
  140. /*! 弹出警告框 */
  141. this.alert = function (msg, call) {
  142. var idx = layer.alert(msg, {end: call, scrollbar: false});
  143. return that.idx.push(idx), idx;
  144. };
  145. /*! 显示成功类型的消息 */
  146. this.success = function (msg, time, call) {
  147. var idx = layer.msg(msg, {icon: 1, shade: this.shade, scrollbar: false, end: call, time: (time || 2) * 1000, shadeClose: true});
  148. return that.idx.push(idx), idx;
  149. };
  150. /*! 显示失败类型的消息 */
  151. this.error = function (msg, time, call) {
  152. var idx = layer.msg(msg, {icon: 2, shade: this.shade, scrollbar: false, time: (time || 3) * 1000, end: call, shadeClose: true});
  153. return that.idx.push(idx), idx;
  154. };
  155. /*! 状态消息提示 */
  156. this.tips = function (msg, time, call) {
  157. var idx = layer.msg(msg, {time: (time || 3) * 1000, shade: this.shade, end: call, shadeClose: true});
  158. return that.idx.push(idx), idx;
  159. };
  160. /*! 显示加载提示 */
  161. this.loading = function (msg, call) {
  162. var idx = msg ? layer.msg(msg, {icon: 16, scrollbar: false, shade: this.shade, time: 0, end: call}) : layer.load(2, {time: 0, scrollbar: false, shade: this.shade, end: call});
  163. return that.idx.push(idx), idx;
  164. };
  165. /*! 页面加载层 */
  166. this.page = new function () {
  167. this.$body = $('body>.think-page-loader');
  168. this.$main = $('.think-page-body+.think-page-loader');
  169. this.stat = function () {
  170. return this.$body.is(':visible');
  171. }, this.show = function () {
  172. this.stat() || this.$main.removeClass('layui-hide').show();
  173. }, this.hide = function () {
  174. if (this.time) clearTimeout(this.time);
  175. this.time = setTimeout(function () {
  176. (that.page.time = 0) || that.page.$main.fadeOut();
  177. }, 200);
  178. };
  179. };
  180. /*! 确认对话框 */
  181. this.confirm = function (msg, ok, no) {
  182. return layer.confirm(msg, {title: '操作确认', btn: ['确认', '取消']}, function (idx) {
  183. (typeof ok === 'function' && ok.call(this, idx)), that.close(idx);
  184. }, function (idx) {
  185. (typeof no === 'function' && no.call(this, idx)), that.close(idx);
  186. });
  187. };
  188. /*! 自动处理JSON数据 */
  189. this.auto = function (ret, time) {
  190. var url = ret.url || (typeof ret.data === 'string' ? ret.data : '');
  191. var msg = ret.msg || (typeof ret.info === 'string' ? ret.info : '');
  192. if (parseInt(ret.code) === 1 && time === 'false') {
  193. return url ? (location.href = url) : $.form.reload();
  194. }
  195. return (parseInt(ret.code) === 1) ? this.success(msg, time, function () {
  196. (url ? (location.href = url) : $.form.reload()), that.close(null);
  197. }) : this.error(msg, 3, function () {
  198. url ? location.href = url : '';
  199. });
  200. };
  201. };
  202. /*! 表单自动化组件 */
  203. $.form = new function () {
  204. var that = this;
  205. /*! 内容区选择器 */
  206. this.selecter = '.layui-layout-admin>.layui-body>.think-page-body';
  207. /*! 刷新当前页面 */
  208. this.reload = function (force) {
  209. if (force) top.location.reload();
  210. else if (self !== top) location.reload();
  211. else window.onhashchange.call(this);
  212. };
  213. /*! 内容区域动态加载后初始化 */
  214. this.reInit = function ($dom) {
  215. $(window).trigger('scroll'), $.vali.listen(this), $dom = $dom || $(this.selecter);
  216. $dom.find('[required]').map(function ($parent) {
  217. if (($parent = $(this).parent()) && $parent.is('label')) {
  218. $parent.addClass('label-required-prev');
  219. } else {
  220. $parent.prevAll('label').addClass('label-required-next');
  221. }
  222. }), $dom.find('input[data-date-range]').map(function () {
  223. this.setAttribute('autocomplete', 'off'), laydate.render({
  224. type: this.dataset.dateRange || 'date', range: true, elem: this, done: function (value) {
  225. $(this.elem).val(value).trigger('change');
  226. }
  227. });
  228. }), $dom.find('input[data-date-input]').map(function () {
  229. this.setAttribute('autocomplete', 'off'), laydate.render({
  230. type: this.dataset.dateInput || 'date', range: false, elem: this, done: function (value) {
  231. $(this.elem).val(value).trigger('change');
  232. }
  233. });
  234. }), $dom.find('[data-lazy-src]:not([data-lazy-loaded])').each(function () {
  235. if (this.dataset.lazyLoaded !== 'true') {
  236. this.dataset.lazyLoaded = "true";
  237. if (this.nodeName === 'IMG') {
  238. this.src = this.dataset.lazySrc;
  239. } else {
  240. this.style.backgroundImage = 'url(' + this.dataset.lazySrc + ')';
  241. }
  242. }
  243. });
  244. };
  245. /*! 在内容区显示视图 */
  246. this.show = function (html) {
  247. $(this.selecter).html(html), setTimeout(function () {
  248. that.reInit($(that.selecter));
  249. }, 500);
  250. };
  251. /*! 异步加载的数据 */
  252. this.load = function (url, data, method, callable, loading, tips, time, headers) {
  253. // 如果主页面 loader 显示中,绝对不显示 loading 图标
  254. loading = $('.layui-page-loader').is(':visible') ? false : loading;
  255. var loadidx = loading !== false ? $.msg.loading(tips) : 0;
  256. if (typeof data === 'object' && typeof data['_token_'] === 'string') {
  257. headers = headers || {}, headers['User-Form-Token'] = data['_token_'], delete data['_token_'];
  258. }
  259. $.ajax({
  260. data: data || {}, type: method || 'GET', url: $.menu.parseUri(url), beforeSend: function (xhr, i) {
  261. if (typeof Pace === 'object' && loading !== false) Pace.restart();
  262. if (typeof headers === 'object') for (i in headers) xhr.setRequestHeader(i, headers[i]);
  263. }, error: function (XMLHttpRequest, $dialog, layIdx, iframe) {
  264. if (parseInt(XMLHttpRequest.status) !== 200 && XMLHttpRequest.responseText.indexOf('Call Stack') > -1) try {
  265. layIdx = layer.open({title: XMLHttpRequest.status + ' - ' + XMLHttpRequest.statusText, type: 2, move: false, content: 'javascript:;'});
  266. layer.full(layIdx), $dialog = $('#layui-layer' + layIdx), iframe = $dialog.find('iframe').get(0);
  267. (iframe.contentDocument || iframe.contentWindow.document).write(XMLHttpRequest.responseText);
  268. $dialog.find('.layui-layer-setwin').css({right: '35px', top: '28px'}).find('a').css({marginLeft: 0});
  269. $dialog.find('.layui-layer-title').css({color: 'red', height: '70px', lineHeight: '70px', fontSize: '22px', textAlign: 'center', fontWeight: 700});
  270. } catch (e) {
  271. layer.close(layIdx);
  272. }
  273. layer.closeAll('loading');
  274. if (parseInt(XMLHttpRequest.status) !== 200) {
  275. $.msg.tips('E' + XMLHttpRequest.status + ' - 服务器繁忙,请稍候再试!');
  276. } else {
  277. this.success(XMLHttpRequest.responseText);
  278. }
  279. }, success: function (ret) {
  280. if (typeof callable === 'function' && callable.call(that, ret) === false) return false;
  281. return typeof ret === 'object' ? $.msg.auto(ret, time || ret.wait || undefined) : that.show(ret);
  282. }, complete: function () {
  283. $.msg.close(loadidx);
  284. }
  285. });
  286. };
  287. /*! 以 HASH 打开新网页 */
  288. this.href = function (url, ele) {
  289. // 重置表格页数缓存
  290. if (ele && ele.dataset.menuNode) layui.sessionData('pages', null);
  291. if (url !== '#') location.hash = $.menu.parseUri(url, ele);
  292. else if (ele && ele.dataset.menuNode) $('[data-menu-node^="' + ele.dataset.menuNode + '-"]:first').trigger('click');
  293. };
  294. /*! 加载 HTML 到 BODY 位置 */
  295. this.open = function (url, data, call, load, tips) {
  296. this.load(url, data, 'get', function (ret) {
  297. return (typeof ret === 'object' ? $.msg.auto(ret) : that.show(ret)), false;
  298. }, load, tips);
  299. };
  300. /*! 打开 IFRAME 窗口 */
  301. this.iframe = function (url, name, area, offset, destroy) {
  302. return layer.open({title: name || '窗口', type: 2, area: area || ['800px', '580px'], end: destroy || null, offset: offset, fixed: true, maxmin: false, content: url});
  303. };
  304. /*! 加载 HTML 到弹出层 */
  305. this.modal = function (url, data, name, call, load, tips, area, offset) {
  306. this.load(url, data, 'GET', function (res) {
  307. if (typeof (res) === 'object') return $.msg.auto(res), false;
  308. $.msg.idx.push(layer.open({
  309. type: 1, btn: false, area: area || "800px", resize: false, content: res, title: name || '', offset: offset || 'auto', success: function ($dom, idx) {
  310. $.form.reInit($dom.off('click', '[data-close]').on('click', '[data-close]', function () {
  311. (function (confirm, callable) {
  312. confirm ? $.msg.confirm(confirm, callable) : callable();
  313. })(this.dataset.confirm, function () {
  314. layer.close(idx);
  315. });
  316. }));
  317. }
  318. }));
  319. return (typeof call === 'function') && call.call(that);
  320. }, load, tips);
  321. };
  322. };
  323. /*! 后台菜单辅助插件 */
  324. $.menu = new function () {
  325. var that = this;
  326. /*! 计算 URL 地址中有效的 URI */
  327. this.getUri = function (uri) {
  328. uri = uri || location.href;
  329. uri = (uri.indexOf(location.host) > -1 ? uri.split(location.host)[1] : uri);
  330. return (uri.indexOf('#') > -1 ? uri.split('#')[1] : uri).split('?')[0];
  331. };
  332. /*! 通过 URI 查询最有可能的菜单 NODE */
  333. this.queryNode = function (url, node) {
  334. if (!/^m-/.test(node = node || location.href.replace(/.*spm=([\d\-m]+).*/ig, '$1'))) {
  335. var $menu = $('[data-menu-node][data-open*="' + url.replace(/\.html$/ig, '') + '"]');
  336. return $menu.size() ? $menu.get(0).dataset.menuNode : '';
  337. }
  338. return node;
  339. };
  340. /*! URL 转 URI */
  341. this.parseUri = function (uri, elem, vars, temp, attrs) {
  342. vars = {}, attrs = [], elem = elem || document.createElement('a');
  343. if (uri.indexOf('?') > -1) uri.split('?')[1].split('&').forEach(function (item) {
  344. if (item.indexOf('=') > -1 && (temp = item.split('=')) && typeof temp[0] === 'string' && temp[0].length > 0) {
  345. vars[temp[0]] = decodeURIComponent(temp[1].replace(/%2B/ig, '%20'));
  346. }
  347. });
  348. uri = this.getUri(uri);
  349. if (typeof vars.spm !== 'string') vars.spm = elem.dataset.menuNode || this.queryNode(uri) || '';
  350. if (typeof vars.spm !== 'string' || vars.spm.length < 1) delete vars.spm;
  351. for (var i in vars) attrs.push(i + '=' + vars[i]);
  352. return uri + (attrs.length > 0 ? '?' + attrs.join('&') : '');
  353. };
  354. /*! 后台菜单动作初始化 */
  355. this.listen = function () {
  356. /*! 菜单模式切换 */
  357. var $menu = $('.layui-layout-admin'), miniClass = 'layui-layout-left-mini';
  358. /*! Mini 菜单模式切换及显示 */
  359. if (layui.data('admin-menu-type')['type-mini']) $menu.addClass(miniClass);
  360. /*! 菜单切换事件处理 */
  361. onEvent('click', '[data-target-menu-type]', function () {
  362. layui.data('admin-menu-type', {key: 'type-mini', value: $menu.toggleClass(miniClass).hasClass(miniClass)});
  363. });
  364. /*! 监听窗口尺寸处理 */
  365. (function (callable) {
  366. $(window).on('resize', callable).trigger('resize');
  367. })(function () {
  368. (layui.data('admin-menu-type')['type-mini'] || $body.width() < 1000) ? $menu.addClass(miniClass) : $menu.removeClass(miniClass);
  369. });
  370. /*! Mini 菜单模式时TIPS文字显示 */
  371. $('[data-target-tips]').mouseenter(function () {
  372. if ($menu.hasClass(miniClass)) (function (idx) {
  373. $(this).mouseleave(function () {
  374. layer.close(idx);
  375. });
  376. }).call(this, layer.tips(this.dataset.targetTips || '', this, {time: 0}));
  377. });
  378. /*! 左则二级菜单展示 */
  379. $('[data-submenu-layout]>a').on('click', function () {
  380. that.syncOpenStatus(1);
  381. });
  382. /*! 同步二级菜单展示状态 */
  383. this.syncOpenStatus = function (mode) {
  384. $('[data-submenu-layout]').map(function () {
  385. var node = this.dataset.submenuLayout;
  386. if (mode === 1) layui.data('admin-menu-stat', {key: node, value: $(this).hasClass('layui-nav-itemed') ? 2 : 1});
  387. else if ((layui.data('admin-menu-stat')[node] || 2) === 2) $(this).addClass('layui-nav-itemed');
  388. });
  389. };
  390. window.onhashchange = function () {
  391. var hash = location.hash || '', node;
  392. if (hash.length < 1) return $('[data-menu-node]:first').trigger('click');
  393. if (/^#(https?:)?(\/\/|\\\\)/.test(hash)) return $.msg.tips('禁止访问外部链接!');
  394. // $.msg.page.show(),$.form.load(hash, {}, 'get', $.msg.page.hide, true),that.syncOpenStatus(2);
  395. $.form.load(hash, {}, 'get', false, !$.msg.page.stat()), that.syncOpenStatus(2);
  396. /*! 菜单选择切换 */
  397. if (/^m-/.test(node = that.queryNode(that.getUri()))) {
  398. var $all = $('a[data-menu-node]').parent(), tmp = node.split('-'), tmpNode = tmp.shift();
  399. while (tmp.length > 0) {
  400. tmpNode = tmpNode + '-' + tmp.shift();
  401. $all = $all.not($('a[data-menu-node="' + tmpNode + '"]').parent().addClass('layui-this'));
  402. }
  403. $all.removeClass('layui-this');
  404. /*! 菜单模式切换 */
  405. if (node.split('-').length > 2) {
  406. var _tmp = node.split('-'), _node = _tmp.shift() + '-' + _tmp.shift();
  407. $('[data-menu-layout]').not($('[data-menu-layout="' + _node + '"]').removeClass('layui-hide')).addClass('layui-hide');
  408. $('[data-menu-node="' + node + '"]').parent().parent().parent().addClass('layui-nav-itemed');
  409. $('.layui-layout-admin').removeClass('layui-layout-left-hide');
  410. } else {
  411. $('.layui-layout-admin').addClass('layui-layout-left-hide');
  412. }
  413. that.syncOpenStatus(1);
  414. }
  415. };
  416. /*! URI初始化动作 */
  417. window.onhashchange.call(this);
  418. };
  419. };
  420. /*! 注册对象到Jq */
  421. $.vali = function (form, callable) {
  422. if ($(form).attr('submit-listen')) {
  423. return $(form).data('validate');
  424. }
  425. return (new function () {
  426. var that = this;
  427. /* 绑定表单元素 */
  428. this.form = $(form);
  429. /* 绑定元素事件 */
  430. this.evts = 'blur change';
  431. /* 筛选表单元素 */
  432. this.tags = 'input,select,textarea';
  433. /* 预设检测规则 */
  434. this.patterns = {
  435. phone: '^1[3-9][0-9]{9}$',
  436. email: '^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$'
  437. };
  438. /*! 去除字符串的空格 */
  439. this.trim = function (str) {
  440. return str.replace(/(^\s*)|(\s*$)/g, '');
  441. };
  442. /*! 检测属性是否有定义 */
  443. this.hasProp = function (ele, prop) {
  444. if (typeof prop !== "string") return false;
  445. var attrProp = ele.getAttribute(prop);
  446. return typeof attrProp !== 'undefined' && attrProp !== null && attrProp !== false;
  447. };
  448. /*! 正则验证表单元素 */
  449. this.isRegex = function (ele) {
  450. var real = this.trim($(ele).val());
  451. var regexp = ele.getAttribute('pattern');
  452. regexp = that.patterns[regexp] || regexp;
  453. if (real === "" || !regexp) return true;
  454. return new RegExp(regexp, 'i').test(real);
  455. };
  456. /*! 检侧所有表单元素 */
  457. this.checkAllInput = function () {
  458. var isPass = true;
  459. that.form.find(this.tags).each(function () {
  460. if (that.checkInput(this) === false) {
  461. return $(this).focus(), isPass = false;
  462. }
  463. });
  464. return isPass;
  465. };
  466. /*! 检测表单单元 */
  467. this.checkInput = function (input) {
  468. if (this.hasProp(input, 'data-auto-none')) return true;
  469. var type = (input.getAttribute("type") || '').replace(/\W+/, "").toLowerCase();
  470. var ingoreTypes = ['file', 'reset', 'image', 'radio', 'checkbox', 'submit', 'hidden'];
  471. if (ingoreTypes.length > 0) for (var i in ingoreTypes) if (type === ingoreTypes[i]) return true;
  472. if (this.hasProp(input, "required") && this.trim($(input).val()) === '') return this.remind(input);
  473. return this.isRegex(input) ? (this.hideError(input), true) : this.remind(input);
  474. };
  475. /*! 显示验证标志 */
  476. this.remind = function (input) {
  477. if (!$(input).is(':visible')) return true;
  478. return this.showError(input, input.getAttribute('title') || input.getAttribute('placeholder') || '输入错误'), false;
  479. };
  480. /*! 错误消息显示 */
  481. this.showError = function (ele, tip) {
  482. $(ele).addClass('validate-error');
  483. this.insertError(ele).addClass('layui-anim-fadein').css({width: 'auto'}).html(tip);
  484. };
  485. /*! 错误消息消除 */
  486. this.hideError = function (ele) {
  487. $(ele).removeClass('validate-error');
  488. this.insertError(ele).removeClass('layui-anim-fadein').css({width: '30px'}).html('');
  489. };
  490. /*! 错误标签插入 */
  491. this.insertError = function (ele) {
  492. if ($(ele).data('input-info')) return $(ele).data('input-info');
  493. var $html = $('<span class="absolute block layui-anim text-center font-s12 notselect" style="color:#A44;z-index:2"></span>');
  494. var $next = $(ele).nextAll('.input-right-icon'), right = ($next ? $next.width() + parseFloat($next.css('right') || '0') : 0) + 10;
  495. var style = {top: $(ele).position().top + 'px', right: right + 'px', lineHeight: ele.nodeName === 'TEXTAREA' ? '32px' : $(ele).css('height')};
  496. return $(ele).data('input-info', $html.css(style).insertAfter(ele)), $html;
  497. };
  498. /*! 表单验证入口 */
  499. that.form.off(that.evts, that.tags).on(that.evts, that.tags, function () {
  500. that.checkInput(this);
  501. }).attr('novalidate', 'novalidate').attr('submit-listen', 'validate.submit');
  502. /*! 绑定提交事件 */
  503. that.form.data('validate', this).bind("submit", function (evt) {
  504. evt.button = that.form.find('button[type=submit],button:not([type=button])');
  505. /* 检查所有表单元素是否通过H5的规则验证 */
  506. if (that.checkAllInput() && typeof callable === 'function') {
  507. if (typeof CKEDITOR === 'object' && typeof CKEDITOR.instances === 'object') {
  508. for (var i in CKEDITOR.instances) CKEDITOR.instances[i].updateElement();
  509. }
  510. /* 触发表单提交后,锁定三秒不能再次提交表单 */
  511. if (that.form.attr('submit-locked')) return false;
  512. that.form.attr('submit-locked', 1), evt.button.addClass('submit-button-loading');
  513. callable.call(this, that.form.formToJson()), setTimeout(function () {
  514. that.form.removeAttr('submit-locked'), evt.button.removeClass('submit-button-loading');
  515. }, 3000);
  516. }
  517. return evt.preventDefault(), false;
  518. }).find('[data-form-loaded]').map(function () {
  519. $(this).html(this.dataset.formLoaded || this.innerHTML);
  520. $(this).removeAttr('data-form-loaded').removeClass('layui-disabled');
  521. });
  522. });
  523. };
  524. /*! 自动监听规则内表单 */
  525. $.vali.listen = function () {
  526. $('form[data-auto]').map(function (index, form) {
  527. $(this).vali(function (data) {
  528. var type = form.method || 'POST', href = form.action || location.href;
  529. var call = window[form.dataset.callable || '_default_callable'] || undefined;
  530. var tips = form.dataset.tips || undefined, time = form.dataset.time || undefined;
  531. (function (confirm, callable) {
  532. confirm ? $.msg.confirm(confirm, callable) : callable();
  533. })(form.dataset.confirm, function () {
  534. $.form.load(href, data, type, call, true, tips, time);
  535. });
  536. });
  537. });
  538. };
  539. /*! 注册对象到JqFn */
  540. $.fn.vali = function (callable) {
  541. return this.each(function () {
  542. $.vali(this, callable);
  543. });
  544. };
  545. /*! 表单转JSON */
  546. $.fn.formToJson = function () {
  547. var self = this, data = {}, push = {};
  548. var rules = {key: /[a-zA-Z0-9_]+|(?=\[])/g, push: /^$/, fixed: /^\d+$/, named: /^[a-zA-Z0-9_]+$/};
  549. this.build = function (base, key, value) {
  550. return (base[key] = value), base;
  551. };
  552. this.pushCounter = function (name) {
  553. if (push[name] === undefined) push[name] = 0;
  554. return push[name]++;
  555. };
  556. $.each($(this).serializeArray(), function () {
  557. var key, keys = this.name.match(rules.key), merge = this.value, name = this.name;
  558. while ((key = keys.pop()) !== undefined) {
  559. name = name.replace(new RegExp("\\[" + key + "\\]$"), '');
  560. if (key.match(rules.push)) merge = self.build([], self.pushCounter(name), merge);
  561. else if (key.match(rules.fixed)) merge = self.build([], key, merge);
  562. else if (key.match(rules.named)) merge = self.build({}, key, merge);
  563. }
  564. data = $.extend(true, data, merge);
  565. });
  566. return data;
  567. };
  568. /*! 全局文件上传插件 */
  569. $.fn.uploadFile = function (callable, initialize) {
  570. return this.each(function () {
  571. if ($(this).data('inited')) return false;
  572. var that = $(this), mult = '|one|btn|'.indexOf(that.data('file') || 'one') > -1 ? 0 : 1;
  573. that.data('inited', true).data('multiple', mult), require(['upload'], function (apply) {
  574. apply(that, callable), (typeof initialize === 'function' && setTimeout(initialize, 100));
  575. });
  576. });
  577. };
  578. /*! 上传单张图片 */
  579. $.fn.uploadOneImage = function () {
  580. return this.each(function () {
  581. if ($(this).data('inited')) return true; else $(this).data('inited', true);
  582. var $in = $(this), $bt = $('<a data-file class="uploadimage transition"><span class="layui-icon">&#x1006;</span></a>').data('input', this);
  583. $bt.attr('data-size', $in.data('size') || 0).attr('data-type', $in.data('type') || 'png,jpg,gif').find('span').on('click', function (event) {
  584. event.stopPropagation(), $bt.attr('style', ''), $in.val('');
  585. }), $in.on('change', function () {
  586. if (this.value) $bt.css('backgroundImage', 'url(' + encodeURI(this.value) + ')');
  587. }).after($bt).trigger('change');
  588. });
  589. };
  590. /*! 上传多张图片 */
  591. $.fn.uploadMultipleImage = function () {
  592. return this.each(function () {
  593. if ($(this).data('inited')) return true; else $(this).data('inited', true);
  594. var $in = $(this), $bt = $('<a data-file="mul" class="uploadimage"></a>'), imgs = this.value ? this.value.split('|') : []
  595. $in.after($bt.attr('data-size', $in.data('size') || 0).attr('data-type', $in.data('type') || 'png,jpg,gif').uploadFile(function (src) {
  596. imgs.push(src), $in.val(imgs.join('|')), showImageContainer([src]);
  597. })), (imgs.length > 0 && showImageContainer(imgs));
  598. function showImageContainer(srcs) {
  599. $(srcs).each(function (idx, src, $image) {
  600. $image = $('<div class="uploadimage uploadimagemtl transition"><div><a class="layui-icon">&#xe603;</a><a class="layui-icon">&#x1006;</a><a class="layui-icon">&#xe602;</a></div></div>');
  601. $image.attr('data-tips-image', encodeURI(src)).css('backgroundImage', 'url(' + encodeURI(src) + ')').on('click', 'a', function (event, index, prevs, $item) {
  602. event.stopPropagation(), $item = $(this).parent().parent(), index = $(this).index();
  603. if (index === 2 && $item.index() !== $bt.prevAll('div.uploadimage').length) $item.next().after($item);
  604. else if (index === 0 && $item.index() > 1) $item.prev().before($item); else if (index === 1) $item.remove();
  605. imgs = [], $bt.prevAll('.uploadimage').map(function () {
  606. imgs.push($(this).attr('data-tips-image'));
  607. });
  608. imgs.reverse(), $in.val(imgs.join('|'));
  609. }), $bt.before($image);
  610. });
  611. }
  612. });
  613. };
  614. /*! 标签输入插件 */
  615. $.fn.initTagInput = function () {
  616. return this.each(function () {
  617. var $this = $(this), tags = this.value ? this.value.split(',') : [];
  618. var $text = $('<textarea class="layui-input layui-input-inline layui-tag-input"></textarea>');
  619. var $tags = $('<div class="layui-tags"></div>').append($text);
  620. $this.parent().append($tags), $text.off('keydown blur'), (tags.length > 0 && showTags(tags));
  621. $text.on('keydown blur', function (event, value) {
  622. if (event.keyCode === 13 || event.type === 'blur') {
  623. event.preventDefault(), (value = $text.val().replace(/^\s*|\s*$/g, ''));
  624. if (tags.indexOf($(this).val()) > -1) return layer.msg('该标签已经存在!');
  625. if (value.length > 0) tags.push(value), $this.val(tags.join(',')), showTags([value]), this.focus(), $text.val('');
  626. }
  627. });
  628. function showTags(tagsArr) {
  629. $(tagsArr).each(function (idx, text, elem) {
  630. elem = $('<div class="layui-tag"></div>').html(text + '<i class="layui-icon">&#x1006;</i>');
  631. elem.on('click', 'i', function (tagText, tagIdx) {
  632. tagText = $(this).parent().text(), tagIdx = tags.indexOf(tagText);
  633. tags.splice(tagIdx, 1), $(this).parent().remove(), $this.val(tags.join(','));
  634. }), $tags.append(elem, $text);
  635. });
  636. }
  637. });
  638. };
  639. /*! 文本框插入内容 */
  640. $.fn.insertAtCursor = function (value) {
  641. return this.each(function () {
  642. if (document.selection) {
  643. this.focus();
  644. var selection = document.selection.createRange();
  645. (selection.text = value), selection.select();
  646. } else if (this.selectionStart || this.selectionStart === 0) {
  647. var startPos = this.selectionStart, afterPos = this.selectionAfter, scrollTop = this.scrollTop;
  648. this.value = this.value.substring(0, startPos) + value + this.value.substring(afterPos, this.value.length);
  649. if (scrollTop > 0) this.scrollTop = scrollTop;
  650. this.focus();
  651. this.selectionAfter = startPos + value.length;
  652. this.selectionStart = startPos + value.length;
  653. } else (this.value += value), this.focus();
  654. });
  655. }
  656. /*! 组件 layui.table 封装 */
  657. $.fn.layTable = function (params) {
  658. return this.each(function (idx, elem) {
  659. // 动态初始化数据表
  660. this.id = this.id || 't' + Math.random().toString().replace('.', '');
  661. this.setAttribute('lay-filter', this.dataset.id = this.getAttribute('lay-filter') || this.id);
  662. // 插件初始化参数
  663. var opt = params || {}, data = opt.where || {}, sort = opt.initSort || opt.sort || {};
  664. opt.id = elem.id, opt.elem = elem, opt.url = params.url || elem.dataset.url || location.href;
  665. opt.page = params.page !== false ? (params.page || true) : false, opt.limit = params.limit || 20;
  666. opt.loading = params.loading === true, opt.autoSort = params.autoSort === true, opt.cols = params.cols || [[]];
  667. // 默认动态设置页数, 动态设置最大高度
  668. if (opt.page === true) opt.page = {curr: layui.sessionData('pages')[opt.id] || 1}
  669. if (opt.height === 'full') if ($(elem).parents('.iframe-pagination').size()) {
  670. $(elem).parents('.iframe-pagination').addClass('not-footer');
  671. opt.height = $(window).height() - $(elem).removeClass('layui-hide').offset().top - 20;
  672. } else {
  673. opt.height = $(window).height() - $(elem).removeClass('layui-hide').offset().top - 35;
  674. }
  675. // 动态计算最大页数
  676. opt.done = function () {
  677. layui.sessionData('pages', {key: elem.id, value: this.page.curr || 1}), (this.loading = true);
  678. this.elem.next().find('[data-load],[data-queue],[data-action],[data-iframe]').not('[data-table-id]').attr('data-table-id', elem.id);
  679. }, opt.parseData = function (res) {
  680. var maxPage = Math.ceil(res.count / this.limit), curPage = layui.sessionData('pages')[opt.id] || 1;
  681. if (curPage > maxPage && curPage > 1) this.elem.trigger('reload', {page: {curr: maxPage}});
  682. };
  683. // 实例并绑定的对象
  684. $(this).data('this', layui.table.render(bindData(opt)));
  685. // 绑定实例重载事件
  686. $(this).bind('reload', function (evt, opts) {
  687. opts = opts || {}, opts.loading = true;
  688. data = $.extend({}, data, opts.where || {});
  689. layui.table.reload(elem.id, bindData(opts || {}));
  690. }).bind('row sort tool edit radio toolbar checkbox rowDouble', function (evt, call) {
  691. layui.table.on(evt.type + '(' + elem.dataset.id + ')', call)
  692. }).bind('setFullHeight', function () {
  693. $(elem).trigger('reload', {height: $(window).height() - $(elem).next().offset().top - 35})
  694. }).trigger('sort', function (object) {
  695. (sort = object), $(elem).trigger('reload')
  696. });
  697. // 搜索表单关联对象
  698. var search = params.search || this.dataset.targetSearch;
  699. if (search) $body.find(search).map(function () {
  700. $(this).attr('data-table-id', elem.id);
  701. });
  702. // 绑定选择项关联对象
  703. var checked = params.checked || this.dataset.targetChecked;
  704. if (checked) $body.find(checked).map(function () {
  705. $(this).attr('data-table-id', elem.id);
  706. });
  707. // 生成初始化参数
  708. function bindData(opts) {
  709. data['output'] = 'layui.table';
  710. if (sort.field && sort.type) {
  711. data['_order_'] = sort.type, data['_field_'] = sort.field;
  712. opts.initSort = {type: sort.type.split(',')[0].split(' ')[0], field: sort.field.split(',')[0].split(' ')[0]};
  713. }
  714. return (opts['where'] = data), opts;
  715. }
  716. });
  717. }
  718. /*! 弹出图片层 */
  719. $.previewImage = function (src, area) {
  720. var img = new Image(), defer = $.Deferred(), loaded = $.msg.loading();
  721. img.style.background = '#FFF', img.referrerPolicy = 'no-referrer';
  722. img.style.height = 'auto', img.style.width = area || '100%', img.style.display = 'none';
  723. document.body.appendChild(img), img.onerror = function () {
  724. $.msg.close(loaded), defer.reject();
  725. }, img.onload = function () {
  726. layer.open({
  727. type: 1, title: false, shadeClose: true, content: $(img), success: function ($elem, idx) {
  728. $.msg.close(loaded), defer.notify($elem, idx);
  729. }, area: area || '480px', skin: 'layui-layer-nobg', closeBtn: 1, end: function () {
  730. document.body.removeChild(img), defer.resolve()
  731. }
  732. });
  733. };
  734. return (img.src = src), defer.resolve();
  735. };
  736. /*! 以手机模式显示内容 */
  737. $.previewPhonePage = function (href, title) {
  738. var template = '<div class="mobile-preview"><div class="mobile-header">{{d.title}}</div><div class="mobile-body"><iframe src="{{d.url}}"></iframe></div></div>';
  739. layer.style(layer.open({type: true, resize: false, scrollbar: false, area: ['320px', '600px'], title: false, closeBtn: true, shadeClose: false, skin: 'layui-layer-nobg', content: layui.laytpl(template).render({title: title || '公众号', url: href})}), {boxShadow: 'none'});
  740. };
  741. /*! 显示任务进度消息 */
  742. $.loadQueue = function (code, doScript, element) {
  743. var doAjax = true, doReload = false;
  744. layui.layer.open({
  745. type: 1, title: false, area: ['560px', '315px'], anim: 2, shadeClose: false, end: function () {
  746. doAjax = false;
  747. if (doReload && doScript) {
  748. if (element && element.dataset && element.dataset.tableId) {
  749. $('#' + element.dataset.tableId).trigger('reload');
  750. } else {
  751. $.form.reload();
  752. }
  753. }
  754. }, content: '' +
  755. '<div class="padding-30 padding-bottom-0" data-queue-load="' + code + '">' +
  756. ' <div class="layui-elip notselect nowrap" data-message-title></div>' +
  757. ' <div class="margin-top-15 layui-progress layui-progress-big" lay-showPercent="yes"><div class="layui-progress-bar transition" lay-percent="0.00%"></div></div>' +
  758. ' <div class="margin-top-15"><code class="layui-textarea layui-bg-black border-0" disabled style="resize:none;overflow:hidden;height:190px"></code></div>' +
  759. '</div>',
  760. success: function ($elem) {
  761. new function () {
  762. var that = this;
  763. this.$box = $elem.find('[data-queue-load=' + code + ']');
  764. if (doAjax === false || this.$box.length < 1) return false;
  765. this.$coder = this.$box.find('code'), this.$name = this.$box.find('[data-message-title]');
  766. this.$percent = this.$box.find('.layui-progress div'), this.SetCache = function (code, index, value) {
  767. var ckey = code + '_' + index, ctype = 'admin-queue-script';
  768. return value !== undefined ? layui.data(ctype, {key: ckey, value: value}) : layui.data(ctype)[ckey] || 0;
  769. }, this.SetState = function (status, message) {
  770. if (message.indexOf('javascript:') === -1) if (status === 1) {
  771. that.$name.html('<b class="color-text">' + message + '</b>').addClass('text-center');
  772. that.$percent.addClass('layui-bg-blue').removeClass('layui-bg-green layui-bg-red');
  773. } else if (status === 2) {
  774. if (message.indexOf('>>>') > -1) {
  775. that.$name.html('<b class="color-blue">' + message + '</b>').addClass('text-center');
  776. } else {
  777. that.$name.html('<b class="color-blue">正在处理:</b>' + message).removeClass('text-center');
  778. }
  779. that.$percent.addClass('layui-bg-blue').removeClass('layui-bg-green layui-bg-red');
  780. } else if (status === 3) {
  781. doReload = true;
  782. that.$name.html('<b class="color-green">' + message + '</b>').addClass('text-center');
  783. that.$percent.addClass('layui-bg-green').removeClass('layui-bg-blue layui-bg-red');
  784. } else if (status === 4) {
  785. that.$name.html('<b class="color-red">' + message + '</b>').addClass('text-center');
  786. that.$percent.addClass('layui-bg-red').removeClass('layui-bg-blue layui-bg-green');
  787. }
  788. }, (this.LoadProgress = function () {
  789. if (doAjax === false || that.$box.length < 1) return false;
  790. $.form.load(tapiRoot + '/api.queue/progress', {code: code}, 'post', function (ret) {
  791. if (ret.code) {
  792. var lines = [];
  793. for (var idx in ret.data.history) {
  794. var line = ret.data.history[idx], percent = '[ ' + line.progress + '% ] ';
  795. if (line.message.indexOf('javascript:') === -1) lines.push(line.message.indexOf('>>>') > -1 ? line.message : percent + line.message);
  796. else if (!that.SetCache(code, idx) && doScript !== false) that.SetCache(code, idx, 1), location.href = line.message;
  797. }
  798. if (ret.data.status > 0) {
  799. that.SetState(parseInt(ret.data.status), ret.data.message);
  800. that.$percent.attr('lay-percent', (parseFloat(ret.data.progress || '0.00').toFixed(2)) + '%'), layui.element.render();
  801. that.$coder.html('<p class="layui-elip">' + lines.join('</p><p class="layui-elip">') + '</p>').animate({scrollTop: that.$coder[0].scrollHeight + 'px'}, 200);
  802. return parseInt(ret.data.status) === 3 || parseInt(ret.data.status) === 4 || setTimeout(that.LoadProgress, Math.floor(Math.random() * 200)), false;
  803. } else return setTimeout(that.LoadProgress, Math.floor(Math.random() * 500) + 200), false;
  804. }
  805. }, false);
  806. })();
  807. };
  808. }
  809. });
  810. };
  811. /*! 注册 data-search 表单搜索行为 */
  812. onEvent('submit', 'form.form-search', function () {
  813. var tableId = this.dataset.tableId;
  814. if (tableId) return $('table#' + tableId).trigger('reload', {
  815. page: {curr: 1}, where: $(this).formToJson()
  816. });
  817. var url = $(this).attr('action').replace(/&?page=\d+/g, '');
  818. if ((this.method || 'get').toLowerCase() === 'get') {
  819. var split = url.indexOf('?') > -1 ? '&' : '?';
  820. var stype = location.href.indexOf('spm=') > -1 ? '#' : '';
  821. return location.href = stype + $.menu.parseUri(url + split + $(this).serialize());
  822. }
  823. return $.form.load(url, this, 'post');
  824. });
  825. /*! 注册 data-file 事件行为 */
  826. onEvent('click', '[data-file]', function () {
  827. if ($(this).data('inited') !== true) (function (that) {
  828. that.uploadFile(undefined, function () {
  829. that.trigger('upload.start');
  830. });
  831. })($(this));
  832. });
  833. /*! 注册 data-load 事件行为 */
  834. onEvent('click', '[data-load]', function () {
  835. var emap = this.dataset, data = {};
  836. if (this.dataset.rule && (applyRuleValue(this, data)) === false) return false;
  837. (function (confirm, callable) {
  838. confirm ? $.msg.confirm(confirm, callable) : callable();
  839. })(emap.confirm, function () {
  840. var call = !emap.tableId ? false : function (ret) {
  841. if (ret.code > 0) return $.msg.success(ret.info, 3, function () {
  842. $('#' + emap.tableId).trigger('reload');
  843. }), false;
  844. }
  845. $.form.load(emap.load, data, 'get', call, true, emap.tips, emap.time);
  846. });
  847. });
  848. /*! 注册 data-reload 事件行为 */
  849. onEvent('click', '[data-reload]', function () {
  850. this.dataset.tableId ? $('#' + this.dataset.tableId).trigger('reload') : $.form.reload();
  851. });
  852. /*! 注册 data-dbclick 事件行为 */
  853. onEvent('dblclick', '[data-dbclick]', function () {
  854. $(this).find(this.dataset.dbclick || '[data-dbclick]').trigger('click');
  855. });
  856. /*! 注册 data-check 事件行为 */
  857. onEvent('click', '[data-check-target]', function () {
  858. var target = this;
  859. $(this.dataset.checkTarget).map(function () {
  860. (this.checked = !!target.checked), $(this).trigger('change');
  861. });
  862. });
  863. /*! 表单元素失去焦点时数字 */
  864. onEvent('blur', '[data-blur-number]', function () {
  865. var emap = this.dataset, min = emap.valueMin, max = emap.valueMax;
  866. var value = parseFloat(this.value) || 0, fiexd = parseInt(emap.blurNumber || 0);
  867. if (typeof min !== 'undefined' && value < min) value = min;
  868. if (typeof max !== 'undefined' && value > max) value = max;
  869. this.value = parseFloat(value).toFixed(fiexd);
  870. });
  871. /*! 表单元素失焦时提交 */
  872. onEvent('blur', '[data-action-blur],[data-blur-action]', function () {
  873. var that = $(this), emap = this.dataset, data = {'_token_': emap.token || emap.csrf || '--'};
  874. var attrs = (emap.value || '').replace('{value}', that.val()).split(';');
  875. for (var i in attrs) data[attrs[i].split('#')[0]] = attrs[i].split('#')[1];
  876. (function (confirm, callable) {
  877. confirm ? $.msg.confirm(confirm, callable) : callable();
  878. })(emap.confirm, function () {
  879. $.form.load(emap.actionBlur || emap.blurAction, data, emap.method || 'post', function (ret) {
  880. return that.css('border', (ret && ret.code) ? '1px solid #e6e6e6' : '1px solid red'), false;
  881. }, emap.loading !== 'false', emap.loading, emap.time)
  882. });
  883. });
  884. /*! 注册 data-href 事件行为 */
  885. onEvent('click', '[data-href]', function () {
  886. if (this.dataset.href && this.dataset.href.indexOf('#') !== 0) {
  887. location.href = this.dataset.href;
  888. }
  889. });
  890. /*! 注册 data-open 事件行为 */
  891. onEvent('click', '[data-open]', function () {
  892. if (this.dataset.open.match(/^https?:/)) {
  893. location.href = this.dataset.open;
  894. } else {
  895. $.form.href(this.dataset.open, this);
  896. }
  897. });
  898. /*! 注册 data-action 事件行为 */
  899. onEvent('click', '[data-action]', function () {
  900. var emap = this.dataset, data = {'_token_': emap.token || emap.csrf || '--'};
  901. var load = emap.loading !== 'false', tips = typeof load === 'string' ? load : undefined;
  902. if ((applyRuleValue(this, data)) === false) return false;
  903. (function (confirm, callable) {
  904. confirm ? $.msg.confirm(confirm, callable) : callable();
  905. })(emap.confirm, function () {
  906. var call = !emap.tableId ? false : function (ret) {
  907. if (ret.code > 0) return $.msg.success(ret.info, 3, function () {
  908. $('#' + emap.tableId).trigger('reload');
  909. }), false;
  910. }
  911. $.form.load(emap.action, data, emap.method || 'post', call, load, tips, emap.time)
  912. });
  913. });
  914. /*! 注册 data-modal 事件行为 */
  915. onEvent('click', '[data-modal]', function () {
  916. var emap = this.dataset, data = {open_type: 'modal'}, un = undefined;
  917. if (emap.rule && (applyRuleValue(this, data)) === false) return false;
  918. return $.form.modal(emap.modal, data, emap.title || this.innerText || '编辑', un, un, un, emap.area || emap.width || '800px', emap.offset || 'auto');
  919. });
  920. /*! 注册 data-iframe 事件行为 */
  921. onEvent('click', '[data-iframe]', function () {
  922. var emap = this.dataset, data = {open_type: 'iframe'};
  923. if (emap.rule && (applyRuleValue(this, data)) === false) return false;
  924. var name = emap.title || this.innerText || 'IFRAME 窗口';
  925. var area = emap.area || [emap.width || '800px', emap.height || '580px'];
  926. var frame = emap.iframe + (emap.iframe.indexOf('?') > -1 ? '&' : '?') + $.param(data);
  927. $(this).attr('data-index', $.form.iframe(frame, name, area, emap.offset || 'auto', emap.tableId ? function () {
  928. typeof emap.refresh !== 'undefined' && $('#' + emap.tableId).trigger('reload');
  929. } : function () {
  930. typeof emap.refresh !== 'undefined' && $.form.reload();
  931. }));
  932. });
  933. /*! 注册 data-icon 事件行为 */
  934. onEvent('click', '[data-icon]', function () {
  935. var location = tapiRoot + '/api.plugs/icon', field = this.dataset.icon || this.dataset.field || 'icon';
  936. $.form.iframe(location + (location.indexOf('?') > -1 ? '&' : '?') + 'field=' + field, '图标选择', ['900px', '700px']);
  937. });
  938. /*! 注册 data-copy 事件行为 */
  939. onEvent('click', '[data-copy]', function () {
  940. (function (content, $textarea) {
  941. $body.append($textarea.val(content)), $textarea.select();
  942. document.execCommand('Copy') ? $.msg.tips('已复制到剪贴板!') : $.msg.tips('请使用鼠标操作复制!');
  943. $textarea.remove();
  944. })(this.dataset.copy, $('<textarea style="position:fixed;top:-500px"></textarea>'));
  945. });
  946. /*! 异步任务状态监听与展示 */
  947. onEvent('click', '[data-queue]', function () {
  948. var that = this;
  949. (function (confirm, callable) {
  950. confirm ? $.msg.confirm(confirm, callable) : callable();
  951. })(this.dataset.confirm, function () {
  952. $.form.load(that.dataset.queue, {}, 'post', function (ret) {
  953. if (typeof ret.data === 'string' && ret.data.indexOf('Q') === 0) {
  954. return $.loadQueue(ret.data, true, that), false;
  955. }
  956. });
  957. });
  958. });
  959. /*! 注册 data-tips-text 事件行为 */
  960. onEvent('mouseenter', '[data-tips-text]', function () {
  961. var opts = {tips: [$(this).attr('data-tips-type') || 3, '#78BA32'], time: 0};
  962. var layidx = layer.tips($(this).attr('data-tips-text') || this.innerText, this, opts);
  963. $(this).off('mouseleave').on('mouseleave', function () {
  964. setTimeout("layui.layer.close('" + layidx + "')", 100);
  965. });
  966. });
  967. /*! 注册 data-tips-hover 事件行为 */
  968. onEvent('mouseenter', '[data-tips-image][data-tips-hover]', function () {
  969. var img = new Image(), ele = $(this);
  970. if ((img.src = this.dataset.tipsImage || this.dataset.lazySrc || this.src)) {
  971. img.layopt = {time: 0, skin: 'layui-layer-image', anim: 5, isOutAnim: false, scrollbar: false};
  972. img.referrerPolicy = 'no-referrer', img.style.maxWidth = '260px', img.style.maxHeight = '260px';
  973. ele.data('layidx', layer.tips(img.outerHTML, this, img.layopt)).off('mouseleave').on('mouseleave', function () {
  974. layui.layer.close(ele.data('layidx'));
  975. });
  976. }
  977. });
  978. /*! 注册 data-tips-image 事件行为 */
  979. onEvent('click', '[data-tips-image]', function () {
  980. $.previewImage(this.dataset.tipsImage || this.dataset.lazySrc || this.src, this.dataset.with);
  981. });
  982. /*! 注册 data-phone-view 事件行为 */
  983. onEvent('click', '[data-phone-view]', function () {
  984. $.previewPhonePage(this.dataset.phoneView || this.href);
  985. });
  986. /*! 表单编辑返回操作 */
  987. onEvent('click', '[data-history-back]', function () {
  988. $.msg.confirm(this.dataset.historyBack || '确定要返回吗?', function () {
  989. history.back();
  990. })
  991. });
  992. /*! 延时关闭加载动画 */
  993. window.addEventListener('load', function () {
  994. setTimeout("$('body>.think-page-loader').fadeOut()", 200);
  995. }, true);
  996. /*! 图片加载异常处理 */
  997. document.addEventListener('error', function (event) {
  998. var elem = event.target;
  999. if (elem.nodeName === 'IMG') {
  1000. elem.src = baseRoot + 'theme/img/404_icon.png';
  1001. }
  1002. }, true);
  1003. /*! 系统菜单表单页面初始化 */
  1004. $.menu.listen(), $.vali.listen(), $.form.reInit($body);
  1005. });