procuremensms.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var SMS_EDIT_AREA = ['960px', '580px'];
  3. var Controller = {
  4. index: function () {
  5. Table.api.init({
  6. extend: {
  7. index_url: 'procuremensms/index' + location.search,
  8. edit_url: 'procuremensms/edit',
  9. table: 'purchase_sms_template',
  10. }
  11. });
  12. var table = $("#table");
  13. var colCenter = {align: 'center', halign: 'center', valign: 'middle'};
  14. var columns = [
  15. {checkbox: true, align: 'center', halign: 'center', valign: 'middle', width: 42},
  16. $.extend({field: 'id', title: '序号', width: 60, operate: false}, colCenter),
  17. $.extend({field: 'title', title: '模版名称', operate: 'LIKE', width: 160}, colCenter),
  18. $.extend({
  19. field: 'content',
  20. title: '模版正文',
  21. operate: 'LIKE',
  22. width: 420,
  23. align: 'left',
  24. halign: 'center',
  25. valign: 'middle',
  26. class: 'col-sms-content',
  27. formatter: function (value) {
  28. if (value == null || value === '') {
  29. return '';
  30. }
  31. return '<div class="sms-content-cell">' + $('<div/>').text(String(value)).html() + '</div>';
  32. }
  33. }, {}),
  34. $.extend({
  35. field: 'status',
  36. title: __('Status'),
  37. width: 90,
  38. operate: '=',
  39. searchList: {'1': '正常', '0': '禁用'},
  40. formatter: function (value) {
  41. var v = String(value);
  42. if (v === '1' || v === 'normal') {
  43. return '<span class="text-success"><i class="fa fa-circle"></i> 正常</span>';
  44. }
  45. return '<span class="text-muted"><i class="fa fa-circle"></i> 禁用</span>';
  46. }
  47. }, colCenter),
  48. $.extend({
  49. field: 'updatetime',
  50. title: __('Updatetime'),
  51. width: 170,
  52. operate: 'RANGE',
  53. addclass: 'datetimerange',
  54. autocomplete: false,
  55. formatter: function (value) {
  56. if (value == null || value === '') {
  57. return '';
  58. }
  59. var s = String(value);
  60. if (/^\d{4}-\d{2}-\d{2}/.test(s)) {
  61. return s.length >= 19 ? s.substr(0, 19) : s;
  62. }
  63. return Table.api.formatter.datetime.call(this, value);
  64. }
  65. }, colCenter)
  66. ];
  67. function smsGetColWidths() {
  68. var widths = [];
  69. $.each(columns, function (_, col) {
  70. if (!col || col.visible === false) {
  71. return;
  72. }
  73. var w = parseInt(col.width, 10);
  74. widths.push((!w || isNaN(w)) ? (col.checkbox ? 42 : 120) : w);
  75. });
  76. return widths;
  77. }
  78. function smsApplyColgroup($tbl, widths) {
  79. if (!$tbl || !$tbl.length || !widths.length) {
  80. return;
  81. }
  82. var total = 0;
  83. var i;
  84. for (i = 0; i < widths.length; i++) {
  85. total += widths[i];
  86. }
  87. $tbl.css({
  88. tableLayout: 'fixed',
  89. width: total + 'px',
  90. minWidth: '100%'
  91. });
  92. var $cg = $tbl.children('colgroup');
  93. if (!$cg.length) {
  94. $cg = $('<colgroup/>').prependTo($tbl);
  95. }
  96. $cg.empty();
  97. for (i = 0; i < widths.length; i++) {
  98. $cg.append($('<col/>').attr('style', 'width:' + widths[i] + 'px;min-width:' + widths[i] + 'px;'));
  99. }
  100. $tbl.find('thead tr:first > th').each(function (idx) {
  101. if (idx >= widths.length) {
  102. return;
  103. }
  104. var w = widths[idx];
  105. $(this).css({width: w + 'px', minWidth: w + 'px', maxWidth: w + 'px'});
  106. $(this).find('.fht-cell').css({width: w + 'px', minWidth: w + 'px', maxWidth: w + 'px'});
  107. });
  108. $tbl.find('tbody tr:first-child:not(.no-records-found) > *').each(function (idx) {
  109. if (idx >= widths.length) {
  110. return;
  111. }
  112. var w = widths[idx];
  113. $(this).css({width: w + 'px', minWidth: w + 'px', maxWidth: w + 'px'});
  114. });
  115. }
  116. function smsSyncHeaderBodyAlign() {
  117. var $bt = table.closest('.bootstrap-table');
  118. if (!$bt.length) {
  119. return;
  120. }
  121. var widths = smsGetColWidths();
  122. if (!widths.length) {
  123. return;
  124. }
  125. var $headerWrap = $bt.find('> .fixed-table-container > .fixed-table-header').first();
  126. var $bodyWrap = $bt.find('> .fixed-table-container > .fixed-table-body').first();
  127. var $headerTable = $headerWrap.length ? $headerWrap.children('table').first() : $();
  128. var $bodyTable = $bodyWrap.length ? $bodyWrap.children('table').first() : table;
  129. if (!$bodyTable.length) {
  130. $bodyTable = $bt.find('.fixed-table-body > table').first();
  131. }
  132. if ($headerTable.length && $bodyTable.length && $headerTable[0] !== $bodyTable[0]) {
  133. var bodyEl = $bodyWrap[0];
  134. var sb = bodyEl ? Math.max(0, bodyEl.offsetWidth - bodyEl.clientWidth) : 0;
  135. $headerWrap.css('margin-right', sb > 0 ? sb + 'px' : '');
  136. smsApplyColgroup($headerTable, widths);
  137. smsApplyColgroup($bodyTable, widths);
  138. $headerWrap.scrollLeft($bodyWrap.scrollLeft() || 0);
  139. } else if ($bodyTable.length) {
  140. smsApplyColgroup($bodyTable, widths);
  141. }
  142. $bt.find('thead th .th-inner').css({
  143. textAlign: 'center',
  144. justifyContent: 'center'
  145. });
  146. }
  147. table.bootstrapTable({
  148. url: $.fn.bootstrapTable.defaults.extend.index_url,
  149. pk: 'id',
  150. sortName: 'id',
  151. sortOrder: 'desc',
  152. singleSelect: true,
  153. clickToSelect: true,
  154. commonSearch: true,
  155. showToggle: false,
  156. showColumns: false,
  157. escape: false,
  158. columns: [columns]
  159. });
  160. Table.api.bindevent(table);
  161. table.on('post-header.bs.table post-body.bs.table load-success.bs.table refresh.bs.table', function () {
  162. setTimeout(smsSyncHeaderBodyAlign, 0);
  163. setTimeout(smsSyncHeaderBodyAlign, 80);
  164. });
  165. table.on('check.bs.table', function (e, row) {
  166. var sel = table.bootstrapTable('getSelections') || [];
  167. if (sel.length <= 1) {
  168. return;
  169. }
  170. $.each(sel, function (i, r) {
  171. if (r && row && r.id !== row.id) {
  172. table.bootstrapTable('uncheckBy', {field: 'id', values: [r.id]});
  173. }
  174. });
  175. });
  176. table.on('post-body.bs.table', function () {
  177. table.closest('.bootstrap-table').find('.btn-editone').each(function () {
  178. $(this).data('area', SMS_EDIT_AREA);
  179. });
  180. });
  181. $('#toolbar .btn-edit').data('area', SMS_EDIT_AREA);
  182. $(window).off('resize.procuremenSms').on('resize.procuremenSms', function () {
  183. setTimeout(smsSyncHeaderBodyAlign, 50);
  184. });
  185. },
  186. edit: function () {
  187. Controller.api.bindevent();
  188. $(document).off('click.procuremenSmsClose', '.btn-procuremen-sms-close').on('click.procuremenSmsClose', '.btn-procuremen-sms-close', function () {
  189. var index = parent.Layer.getFrameIndex(window.name);
  190. parent.Layer.close(index);
  191. });
  192. $(document).off('click.smsVarInsert', '.sms-var-tag').on('click.smsVarInsert', '.sms-var-tag', function () {
  193. var tag = $(this).data('tag') || $(this).text();
  194. tag = String(tag || '').trim();
  195. if (!tag) {
  196. return;
  197. }
  198. var $ta = $('#sms-template-content');
  199. if (!$ta.length) {
  200. return;
  201. }
  202. var el = $ta[0];
  203. var start = el.selectionStart;
  204. var end = el.selectionEnd;
  205. var val = $ta.val();
  206. if (typeof start === 'number' && typeof end === 'number') {
  207. $ta.val(val.substring(0, start) + tag + val.substring(end));
  208. var pos = start + tag.length;
  209. el.setSelectionRange(pos, pos);
  210. } else {
  211. $ta.val(val + tag);
  212. }
  213. $ta.focus();
  214. });
  215. },
  216. api: {
  217. bindevent: function () {
  218. Form.api.bindevent($("form[role=form]"));
  219. }
  220. }
  221. };
  222. return Controller;
  223. });