labelGeneration.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. layui.define(['form'], function (exports) {
  2. var form = layui.form,
  3. $ = layui.jquery,
  4. index = 0,
  5. oldId,
  6. MOD_NAME = 'labelGeneration',
  7. formField = {
  8. label: {
  9. id: '-1',
  10. tag: "label",
  11. },
  12. },
  13. labelGeneration = {
  14. set: function (options) {
  15. var that = this;
  16. that.config = $.extend({}
  17. , that.config
  18. , options);
  19. return that;
  20. }
  21. //事件监听
  22. , on: function (events
  23. , callback) {
  24. return layui.onevent.call(this
  25. , MOD_NAME
  26. , events
  27. , callback);
  28. }
  29. },
  30. Class = function (options) {
  31. var that = this;
  32. that.config = $.extend({}
  33. , that.config
  34. , labelGeneration.config
  35. , options);
  36. that.render();
  37. },
  38. thisIns = function () {
  39. var that = this
  40. , options = that.config;
  41. return {
  42. reload: function (options) {
  43. that.reload.call(that
  44. , options);
  45. }, getOptions: function () {
  46. return options || null;
  47. }, getData: function () {
  48. return options.data || null;
  49. }
  50. }
  51. }
  52. Class.prototype.config = {
  53. version: "1.0.0"
  54. , Author: "谁家没一个小强"
  55. , generateId: 0
  56. , data: []
  57. , isEnter: false
  58. };
  59. /* 自动生成ID 当前页面自动排序*/
  60. Class.prototype.autoId = function (tag) {
  61. var that = this,
  62. options = that.config;
  63. options.generateId = options.generateId + 1;
  64. return tag + '_' + options.generateId;
  65. }
  66. Class.prototype.components = {
  67. label: {
  68. render: function (json,options) {
  69. var _html = '<blockquote class="layui-elem-quote">';
  70. _html += '<div class="layui-form layui-form-pane layui-form-item">';
  71. _html += '<label class="layui-form-label">输入手机号</label>';
  72. _html += '<div class="layui-input-inline">';
  73. if (options.isEnter) {
  74. _html += '<input type="text" id="{0}" lay-verify="required" placeholder="按回车生成手机号" autocomplete="off" class="layui-input">'
  75. .format(json.id);
  76. } else {
  77. _html += '<input type="text" id="{0}" lay-verify="required" placeholder="通过按钮生成标签" autocomplete="off" class="layui-input">'
  78. .format(json.id);
  79. }
  80. _html += '</div>';
  81. if (!options.isEnter) {
  82. _html += '<button type="button" id="{0}-button" class="layui-btn layui-btn-normal">确定</button>'.format(json.id);
  83. }
  84. // _html += '<label class="layui-form-label">颜色选择</label>';
  85. // _html += '<div class="layui-input-inline">';
  86. // _html += '<select lay-filter="{0}-switchTest">'.format(json.id);
  87. // _html += '<option value="" selected>墨绿色</option>';
  88. // _html += '<option value="layui-btn-primary">原始色</option>';
  89. // _html += '<option value="layui-btn-normal">天蓝色</option>';
  90. // _html += '<option value="layui-btn-warm">暖黄色</option>';
  91. // _html += '<option value="layui-btn-danger">红色</option>';
  92. // _html += '</select>';
  93. // _html += '</div>';
  94. _html += '</div>';
  95. _html += '<div id="{0}-content"></div>'.format(json.id);
  96. _html += '</blockquote>';
  97. return _html;
  98. },
  99. update: function (json) {
  100. },
  101. /* 获取对象 */
  102. jsonData: function (id, that) {
  103. //分配一个新的ID
  104. var _json = JSON.parse(JSON.stringify(formField.label));
  105. _json.id = id == undefined ? that.autoId(_json.tag) : id;
  106. that.checkId(_json,that);
  107. return _json;
  108. }
  109. }
  110. };
  111. /* 判定id是否重复*/
  112. Class.prototype.checkId = function (json,that) {
  113. if ($("#" + json.id + "-content").length != 0) {
  114. json.id = that.autoId(json.tag);
  115. that.checkId(json);
  116. } else {
  117. return;
  118. }
  119. }
  120. // Class.prototype.bindGridSortEvent = function (json) {
  121. // var that = this
  122. // , options = that.config;
  123. // var formItemSort = Sortable.create(document.getElementById(json.id + "-content"), {
  124. // group: {
  125. // name: 'group' + json.id
  126. // },
  127. // animation: 1000,
  128. // onEnd: function (evt) {
  129. // var _values = $("#" + json.id + "-content").find("div");
  130. // var ops = [];
  131. // for (var i = 0; i < _values.length; i++) {
  132. // ops.push({"ngColor": $(_values[i]).attr("ng-color"), "value": $(_values[i]).text()});
  133. // }
  134. // options.data = ops;
  135. // }
  136. // });
  137. // }
  138. /* 绑定事件*/
  139. Class.prototype.deleteValue = function (value, ngValue) {
  140. var that = this
  141. , options = that.config;
  142. for (var i = 0; i < options.data.length; i++) {
  143. if (options.data[i].value === value && options.data[i].ngColor === ngValue) {
  144. options.data.splice(i, 1);
  145. break;
  146. }
  147. }
  148. }
  149. /* 绑定事件*/
  150. Class.prototype.bindPropertyEvent = function (_json) {
  151. var that = this
  152. , options = that.config;
  153. var colorClass = "";
  154. if (options.isEnter) {
  155. $("#" + _json.id).keypress(function (event) {
  156. if (event.which === 13) {
  157. var _value = $(this).val();
  158. if (!_value) {
  159. layer.msg('请输入手机号!')
  160. return;
  161. }
  162. if (!/^1[3456789]\d{9}$/.test(_value)) {
  163. layer.msg('手机号格式错误!')
  164. return;
  165. }
  166. let arr = $("#" + _json.id + "-content .layui-btn")
  167. for (let i = 0; i < arr.length; i++) {
  168. if ($(arr[i]).text().trim() == _value) {
  169. layer.msg('输入手机号重复!')
  170. return
  171. }
  172. }
  173. index = index + 1;
  174. var _html = '<div class="layui-btn {0} none-transition" id="{2}" ng-index="{3}" ng-color="{0}">{1}<i class="layui-icon layui-icon-close"></i></div>'
  175. .format(colorClass, _value, _json.id + index, index);
  176. $("#" + _json.id + "-content").append(_html);
  177. $("#" + _json.id).val('');
  178. options.data.push({"ngColor": colorClass, "value": _value});
  179. $("#" + _json.id + index + " .layui-icon-close").click(function () {
  180. that.deleteValue($(this).parent().text(), $(this).parent().attr("ng-color"));
  181. $(this).parent().remove();
  182. });
  183. }
  184. });
  185. } else {
  186. $("#" + _json.id + "-button").click(function (event) {
  187. var _value = $("#" + _json.id).val();
  188. index = index + 1;
  189. var _html = '<div class="layui-btn {0} none-transition" id="{2}" ng-index="{3}" ng-color="{0}">{1}<i class="layui-icon layui-icon-close"></i></div>'
  190. .format(colorClass, _value, _json.id + index, index);
  191. $("#" + _json.id + "-content").append(_html);
  192. options.data.push({"ngColor": colorClass, "value": _value});
  193. $("#" + _json.id + index + " .layui-icon-close").click(function () {
  194. that.deleteValue($(this).parent().text(), $(this).parent().attr("ng-color"));
  195. $(this).parent().remove();
  196. });
  197. });
  198. }
  199. form.on('select(' + _json.id + '-switchTest)', function (data) {
  200. colorClass = data.value;
  201. });
  202. for (var i = 0; i < options.data.length; i++) {
  203. index = index + 1;
  204. var _html = '<div class="layui-btn {0} none-transition" id="{2}" ng-index="{3}" ng-color="{0}">{1}<i class="layui-icon layui-icon-close"></i></div>'
  205. .format(options.data[i].ngColor, options.data[i].value, _json.id + index, index);
  206. $("#" + _json.id + "-content").append(_html);
  207. $("#" + _json.id + index + " .layui-icon-close").click(function () {
  208. that.deleteValue($(this).parent().text(), $(this).parent().attr("ng-color"));
  209. $(this).parent().remove();
  210. });
  211. }
  212. }
  213. /* 渲染组件 */
  214. Class.prototype.renderComponents = function () {
  215. var that = this
  216. , options = that.config;
  217. var elem = $(options.elem);
  218. elem.empty();
  219. var jsonData = that.components['label'].jsonData(undefined, that);
  220. elem.append(that.components['label'].render(jsonData,options));
  221. that.bindPropertyEvent(jsonData);
  222. // that.bindGridSortEvent(jsonData);
  223. form.render();
  224. }
  225. Class.prototype.reload = function (options) {
  226. var that = this;
  227. options = options || {};//如果是空的话,就赋值 {}
  228. that.config = $.extend({}
  229. , that.config
  230. , labelGeneration.config
  231. , options);
  232. that.render();
  233. }
  234. //核心入口 初始化一个 regionSelect 类
  235. labelGeneration.render = function (options) {
  236. var ins = new Class(options);
  237. return thisIns.call(ins);
  238. }
  239. /**
  240. * 渲染组件
  241. */
  242. Class.prototype.render = function (options) {
  243. var that = this
  244. , options = that.config;
  245. that.renderComponents();
  246. }
  247. String.prototype.format = function (args) {
  248. var result = this;
  249. if (arguments.length > 0) {
  250. if (arguments.length == 1 && typeof (args) == "object") {
  251. for (var key in args) {
  252. if (args[key] != undefined) {
  253. var reg = new RegExp("({" + key + "})"
  254. , "g");
  255. result = result.replace(reg
  256. , args[key]);
  257. }
  258. }
  259. } else {
  260. for (var i = 0; i < arguments.length; i++) {
  261. if (arguments[i] != undefined) {
  262. var reg = new RegExp("({[" + i + "]})"
  263. , "g");
  264. result = result.replace(reg
  265. , arguments[i]);
  266. }
  267. }
  268. }
  269. }
  270. return result;
  271. }
  272. exports(MOD_NAME, labelGeneration);
  273. });