yvanui.yvselect.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * yvan.yvselect.js
  3. * @author luoyifan
  4. * 2019-07-22 19:21:00
  5. */
  6. 'use strict';
  7. (function ($) {
  8. function init(target) {
  9. var state = $.data(target, 'yvselect');
  10. var option = state.options;
  11. var $dom = $(target);
  12. var select2Option = {
  13. language: "zh-CN",
  14. placeholder: '请选择',
  15. allowClear: true,
  16. data: option.data,
  17. matcher: function (term, text) {
  18. term = term.toUpperCase();
  19. //支持拼音筛选
  20. if (pinyin) {
  21. var letter = pinyin.getCamelChars(text) + ' ' + text;
  22. return (letter.toUpperCase().indexOf(term) >= 0);
  23. } else {
  24. return (text.toUpperCase().indexOf(term) >= 0);
  25. }
  26. },
  27. };
  28. if (option.multiple === true) {
  29. $.extend(select2Option, {
  30. allowClear: true,
  31. multiple: true,
  32. tags: false,
  33. separator: option.joinChar,
  34. tokenSeparators: option.joinChar,
  35. minimumSelectionLength: select2Option.minTagLength,
  36. maximumSelectionLength: select2Option.maxTagLength
  37. });
  38. }
  39. $dom.textbox($.extend({}, option, {
  40. onChange: function () {
  41. },
  42. onValidate: function (validResult) {
  43. var option = $(this).textbox("options");
  44. var box = $(this);
  45. if (option.oldInputValue != undefined) {
  46. box.val(option.oldInputValue);
  47. option.oldInputValue = undefined;
  48. }
  49. var tb = box.next();
  50. if (validResult) {
  51. tb.removeClass("textbox-invalid");
  52. } else {
  53. tb.addClass("textbox-invalid");
  54. }
  55. //option.onValidate.call(target, validResult);
  56. }
  57. }));
  58. var $ctl = $dom.textbox('textbox');
  59. $ctl.select2(select2Option);
  60. //将 validatebox 属性,复制给 select2.container 控件下面
  61. var validateboxOpt = $ctl.data('validatebox');
  62. var select2Data = $ctl.data('select2');
  63. $.data(select2Data.container[0], 'validatebox', validateboxOpt);
  64. var ss = $ctl.data('select2');
  65. //validatebox 需要监听 focus / blur / mouseenter / mouseleave
  66. // 4个事件,见 jquery.validatebox.src.js 329行
  67. ss.container.on('mouseenter mouseleave', function (e) {
  68. var $target = $(e.target).closest('.select2-container');
  69. var state = $target.data('validatebox');
  70. var option = state.options;
  71. e.data = { target: $target[0] };
  72. switch (e.type) {
  73. case 'mouseenter':
  74. return option.events.mouseenter(e);
  75. case 'mouseleave':
  76. return option.events.mouseleave(e);
  77. }
  78. });
  79. //让 container 能获取焦点, 防止错过 validatebox 定位焦点的问题
  80. //同时需要修改 jquery.easyui.min.js 7175 行
  81. ss.container.attr('tabindex', 10);
  82. $ctl.on("select2-focus select2-blur", function (e) {
  83. var state = $(e.target).data('validatebox');
  84. var option = state.options;
  85. e.data = { target: e.target };
  86. switch (e.type) {
  87. case 'select2-focus':
  88. return option.events.focus(e);
  89. case 'select2-blur':
  90. return option.events.blur(e);
  91. }
  92. });
  93. $ctl.on("select2-selected selected select2-removed", function (e) {
  94. selectValueChange(this, e);
  95. });
  96. }
  97. function selectValueChange(target, e) {
  98. target = $(target).parent().prev()[0];
  99. var state = $.data(target, 'yvselect');
  100. var option = state.options;
  101. var $dom = $(target);
  102. var $ctl = $dom.textbox('textbox');
  103. var newValue = "";
  104. if (option.multiple === true) {
  105. newValue = $ctl.select2('val').join(option.joinChar);
  106. } else {
  107. if (e.type === 'select2-removed') {
  108. newValue = "";
  109. } else {
  110. newValue = e.val;
  111. }
  112. }
  113. $dom.textbox('setValue', newValue);
  114. if ($.type(e.type) === 'string' && $.type(option.onChange) === 'function') {
  115. option.onChange.call(target, newValue, e);
  116. }
  117. }
  118. $.fn.yvselect = function (options, param) {
  119. if (typeof options == 'string') {
  120. var method = $.fn.yvselect.methods[options];
  121. if (method) {
  122. var args = [this];
  123. for (var i = 1; i < arguments.length; i++) {
  124. args.push(arguments[i]);
  125. }
  126. return method.apply(this, args);
  127. } else {
  128. console.error('not found method:' + options);
  129. }
  130. return;
  131. }
  132. options = options || {};
  133. return this.each(function () {
  134. var state = $.data(this, 'yvselect');
  135. if (state) {
  136. $.extend(state.options, options);
  137. } else {
  138. var parseOptions = function (target) {
  139. var t = $(target);
  140. return $.extend({},
  141. $.fn.validatebox.parseOptions(target),
  142. $.fn.textbox.parseOptions(target),
  143. {
  144. value: (t.val() || undefined),
  145. type: (t.attr("type") ? t.attr("type") : undefined)
  146. }
  147. );
  148. };
  149. $.data(this, 'yvselect', {
  150. options: $.extend({}, $.fn.yvselect.defaults, parseOptions(this), options)
  151. });
  152. }
  153. init(this);
  154. });
  155. };
  156. $.fn.yvselect.methods = {
  157. getValue: function ($dom) {
  158. var $ctl = $dom.textbox('textbox');
  159. return $ctl.select2('val');
  160. },
  161. setValue: function ($dom, value) {
  162. var $ctl = $dom.textbox('textbox');
  163. return $ctl.select2('val', value);
  164. },
  165. getText: function ($dom) {
  166. var $ctl = $dom.textbox('textbox');
  167. var data = $ctl.select2('data');
  168. if(data) {
  169. return data.text
  170. }
  171. },
  172. open: function ($dom) {
  173. var $ctl = $dom.textbox('textbox');
  174. return $ctl.select2('open');
  175. },
  176. close: function ($dom) {
  177. var $ctl = $dom.textbox('textbox');
  178. return $ctl.select2('close');
  179. }
  180. };
  181. $.extend($.fn.validatebox.methods, {
  182. setValues: function ($dom, value) {
  183. return $dom.each(function () {
  184. var $me = $(this);
  185. if ($me.hasClass('select2-container')) {
  186. if ($.type(value) === 'array') {
  187. $me.select2('val', value);
  188. selectValueChange($me[0], {});
  189. } else if ($.type(value) === 'string') {
  190. $me.select2('val', value.split(','));
  191. selectValueChange($me[0], {});
  192. }
  193. }
  194. });
  195. }
  196. });
  197. $.fn.yvselect.defaults = {
  198. required: false,
  199. validType: null,
  200. validParams: null,
  201. delay: 200,
  202. interval: 200,
  203. deltaX: 0,
  204. deltaY: 0,
  205. novalidate: false,
  206. editable: true,
  207. disabled: false,
  208. readonly: false,
  209. validateOnCreate: true,
  210. validateOnBlur: false,
  211. multiple: false,
  212. joinChar: ",",
  213. minTagLength: 1,
  214. maxTagLength: 999,
  215. label: null,
  216. labelPosition: "before",
  217. labelAlign: 'right',
  218. width: 280,
  219. labelWidth: 120,
  220. prompt: "",
  221. multiline: false,
  222. type: "select",
  223. // missingMessage: "必填",
  224. missingMessage: "",
  225. invalidMessage: null,
  226. tipPosition: "right",
  227. val: function (target) {
  228. if (!$.data(target, 'select2')) {
  229. return "";
  230. }
  231. var value = $(target).select2('val');
  232. if (value === null) {
  233. return "";
  234. }
  235. return value;
  236. },
  237. onValidate: function (validResult) {
  238. }
  239. };
  240. })(jQuery);