123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- /**
- * power
- * @author luoyifan
- * 2018-11-30 22:45:00
- */
- (function ($) {
- "use strict";
- var DICT_CACHE = {};
- $.extend($.yvan, {
- //一次性载入所有字典
- dictLoadAll: function (option) {
- $.yvan.ajax($.extend({}, option, {
- success: function (data) {
- //清空缓存
- DICT_CACHE = {};
- for (var name in data.data) {
- DICT_CACHE[name] = new Dict(data.data[name]);
- }
- // console.log('仓库字典载入完成, 总共' + Object.getOwnPropertyNames(DICT_CACHE).length + "项");
- if ($.type(option.success) === 'function') {
- option.success.apply(this, arguments);
- }
- }
- }));
- },
- dictLoadDefine: function (loadFunction) {
- return function (param) {
- var dict;
- if ($.type(param) === 'string') {
- dict = [param];
- } else if ($.type(param) === 'array') {
- dict = param;
- } else {
- console.error('错误的调用方式' + param);
- return;
- }
- //筛选出本地没有缓存的 dictName
- var reqLength = 0;
- var requireDict = {};
- for (var i = 0; i < dict.length; i++) {
- var dictName = $.trim(dict[i]);
- if (!dictName) continue;
- if (!DICT_CACHE.hasOwnProperty(dictName)) {
- //requireDict [dictName] = new Date().getTime();
- //reqLength++;
- //不允许再后期按需加载
- return new Dict([]);
- }
- }
- /*
- //阻塞式调用
- if (reqLength > 0) {
- var serverData;
- $.yvan.ajax($.extend({
- async: false,
- success: function (data) {
- serverData = data.data;
- }
- }, loadFunction.call(this, requireDict)));
- }
- //缓存起来
- for (var name in serverData) {
- if (!serverData.hasOwnProperty(name)) continue;
- DICT_CACHE[name] = new Dict(serverData[name]);
- }
- */
- if ($.type(param) === 'string') {
- //字典名换字典
- return DICT_CACHE[param];
- } else if ($.type(param) === 'array') {
- //字典名数组 换 字典数组
- return param.map(function (dict) {
- return DICT_CACHE[dict];
- });
- }
- };
- }
- });
- })(jQuery);
- function Dict(idValues) {
- this.data = idValues;
- }
- Dict.prototype.fmt = function () {
- var me = this;
- return function (id) {
- for (var i = 0; i < me.data.length; i++) {
- if (id == me.data[i].id) {
- return me.data[i].text;
- }
- }
- return "";
- };
- };
- Dict.prototype.combo = function (opts) {
- var me = this;
- //方法回调
- if ($.type(opts) === 'function') {
- var r = opts(me.data);
- //有返回值
- if (r) return r;
- } else if ($.type(opts) === 'array') {
- //添加选项
- me.data.concat(opts);
- } else if ($.type(opts) === "object") {
- me.data.push(opts);
- }
- return me.data;
- };
- Dict.prototype.combowithAll = function (opts) {
- var me = this;
- //方法回调
- if ($.type(opts) === 'function') {
- var r = opts(me.data);
- //有返回值
- if (r) return r;
- } else if ($.type(opts) === 'array') {
- //添加选项
- me.data.concat(opts);
- } else if ($.type(opts) === "object") {
- me.data.push(opts);
- }
- var arr = [{"id": '', "text": '全部'}];
- for (var i = 0; i < me.data.length; i++) {
- arr.push(me.data[i]);
- }
- return arr;
- };
- Dict.prototype.combowith = function (opts) {
- var me = this;
- //方法回调
- if ($.type(opts) === 'function') {
- var r = opts(me.data);
- //有返回值
- if (r) return r;
- } else if ($.type(opts) === 'array') {
- //添加选项
- me.data.concat(opts);
- } else if ($.type(opts) === "object") {
- me.data.push(opts);
- }
- var arr = [/*{"id": '', "text": '全部'}*/];
- for (var i = 0; i < me.data.length; i++) {
- arr.push(me.data[i]);
- }
- return arr;
- };
- Dict.prototype.combowithAllSelect = function (opts) {
- var me = this;
- //方法回调
- if ($.type(opts) === 'function') {
- var r = opts(me.data);
- //有返回值
- if (r) return r;
- } else if ($.type(opts) === 'array') {
- //添加选项
- me.data.concat(opts);
- } else if ($.type(opts) === "object") {
- me.data.push(opts);
- }
- var arr = [{"id": '0', "text": '请选择'}];
- for (var i = 0; i < me.data.length; i++) {
- arr.push(me.data[i]);
- }
- return arr;
- };
- Dict.prototype.tree = function (opts) {
- var me = this;
- //方法回调
- if ($.type(opts) === 'function') {
- var r = opts(me.data);
- //有返回值
- if (r) return r;
- } else if ($.type(opts) === 'array') {
- //添加选项
- me.data.concat(opts);
- } else if ($.type(opts) === "object") {
- me.data.push(opts);
- }
- return me.data;
- };
|