View.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // 模块窗口
  2. $layout_module = $('#layout-module-win');
  3. $layout_content = $('.layout-content');
  4. $(function()
  5. {
  6. // 布局-拖拽-初始化
  7. $(".layout-content").dragsort(
  8. {
  9. dragSelector: ".drag-submit",
  10. dragEnd: function()
  11. {
  12. var data = $(".layout-content .drag-submit").map(function() { return $(this).parents('.layout-view').data('layout-id'); }).get();
  13. // ajax请求
  14. $.ajax({
  15. url:$layout_content.data('layout-sort-save-url'),
  16. type:'POST',
  17. dataType:"json",
  18. timeout:10000,
  19. data:{"data":data},
  20. success:function(result)
  21. {
  22. if(result.code == 0)
  23. {
  24. Prompt(result.msg, 'success');
  25. } else {
  26. Prompt(result.msg);
  27. }
  28. },
  29. error:function()
  30. {
  31. Prompt('网络异常错误');
  32. }
  33. });
  34. },
  35. placeHolderTemplate: "<div class='layout-view-drag'></div>"
  36. });
  37. // 模块-添加
  38. $(document).on('click', '.layout-submit-add', function()
  39. {
  40. var url = $layout_content.data('module-add-url');
  41. var tag = $(this).data('tag');
  42. var id = $(this).data('id');
  43. var value = $(this).data('value');
  44. var html = $(this).data('html');
  45. if(url != undefined && tag != undefined && value != undefined && id != undefined && html != undefined)
  46. {
  47. // ajax请求
  48. $.ajax({
  49. url:url,
  50. type:'POST',
  51. dataType:"json",
  52. timeout:10000,
  53. data:{"id":id},
  54. success:function(result)
  55. {
  56. if(result.code == 0)
  57. {
  58. // 数据替换-添加
  59. html = html.replace(/{data-module-id}/g, result.data);
  60. html = html.replace(/{data-value}/g, value);
  61. html = html.replace(/{content-module-id}/g, 'layout-content-'+value+'-'+result.data);
  62. html = html.replace(/{layout-module-win}/g, "'#layout-module-win'");
  63. $('#'+tag).append(html);
  64. } else {
  65. Prompt(result.msg);
  66. }
  67. },
  68. error:function()
  69. {
  70. Prompt('网络异常错误');
  71. }
  72. });
  73. } else {
  74. Prompt('模块配置参数有误');
  75. }
  76. });
  77. // 模块-编辑
  78. $(document).on('click', '.layout-submit-edit', function()
  79. {
  80. $layout_module.attr('data-tag', $(this).data('tag'));
  81. $layout_module.find('input[name="id"]').val($(this).data('id'));
  82. $layout_module.find('input[name="layout_value"]').val($(this).data('value'));
  83. FormDataFill(eval('('+$(this).attr('data-json')+')'), '#layout-module-win');
  84. });
  85. // [布局-模块]-删除
  86. $(document).on('click', '.layout-submit-delete', function()
  87. {
  88. // ajax数据库删除
  89. var id = $(this).data('id');
  90. var type = $(this).data('type');
  91. if(id != undefined && type != undefined)
  92. {
  93. var $this = $(this);
  94. $.ajax({
  95. url:$layout_content.data(type+'-delete-url'),
  96. type:'POST',
  97. dataType:"json",
  98. timeout:10000,
  99. data:{"id":id},
  100. success:function(result)
  101. {
  102. if(result.code == 0)
  103. {
  104. // 移除元素
  105. $this.parent().parent().remove();
  106. Prompt(result.msg, 'success');
  107. } else {
  108. Prompt(result.msg);
  109. }
  110. },
  111. error:function()
  112. {
  113. Prompt('网络异常错误');
  114. }
  115. });
  116. } else {
  117. Prompt('布局配置参数有误');
  118. }
  119. });
  120. // 布局-添加
  121. $('.layout-list').on('click', 'button', function()
  122. {
  123. var url = $layout_content.data('layout-save-url');
  124. var type = $layout_content.data('type');
  125. var value = $(this).data('value');
  126. var html = $(this).data('html');
  127. var html_item = $(this).data('html-item');
  128. if(url != undefined && type != undefined && value != undefined && html != undefined)
  129. {
  130. // ajax请求
  131. $.ajax({
  132. url:url,
  133. type:'POST',
  134. dataType:"json",
  135. timeout:10000,
  136. data:{"type":type, "value":value},
  137. success:function(result)
  138. {
  139. if(result.code == 0)
  140. {
  141. // 数据替换-添加
  142. html = html.replace(/{data-layout-id}/g, result.data.layout_id)
  143. html = html.replace(/{data-value}/g, value);
  144. html = html.replace(/{layout-module-win}/g, "'#layout-module-win'");
  145. // 布局是否带模块
  146. if(result.data.module_count > 0)
  147. {
  148. for(var i=1; i<=result.data.module_count; i++)
  149. {
  150. var temp_field = 'module'+i+'_id';
  151. if(result.data[temp_field] != undefined)
  152. {
  153. html = html.replace(new RegExp('{data-module'+i+'-id}','g'), result.data[temp_field]);
  154. html = html.replace(new RegExp('{content-module'+i+'-id}','g'), 'layout-content-'+value+'-'+result.data[temp_field]);
  155. }
  156. }
  157. } else {
  158. html = html.replace(/{data-module-id}/g, result.data.layout_id);
  159. html = html.replace(/{content-module-id}/g, 'layout-content-'+value+'-'+result.data.layout_id);
  160. }
  161. // 是否存在子html
  162. if(html_item != undefined)
  163. {
  164. html = html.replace('{data-html-item}', "data-html='"+html_item+"'");
  165. }
  166. // 添加到页面中
  167. $layout_content.prepend(html);
  168. // 布局开关操作初始化
  169. LayoutSwitchInit();
  170. // 动画处理
  171. setTimeout(function()
  172. {
  173. var $first_child = $(".layout-content").children("div:first-child");
  174. $first_child.css('opacity', 0);
  175. $first_child.animate({opacity:1}, 500);
  176. }, 1);
  177. Prompt(result.msg, 'success');
  178. } else {
  179. Prompt(result.msg);
  180. }
  181. },
  182. error:function()
  183. {
  184. Prompt('网络异常错误');
  185. }
  186. });
  187. } else {
  188. Prompt('布局配置参数有误');
  189. }
  190. });
  191. // 数据导入form初始化
  192. FromInit('form.form-validation-layout-import');
  193. // 模板上传选择名称展示
  194. $('#layout-import-win input[name="file"]').on('change', function()
  195. {
  196. var fileNames = '';
  197. $.each(this.files, function()
  198. {
  199. fileNames += '<span class="am-badge">' + this.name + '</span> ';
  200. });
  201. $('#form-file-tips').html(fileNames);
  202. });
  203. });
  204. /**
  205. * [LayoutSwitchInit 布局开关插件初始化]
  206. * @author Devil
  207. * @blog http://gong.gg/
  208. * @version 0.0.1
  209. * @datetime 2017-02-21T15:25:12+0800
  210. */
  211. function LayoutSwitchInit()
  212. {
  213. $('[name="switch-checkbox"]').bootstrapSwitch(
  214. {
  215. onSwitchChange: function(event, state)
  216. {
  217. var $this = $(this);
  218. var state_text = (state == true) ? $(this).data('on-text') : $(this).data('off-text');
  219. state = (state == true) ? 1 : 0;
  220. $.ajax({
  221. url:$layout_content.data('layout-state-url'),
  222. type:'POST',
  223. dataType:"json",
  224. timeout:10000,
  225. data:{"id":$(this).data('id'), "state":state},
  226. success:function(result)
  227. {
  228. if(result.code == 0)
  229. {
  230. if(state == 1)
  231. {
  232. $this.parents('.layout-view').removeClass('layout-view-hidden');
  233. } else {
  234. $this.parents('.layout-view').addClass('layout-view-hidden');
  235. }
  236. Prompt('[ '+state_text+' ] '+result.msg, 'success');
  237. } else {
  238. Prompt('[ '+state_text+' ] '+result.msg);
  239. }
  240. },
  241. error:function()
  242. {
  243. Prompt('网络异常错误');
  244. }
  245. });
  246. }
  247. });
  248. }
  249. LayoutSwitchInit();