Helpers.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import debounce from './Debounce'
  2. export default class Helpers {
  3. constructor(Dcat) {
  4. Dcat.helpers = this;
  5. this.dcat = Dcat;
  6. // 延迟触发,消除重复触发
  7. this.debounce = debounce;
  8. }
  9. /**
  10. * 获取json对象或数组的长度
  11. *
  12. * @param obj
  13. * @returns {number}
  14. */
  15. len(obj) {
  16. if (typeof obj !== 'object') {
  17. return 0;
  18. }
  19. let i, len = 0;
  20. for(i in obj) {
  21. len += 1;
  22. }
  23. return len;
  24. }
  25. /**
  26. * 判断变量或key是否存在
  27. *
  28. * @param _var
  29. * @param key
  30. * @returns {boolean}
  31. */
  32. isset(_var, key) {
  33. let isset = (typeof _var !== 'undefined' && _var !== null);
  34. if (typeof key === 'undefined') {
  35. return isset;
  36. }
  37. return isset && typeof _var[key] !== 'undefined';
  38. };
  39. empty(obj, key) {
  40. return !(this.isset(obj, key) && obj[key]);
  41. };
  42. /**
  43. * 根据key获取对象的值,支持获取多维数据
  44. *
  45. * @param arr
  46. * @param key
  47. * @param def
  48. * @returns {null|*}
  49. */
  50. get(arr, key, def) {
  51. def = null;
  52. if (this.len(arr) < 1) {
  53. return def;
  54. }
  55. key = String(key).split('.');
  56. for (var i = 0; i < key.length; i++) {
  57. if (this.isset(arr, key[i])) {
  58. arr = arr[key[i]];
  59. } else {
  60. return def;
  61. }
  62. }
  63. return arr;
  64. }
  65. /**
  66. * 判断key是否存在
  67. *
  68. * @param arr
  69. * @param key
  70. * @returns {def|boolean}
  71. */
  72. has(arr, key) {
  73. if (this.len(arr) < 1) return def;
  74. key = String(key).split('.');
  75. for (var i = 0; i < key.length; i++) {
  76. if (this.isset(arr, key[i])) {
  77. arr = arr[key[i]];
  78. } else {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. /**
  85. * 判断元素是否在对象中存在
  86. *
  87. * @param arr
  88. * @param val
  89. * @param strict
  90. * @returns {boolean}
  91. */
  92. inObject(arr, val, strict) {
  93. if (this.len(arr) < 1) {
  94. return false;
  95. }
  96. for (var i in arr) {
  97. if (strict) {
  98. if (val === arr[i]) {
  99. return true;
  100. }
  101. continue
  102. }
  103. if (val == arr[i]) {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. // 判断对象是否相等
  110. equal(array, array2, strict) {
  111. if (!array || !array2) {
  112. return false;
  113. }
  114. let len1 = this.len(array),
  115. len2 = this.len(array2), i;
  116. if (len1 !== len2) {
  117. return false;
  118. }
  119. for (i in array) {
  120. if (! this.isset(array2, i)) {
  121. return false;
  122. }
  123. if (array[i] === null && array2[i] === null) {
  124. return true;
  125. }
  126. if (typeof array[i] === 'object' && typeof array2[i] === 'object') {
  127. if (! this.equal(array[i], array2[i], strict)) {
  128. return false;
  129. }
  130. continue;
  131. }
  132. if (strict) {
  133. if (array[i] !== array2[i]) {
  134. return false;
  135. }
  136. } else {
  137. if (array[i] != array2[i]) {
  138. return false;
  139. }
  140. }
  141. }
  142. return true;
  143. }
  144. // 字符串替换
  145. replace(str, replace, subject) {
  146. if (!str) {
  147. return str;
  148. }
  149. return str.replace(
  150. new RegExp(replace, "g"),
  151. subject
  152. );
  153. }
  154. /**
  155. * 生成随机字符串
  156. *
  157. * @returns {string}
  158. */
  159. random(len) {
  160. return Math.random().toString(12).substr(2, len || 16)
  161. }
  162. // 预览图片
  163. previewImage(src, width, title) {
  164. let Dcat = this.dcat,
  165. img = new Image(),
  166. win = this.isset(window.top) ? top : window,
  167. clientWidth = Math.ceil(win.screen.width * 0.6),
  168. clientHeight = Math.ceil(win.screen.height * 0.8);
  169. img.style.display = 'none';
  170. img.style.height = 'auto';
  171. img.style.width = width || '100%';
  172. img.src = src;
  173. document.body.appendChild(img);
  174. Dcat.loading();
  175. img.onload = function () {
  176. Dcat.loading(false);
  177. let srcw = this.width,
  178. srch = this.height,
  179. width = srcw > clientWidth ? clientWidth : srcw,
  180. height = Math.ceil(width * (srch/srcw));
  181. height = height > clientHeight ? clientHeight : height;
  182. title = title || src.split('/').pop();
  183. if (title.length > 50) {
  184. title = title.substr(0, 50) + '...';
  185. }
  186. win.layer.open({
  187. type: 1,
  188. shade: 0.2,
  189. title: false,
  190. maxmin: false,
  191. shadeClose: true,
  192. closeBtn: 2,
  193. content: $(img),
  194. area: [width+'px', (height) + 'px'],
  195. skin: 'layui-layer-nobg',
  196. end: function () {
  197. document.body.removeChild(img);
  198. }
  199. });
  200. };
  201. img.onerror = function () {
  202. Dcat.loading(false);
  203. Dcat.error(Dcat.lang.trans('no_preview'))
  204. };
  205. }
  206. // 异步加载
  207. asyncRender(url, done, error) {
  208. let Dcat = this.dcat;
  209. $.ajax(url).then(function (data) {
  210. done(
  211. Dcat.assets.resolveHtml(data, Dcat.triggerReady).render()
  212. );
  213. }, function (a, b, c) {
  214. if (error) {
  215. if (error(a, b, c) === false) {
  216. return false;
  217. }
  218. }
  219. Dcat.handleAjaxError(a, b, c);
  220. })
  221. }
  222. /**
  223. * 联动多个字段.
  224. *
  225. * @param _this
  226. * @param options
  227. */
  228. loadFields(_this, options) {
  229. let refreshOptions = function(url, target) {
  230. Dcat.loading();
  231. $.ajax(url).then(function(data) {
  232. Dcat.loading(false);
  233. target.find("option").remove();
  234. $.map(data, function (d) {
  235. target.append(new Option(d[options.textField], d[options.idField], false, false));
  236. });
  237. $(target).val(String(target.data('value')).split(',')).trigger('change');
  238. });
  239. };
  240. let promises = [],
  241. values = [];
  242. if (! options.values) {
  243. $(_this).find('option:selected').each(function () {
  244. if (String(this.value) === '0' || this.value) {
  245. values.push(this.value)
  246. }
  247. });
  248. } else {
  249. values = options.values;
  250. if (typeof values === 'string') {
  251. values = [values];
  252. }
  253. }
  254. if (! values.length) {
  255. return;
  256. }
  257. options.fields.forEach(function(field, index){
  258. var target = $(_this).closest(options.group).find('.' + options.fields[index]);
  259. if (! values.length) {
  260. return;
  261. }
  262. promises.push(refreshOptions(options.urls[index] + (options.urls[index].match(/\?/)?'&':'?') + "q="+ values.join(','), target));
  263. });
  264. $.when(promises).then(function() {});
  265. }
  266. }