yvanui.dict.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * power
  3. * @author luoyifan
  4. * 2018-11-30 22:45:00
  5. */
  6. (function ($) {
  7. "use strict";
  8. var DICT_CACHE = {};
  9. $.extend($.yvan, {
  10. //一次性载入所有字典
  11. dictLoadAll: function (option) {
  12. $.yvan.ajax($.extend({}, option, {
  13. success: function (data) {
  14. //清空缓存
  15. DICT_CACHE = {};
  16. for (var name in data.data) {
  17. DICT_CACHE[name] = new Dict(data.data[name]);
  18. }
  19. // console.log('仓库字典载入完成, 总共' + Object.getOwnPropertyNames(DICT_CACHE).length + "项");
  20. if ($.type(option.success) === 'function') {
  21. option.success.apply(this, arguments);
  22. }
  23. }
  24. }));
  25. },
  26. dictLoadDefine: function (loadFunction) {
  27. return function (param) {
  28. var dict;
  29. if ($.type(param) === 'string') {
  30. dict = [param];
  31. } else if ($.type(param) === 'array') {
  32. dict = param;
  33. } else {
  34. console.error('错误的调用方式' + param);
  35. return;
  36. }
  37. //筛选出本地没有缓存的 dictName
  38. var reqLength = 0;
  39. var requireDict = {};
  40. for (var i = 0; i < dict.length; i++) {
  41. var dictName = $.trim(dict[i]);
  42. if (!dictName) continue;
  43. if (!DICT_CACHE.hasOwnProperty(dictName)) {
  44. //requireDict [dictName] = new Date().getTime();
  45. //reqLength++;
  46. //不允许再后期按需加载
  47. return new Dict([]);
  48. }
  49. }
  50. /*
  51. //阻塞式调用
  52. if (reqLength > 0) {
  53. var serverData;
  54. $.yvan.ajax($.extend({
  55. async: false,
  56. success: function (data) {
  57. serverData = data.data;
  58. }
  59. }, loadFunction.call(this, requireDict)));
  60. }
  61. //缓存起来
  62. for (var name in serverData) {
  63. if (!serverData.hasOwnProperty(name)) continue;
  64. DICT_CACHE[name] = new Dict(serverData[name]);
  65. }
  66. */
  67. if ($.type(param) === 'string') {
  68. //字典名换字典
  69. return DICT_CACHE[param];
  70. } else if ($.type(param) === 'array') {
  71. //字典名数组 换 字典数组
  72. return param.map(function (dict) {
  73. return DICT_CACHE[dict];
  74. });
  75. }
  76. };
  77. }
  78. });
  79. })(jQuery);
  80. function Dict(idValues) {
  81. this.data = idValues;
  82. }
  83. Dict.prototype.fmt = function () {
  84. var me = this;
  85. return function (id) {
  86. for (var i = 0; i < me.data.length; i++) {
  87. if (id == me.data[i].id) {
  88. return me.data[i].text;
  89. }
  90. }
  91. return "";
  92. };
  93. };
  94. Dict.prototype.combo = function (opts) {
  95. var me = this;
  96. //方法回调
  97. if ($.type(opts) === 'function') {
  98. var r = opts(me.data);
  99. //有返回值
  100. if (r) return r;
  101. } else if ($.type(opts) === 'array') {
  102. //添加选项
  103. me.data.concat(opts);
  104. } else if ($.type(opts) === "object") {
  105. me.data.push(opts);
  106. }
  107. return me.data;
  108. };
  109. Dict.prototype.combowithAll = function (opts) {
  110. var me = this;
  111. //方法回调
  112. if ($.type(opts) === 'function') {
  113. var r = opts(me.data);
  114. //有返回值
  115. if (r) return r;
  116. } else if ($.type(opts) === 'array') {
  117. //添加选项
  118. me.data.concat(opts);
  119. } else if ($.type(opts) === "object") {
  120. me.data.push(opts);
  121. }
  122. var arr = [{"id": '', "text": '全部'}];
  123. for (var i = 0; i < me.data.length; i++) {
  124. arr.push(me.data[i]);
  125. }
  126. return arr;
  127. };
  128. Dict.prototype.combowith = function (opts) {
  129. var me = this;
  130. //方法回调
  131. if ($.type(opts) === 'function') {
  132. var r = opts(me.data);
  133. //有返回值
  134. if (r) return r;
  135. } else if ($.type(opts) === 'array') {
  136. //添加选项
  137. me.data.concat(opts);
  138. } else if ($.type(opts) === "object") {
  139. me.data.push(opts);
  140. }
  141. var arr = [/*{"id": '', "text": '全部'}*/];
  142. for (var i = 0; i < me.data.length; i++) {
  143. arr.push(me.data[i]);
  144. }
  145. return arr;
  146. };
  147. Dict.prototype.combowithAllSelect = function (opts) {
  148. var me = this;
  149. //方法回调
  150. if ($.type(opts) === 'function') {
  151. var r = opts(me.data);
  152. //有返回值
  153. if (r) return r;
  154. } else if ($.type(opts) === 'array') {
  155. //添加选项
  156. me.data.concat(opts);
  157. } else if ($.type(opts) === "object") {
  158. me.data.push(opts);
  159. }
  160. var arr = [{"id": '0', "text": '请选择'}];
  161. for (var i = 0; i < me.data.length; i++) {
  162. arr.push(me.data[i]);
  163. }
  164. return arr;
  165. };
  166. Dict.prototype.tree = function (opts) {
  167. var me = this;
  168. //方法回调
  169. if ($.type(opts) === 'function') {
  170. var r = opts(me.data);
  171. //有返回值
  172. if (r) return r;
  173. } else if ($.type(opts) === 'array') {
  174. //添加选项
  175. me.data.concat(opts);
  176. } else if ($.type(opts) === "object") {
  177. me.data.push(opts);
  178. }
  179. return me.data;
  180. };