common.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. function closeBox(obj) {
  2. var elem = $(obj).parents(".template-edit-title").next();
  3. if ($(elem).hasClass("layui-hide")) {
  4. $(elem).removeClass("layui-hide");
  5. $(obj).removeClass("closed-right");
  6. } else {
  7. $(elem).addClass("layui-hide");
  8. $(obj).addClass("closed-right");
  9. }
  10. }
  11. function get_math_rant(len) {
  12. return Number(Math.random().toString().substr(3, len) + Date.now()).toString(36);
  13. }
  14. var show_link_box_flag = true;
  15. function select_link(link, support_diy_view, callback) {
  16. var url = linkUrl;
  17. if (show_link_box_flag) {
  18. show_link_box_flag = false;
  19. $.post(url, {link: JSON.stringify(link), support_diy_view: support_diy_view}, function (str) {
  20. window.linkIndex = layer.open({
  21. type: 1,
  22. title: "选择链接",
  23. content: str,
  24. btn: [],
  25. area: ['850px'], //宽高
  26. maxWidth: 1920,
  27. cancel: function (index, layero) {
  28. show_link_box_flag = true;
  29. },
  30. end: function () {
  31. if (window.linkData) {
  32. if (callback) callback(window.linkData);
  33. delete window.linkData;// 清空本次选择
  34. }
  35. show_link_box_flag = true;
  36. }
  37. });
  38. });
  39. }
  40. };
  41. /**
  42. * 数据表格
  43. * layui官方文档:https://www.layui.com/doc/modules/table.html
  44. * @param options
  45. * @constructor
  46. */
  47. function Table(options) {
  48. if (!options) return;
  49. var _self = this;
  50. options.parseData = options.parseData || function (data) {
  51. return {
  52. "code": data.code,
  53. "msg": data.message,
  54. "count": data.data.count,
  55. "data": data.data.list
  56. };
  57. };
  58. options.request = options.request || {
  59. limitName: 'page_size' //每页数据量的参数名,默认:limit
  60. };
  61. if (options.page == undefined) {
  62. options.page = {
  63. layout: ['count', 'limit', 'prev', 'page', 'next'],
  64. limit: 10
  65. };
  66. }
  67. options.defaultToolbar = options.defaultToolbar || [];//'filter', 'print', 'exports'
  68. options.toolbar = options.toolbar || "";//头工具栏事件
  69. options.skin = options.skin || 'line';
  70. options.size = options.size || 'lg';
  71. options.async = (options.async != undefined) ? options.async : true;
  72. options.done = function (res, curr, count) {
  73. //加载图片放大
  74. loadImgMagnify();
  75. if (options.callback) options.callback(res, curr, count);
  76. };
  77. layui.use('table', function () {
  78. _self._table = layui.table;
  79. _self._table.render(options);
  80. });
  81. this.filter = options.filter || options.elem.replace(/#/g, "");
  82. this.elem = options.elem;
  83. //获取当前选中的数据
  84. this.checkStatus = function () {
  85. return this._table.checkStatus(_self.elem.replace(/#/g, ""));
  86. };
  87. }
  88. /**
  89. * 监听头工具栏事件
  90. * @param callback 回调
  91. */
  92. Table.prototype.toolbar = function (callback) {
  93. var _self = this;
  94. var interval = setInterval(function () {
  95. if (_self._table) {
  96. _self._table.on('toolbar(' + _self.filter + ')', function (obj) {
  97. var checkStatus = _self._table.checkStatus(obj.config.id);
  98. obj.data = checkStatus.data;
  99. obj.isAll = checkStatus.isAll;
  100. if (callback) callback.call(this, obj);
  101. });
  102. clearInterval(interval);
  103. }
  104. }, 50);
  105. };
  106. /**
  107. * 监听底部工具栏事件
  108. * @param callback 回调
  109. */
  110. Table.prototype.bottomToolbar = function (callback) {
  111. var _self = this;
  112. var interval = setInterval(function () {
  113. if (_self._table) {
  114. _self._table.on('bottomToolbar(' + _self.filter + ')', function (obj) {
  115. var checkStatus = _self._table.checkStatus(obj.config.id);
  116. obj.data = checkStatus.data;
  117. obj.isAll = checkStatus.isAll;
  118. if (callback) callback.call(this, obj);
  119. });
  120. clearInterval(interval);
  121. }
  122. }, 50);
  123. };
  124. /**
  125. * 绑定layui的on事件
  126. * @param name
  127. * @param callback
  128. */
  129. Table.prototype.on = function (name, callback) {
  130. var _self = this;
  131. var interval = setInterval(function () {
  132. if (_self._table) {
  133. _self._table.on(name + '(' + _self.filter + ')', function (obj) {
  134. if (callback) callback.call(this, obj);
  135. });
  136. clearInterval(interval);
  137. }
  138. }, 50);
  139. };
  140. /**
  141. * //监听行工具事件
  142. * @param callback 回调
  143. */
  144. Table.prototype.tool = function (callback) {
  145. var _self = this;
  146. var interval = setInterval(function () {
  147. if (_self._table) {
  148. _self._table.on('tool(' + _self.filter + ')', function (obj) {
  149. if (callback) callback.call(this, obj);
  150. });
  151. clearInterval(interval);
  152. }
  153. }, 50);
  154. };
  155. /**
  156. * 刷新数据
  157. * @param options 参数,参考layui数据表格参数
  158. */
  159. Table.prototype.reload = function (options) {
  160. options = options || {
  161. page: {
  162. curr: 1
  163. }
  164. };
  165. var _self = this;
  166. var interval = setInterval(function () {
  167. if (_self._table) {
  168. _self._table.reload(_self.elem.replace(/#/g, ""), options);
  169. clearInterval(interval);
  170. }
  171. }, 50);
  172. };
  173. /**
  174. * 自定义分页
  175. * @param options
  176. * @constructor
  177. */
  178. function Page(options) {
  179. if (!options) return;
  180. var _self = this;
  181. options.elem = options.elem.replace(/#/g, "");// 注意:这里不能加 # 号
  182. options.count = options.count || 0;// 数据总数。一般通过服务端得到
  183. options.limit = options.limit || 10;// 每页显示的条数。laypage将会借助 count 和 limit 计算出分页数。
  184. options.limits = options.limits || [];// 每页条数的选择项。如果 layout 参数开启了 limit,则会出现每页条数的select选择框
  185. options.curr = location.hash.replace('#!page=', '');// 起始页。一般用于刷新类型的跳页以及HASH跳页
  186. // options.hash = options.hash || 'page';// 开启location.hash,并自定义 hash 值。如果开启,在触发分页时,会自动对url追加:#!hash值={curr} 利用这个,可以在页面载入时就定位到指定页
  187. options.groups = options.groups || 5;// 连续出现的页码个数
  188. options.prev = options.prev || '<i class="layui-icon layui-icon-left"></i>';// 自定义“上一页”的内容,支持传入普通文本和HTML
  189. options.next = options.next || '<i class="layui-icon layui-icon-right"></i>';// 自定义“下一页”的内容,同上
  190. options.first = options.first || 1;// 自定义“首页”的内容,同上
  191. // 自定义排版。可选值有:count(总条目输区域)、prev(上一页区域)、page(分页区域)、next(下一页区域)、limit(条目选项区域)、refresh(页面刷新区域。注意:layui 2.3.0 新增) 、skip(快捷跳页区域)
  192. options.layout = options.layout || ['count', 'prev', 'page', 'next'];
  193. options.jump = function (obj, first) {
  194. //首次不执行,一定要加此判断,否则初始时会无限刷新
  195. if (!first) {
  196. obj.page = obj.curr;
  197. if(options.callback) options.callback.call(this, obj);
  198. }
  199. };
  200. layui.use('laypage', function () {
  201. _self._page = layui.laypage;
  202. _self._page.render(options);
  203. });
  204. }
  205. $(function () {
  206. loadImgMagnify();
  207. });
  208. //图片最大递归次数
  209. var IMG_MAX_RECURSIVE_COUNT = 6;
  210. var count = 0;
  211. /**
  212. * //加载图片放大
  213. */
  214. function loadImgMagnify() {
  215. setTimeout(function () {
  216. try {
  217. if (layer) {
  218. $("img[src!=''][layer-src]").each(function () {
  219. var id = getId($(this).parent());
  220. layer.photos({
  221. photos: "#" + id,
  222. anim: 5
  223. });
  224. count = 0;
  225. });
  226. }
  227. } catch (e) {
  228. }
  229. }, 200);
  230. }
  231. function getId(o) {
  232. count++;
  233. var id = o.attr("id");
  234. if (id == undefined && count < IMG_MAX_RECURSIVE_COUNT) {
  235. id = getId(o.parent());
  236. }
  237. if (id == undefined) {
  238. id = get_math_rant(10);
  239. o.attr("id", id);
  240. }
  241. return id;
  242. }
  243. /**
  244. * 专题选择器
  245. * @param callback 回调函数
  246. * @param selectId 已选专题id
  247. * @param params
  248. */
  249. function specialSelect(callback, selectId, params) {
  250. layui.use(['layer'], function () {
  251. if (selectId.length) {
  252. params.select_id = selectId.toString();
  253. }
  254. params.disabled = params.disabled == 0 ? 0 : 1;
  255. var url = specialUrl;
  256. //iframe层-父子操作
  257. layer.open({
  258. title: "专题选择",
  259. type: 2,
  260. area: ['1000px', '720px'],
  261. fixed: false, //不固定
  262. btn: ['保存', '返回'],
  263. content: url,
  264. yes: function (index, layero) {
  265. var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象,执行iframe页的方法:
  266. iframeWin.selectSpecial(function (obj) {
  267. if (typeof callback == "string") {
  268. try {
  269. eval(callback + '(obj)');
  270. layer.close(index);
  271. } catch (e) {
  272. console.error('回调函数' + callback + '未定义');
  273. }
  274. } else if (typeof callback == "function") {
  275. callback(obj);
  276. layer.close(index);
  277. }
  278. });
  279. }
  280. });
  281. });
  282. }