123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- /**
- * power
- * @author luoyifan
- * 2018-11-22 17:32:00
- */
- "use strict";
- var sidm = 0;
- (function ($) {
- // 取消jQuery缓存
- $.ajaxSetup({
- cache: false
- });
- $.yvan = $.yvan || {};
- $.extend($.yvan, {
- version: '1.0.0',
- get: function (obj, exp) {
- if ($.type(exp) === 'string') {
- exp = exp.split('.');
- }
- return exp.reduce(function (acc, key) {
- return acc && key in acc ? acc[key] : null;
- }, obj);
- },
- set: function (obj, exp, value) {
- if ($.type(exp) === 'string') {
- exp = exp.split('.');
- }
- for (var i = exp.length - 1; i >= 0; i--) {
- var c = {};
- c[exp[i]] = value;
- value = c;
- }
- $.extend(true, obj, value);
- },
- props: function (p1, p2) {
- var target = {}, i;
- if ($.type(p2) === 'array') {
- for (i = 0; i < p2.length; i++) {
- if (p1.hasOwnProperty(p2[i])) {
- target[p2[i]] = p1[p2[i]];
- }
- }
- } else if ($.type(p2) === 'object') {
- for (var name in p2) {
- if (!p2.hasOwnProperty(name)) continue;
- target[p2[name]] = p1[name];
- }
- }
- return target;
- },
- upload: function (ajaxOpts) {
- var $dom = $('<form id="uploadForm" enctype="multipart/form-data">'
- + '<input type="file" name="file" multiple="multiple"/>'
- + '</form>');
- $('body').append($dom.hide());
- $dom.change(function () {
- var formData = new FormData($dom[0]);
- if (ajaxOpts.data) {
- for (var name in ajaxOpts.data) {
- if (!ajaxOpts.data.hasOwnProperty(name)) continue;
- formData.append(name, ajaxOpts.data[name]);
- }
- }
- $.yvan.ajax($.extend({}, ajaxOpts, {
- type: 'post',
- data: formData,
- cache: false,
- processData: false,
- contentType: false,
- }));
- $dom.remove();
- }).find('input').trigger("click");
- },
- createId: function (prefix) {
- return (prefix || 'd') + '_' + (sidm++);
- },
- getRandomNum: function (a, b) {
- switch (arguments.length) {
- case 1:
- return parseInt(Math.random() * a + 1);
- case 2:
- return parseInt(Math.random() * (b - a + 1) + a);
- default:
- return 0;
- }
- },
- unparam: function (query) {
- var query_string = {};
- if (query.slice(0, 1) === '#') {
- query = query.slice(1);
- }
- var vars = query.split("&");
- for (var i = 0; i < vars.length; i++) {
- var pair = vars[i].split("=");
- pair[0] = decodeURIComponent(pair[0]);
- pair[1] = decodeURIComponent(pair[1]);
- // If first entry with this name
- if (typeof query_string[pair[0]] === "undefined") {
- query_string[pair[0]] = pair[1];
- } else if (typeof query_string[pair[0]] === "string") {
- var arr = [query_string[pair[0]], pair[1]];
- query_string[pair[0]] = arr;
- } else {
- query_string[pair[0]].push(pair[1]);
- }
- }
- return query_string;
- },
- getRandomNumByDef: function () {
- return this.getRandomNum(1e5, 999999);
- },
- fillCommonProperties: function ($target, opts) {
- for (var name in opts) {
- if (!opts.hasOwnProperty(name)) continue;
- switch (name) {
- case 'xtype':
- $target.attr('xtype', opts[name]);
- break;
- case 'attr':
- for (var attrKey in opts[name]) {
- if (!opts[name].hasOwnProperty(attrKey)) continue;
- $target.attr(attrKey, opts[name][attrKey]);
- }
- break;
- case 'dialogId':
- $target.addClass('dlg_' + opts[name]);
- break;
- case 'name':
- $target.attr('itemId', opts[name]);
- break;
- case 'id':
- $target.attr('id', opts[name]);
- break;
- case 'html':
- $target.html(opts[name]);
- break;
- case 'class':
- $target.addClass(opts[name]);
- break;
- case 'css':
- $target.css(opts[name]);
- break;
- }
- }
- },
- xtypeIsInput: function (xtype) {
- return ($.inArray(xtype, ['textbox', 'combobox', 'searchbox', 'datebox', 'numberbox', 'combotree', 'combotreegrid', 'combogrid', 'datetimebox', 'tagbox', 'maskedbox', 'passwordbox', 'filebox', 'numberspinner', 'hidden']) >= 0);
- },
- copyData: function (str) {
- var save = function (e) {
- e.clipboardData.setData('text/plain', str);
- e.preventDefault();
- document.removeEventListener('copy', save);
- };
- document.addEventListener('copy', save);
- document.execCommand("copy");
- },
- getItems: function (item, context) {
- if (!item.items) return [];
- if ($.type(item.items) === 'array') return item.items;
- if ($.type(item.items) === 'function') {
- var r = item.items.apply(this, context);
- if (!r) return [];
- if ($.type(r) === 'array') return r;
- return [r];
- }
- return [item.items];
- },
- formClear: function () {
- var jq = this;
- if (jq.length <= 0) return;
- jq.each(function (index, ele) {
- if (ele.tagName === 'FORM') {
- var $ele = $(ele);
- $ele.form('clear');
- $ele.find('input[type="hidden"]').change();
- $ele.find('input[type=select]').trigger('change');
- //解决 formClear 但 searchbox 仍然会还原值的问题
- $ele.find('.easyui-searchbox').each(function (a1, a2) {
- $(a2).data().widgetTmpValue = '';
- $(a2).data().widgetValue = '';
- });
- }
- });
- jq.find('form').each(function (index, form) {
- var $form = $(form);
- $form.form('clear');
- $form.find('input[type="hidden"]').change();
- $form.find('input[type=select]').trigger('change');
- //解决 formClear 但 searchbox 仍然会还原值的问题
- $(form).find('.easyui-searchbox').each(function (a1, a2) {
- $(a2).data().widgetTmpValue = '';
- $(a2).data().widgetValue = '';
- });
- });
- },
- formSet: function (data) {
- var jq = this;
- if (jq.length <= 0) {
- return {};
- }
- var r = jq.each(function (index, ele) {
- if (ele.tagName === 'FORM') {
- var $ele = $(ele);
- $ele.form('load', data);
- $ele.find('input[type="hidden"]').change();
- $ele.find('input[type=select]').trigger('change');
- //解决对焦之后,值会消失的问题
- $(ele).find('.easyui-searchbox').each(function (a1, a2) {
- $(a2).data().widgetTmpValue = data[$(a2).attr('itemid')];
- $(a2).data().widgetValue = data[$(a2).attr('itemid')];
- });
- }
- });
- jq.find('form').each(function (index, form) {
- var $form = $(form);
- if ($form.attr('name')) {
- var formName = $form.attr('name');
- if (data.hasOwnProperty(formName) && $.type(data[formName]) === 'object') {
- //子对象赋值
- $form.form('load', data[formName]);
- $form.find('input[type="hidden"]').change();
- $form.find('input[type=select]').trigger('change');
- //解决对焦之后,值会消失的问题
- $form.find('.easyui-searchbox').each(function (a1, a2) {
- $(a2).data().widgetTmpValue = data[$(a2).attr('itemid')];
- $(a2).data().widgetValue = data[$(a2).attr('itemid')];
- });
- }
- } else {
- //没有名字,平级赋值
- $form.form('load', data);
- $form.find('input[type="hidden"]').change();
- $form.find('input[type=select]').trigger('change');
- //解决对焦之后,值会消失的问题
- $form.find('.easyui-searchbox').each(function (a1, a2) {
- $(a2).data().widgetTmpValue = data[$(a2).attr('itemid')];
- $(a2).data().widgetValue = data[$(a2).attr('itemid')];
- });
- }
- });
- return r;
- },
- formGet: function (validate) {
- var jq = this;
- if (jq.length <= 0) {
- return {};
- }
- var vra = [];
- var m = [];
- jq.each(function (index, ele) {
- if (ele.tagName === 'FORM') {
- var $form = $(this);
- if (validate) {
- //需要强制校验
- var vr = $form.form('enableValidation').form('validate');
- if (!vr) {
- vra.push($form);
- }
- if (vra.length > 0) {
- //校验失败
- return vra;
- }
- }
- m.push({
- id: '',
- obj: $form.serializeObject()
- });
- }
- });
- jq.find('form').each(function (index, form) {
- var $form = $(form);
- if (validate) {
- //需要强制校验
- var vr = $form.form('enableValidation').form('validate');
- if (!vr) {
- vra.push($form);
- }
- if (vra.length > 0) {
- //校验失败
- return vra;
- }
- }
- if ($form.attr('name')) {
- m.push({
- id: $form.attr('name'),
- obj: $form.serializeObject()
- });
- } else {
- m.push({
- id: '',
- obj: $form.serializeObject()
- });
- }
- });
- if (validate && vra.length > 0) {
- return {
- validate: false,
- validateError: vra
- };
- }
- var r = {};
- if (validate) {
- $.extend(r, {
- validate: true
- });
- }
- for (var i = 0; i < m.length; i++) {
- if (m[i].id) {
- if (r.hasOwnProperty(m[i].id)) {
- $.extend(r[m[i].id], m[i].obj);
- } else {
- r[m[i].id] = m[i].obj;
- }
- } else {
- $.extend(r, m[i].obj);
- }
- }
- return r;
- },
- postJson: function (opts) {
- var data = opts.data;
- $.yvan.ajax($.extend(opts, {
- type: 'post',
- dataType: 'json',
- data: JSON.stringify(data),
- contentType: "application/json; charset=utf-8"
- }));
- },
- uploadFormData: function ($form, opts) {
- var vr = $form.form('enableValidation').form('validate');
- if (!vr) {
- return;
- }
- var formData = new FormData($form[0]);
- formData.delete("file");
- $.yvan.ajax($.extend({}, opts, {
- type: 'post',
- data: formData,
- cache: false,
- processData: false,
- contentType: false,
- }));
- },
- uploadFile: function (formData, opts) {
- $.yvan.ajax($.extend({}, opts, {
- type: 'post',
- data: formData,
- cache: false,
- processData: false,
- contentType: false,
- }));
- },
- uploadFileCheckMd5: function (formData, opts) {
- $.yvan.getFileMd5(formData.get('file'), function (md5) {
- formData.append('fileMd5', md5);
- $.yvan.ajax({
- url: fileUrl('/file/isExist'),
- method: 'get',
- data: {fileMd5: md5, param: formData.get('param')},
- success: function (data) {
- if (data.data === '') {
- $.yvan.uploadFile(formData, opts);
- } else {
- opts.success(data);
- }
- }
- });
- });
- },
- getFileMd5: function (file, valueCallback) {
- var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
- md5value = '',
- chunkSize = 2097152, // Read in chunks of 2MB
- chunks = Math.ceil(file.size / chunkSize),
- currentChunk = 0,
- spark = new SparkMD5.ArrayBuffer(),
- fileReader = new FileReader();
- fileReader.onload = function (e) {
- console.log('read chunk nr', currentChunk + 1, 'of', chunks);
- spark.append(e.target.result); // Append array buffer
- currentChunk++;
- if (currentChunk < chunks) {
- loadNext();
- } else {
- console.log('finished loading');
- md5value = spark.end();
- console.info('computed hash', md5value); // Compute hash
- valueCallback(md5value);
- }
- };
- fileReader.onerror = function () {
- console.warn('oops, something went wrong.');
- };
- function loadNext() {
- var start = currentChunk * chunkSize,
- end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
- fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
- }
- loadNext();
- },
- postForm: function ($form, opts) {
- var vr = $form.form('enableValidation').form('validate');
- if (!vr) {
- return false;
- }
- //var data = $form.serializeObject();
- var data = $form.formGet();
- if (opts.dataProcess) {
- opts.dataProcess(data);
- }
- this.postJson($.extend(opts, {
- data: data
- }));
- },
- //不验证提交Form,物资编码使用
- postFormNoValidate: function ($form, opts) {
- // var vr = $form.form('enableValidation').form('validate');
- // if (!vr) {
- // return false;
- // }
- //var data = $form.serializeObject();
- var data = $form.formGet();
- if (opts.dataProcess) {
- opts.dataProcess(data);
- }
- this.postJson($.extend(opts, {
- data: data
- }));
- },
- easyuiAjax: function (opts, param, success, error) {
- if (!opts.url) {
- if ($.type(opts.bizDict) === 'string') {
- //数据来源于 biz 字典
- success($.yvan.sysDict(opts.bizDict).combo());
- return;
- }
- if ($.type(opts.sysDict) === 'string') {
- //数据来源于 sys 字典
- success($.yvan.sysDict(opts.sysDict).combo());
- return;
- }
- //其他情况
- error();
- return false;
- }
- if (opts.hasOwnProperty('loadCondition')) {
- //有ajax条件的情况
- if (!opts.loadCondition(opts, param)) {
- error();
- return false;
- }
- }
- $.yvan.ajax({
- type: opts.method,
- url: opts.url,
- data: param,
- loadingMask: opts.loadingMask || false,
- success: function (data, textStatus, res) {
- if (opts.dataProcess) {
- opts.dataProcess.apply(this, arguments);
- }
- if (opts.pagination) {
- success({
- total: data.pagination.count,
- rows: data.data
- });
- } else {
- success(data.data);
- }
- },
- error: function (event, xhr, options, exc) {
- error.apply(this, arguments);
- }
- });
- }
- });
- $.fn.extend({
- progress: function (method) {
- if ($.type(method) === 'string') {
- if (method === 'close') {
- this.find('.loading-wrap').remove();
- } else {
- console.error('no method: progress(' + method + ')');
- }
- } else {
- if ($('.loading-wrap').length > 0) {
- return;
- }
- var $dom = $(
- '<div class="loading-wrap" style="opacity:.6">' +
- '<div class="spinner">' +
- ' <div class="spinner-container container1">\n' +
- ' <div class="circle1"></div>\n' +
- ' <div class="circle2"></div>\n' +
- ' <div class="circle3"></div>\n' +
- ' <div class="circle4"></div>\n' +
- ' </div>\n' +
- ' <div class="spinner-container container2">\n' +
- ' <div class="circle1"></div>\n' +
- ' <div class="circle2"></div>\n' +
- ' <div class="circle3"></div>\n' +
- ' <div class="circle4"></div>\n' +
- ' </div>\n' +
- ' <div class="spinner-container container3">\n' +
- ' <div class="circle1"></div>\n' +
- ' <div class="circle2"></div>\n' +
- ' <div class="circle3"></div>\n' +
- ' <div class="circle4"></div>\n' +
- ' </div>' +
- '</div>' +
- '</div>'
- );
- this.append($dom);
- }
- },
- serializeObject: function () {
- var o = {};
- var a = this.serializeArray();
- $.each(a, function () {
- if (o[this.name] !== undefined) {
- if (!o[this.name].push) {
- o[this.name] = [o[this.name]];
- }
- o[this.name].push(this.value || '');
- } else {
- o[this.name] = this.value || '';
- }
- });
- return o;
- },
- findWindow: function () {
- var $me = $(this);
- if ($me.parent().is('.panel-footer')) {
- return $(this).closest('.window').down('dialog');
- }
- return $(this).closest('.window').down('dialog');
- },
- up: function (xtype) {
- return this.closest("[xtype='" + xtype + "']");
- },
- down: function (xtype) {
- return this.find("[xtype='" + xtype + "']");
- },
- item: function (itemId) {
- return this.find('[itemId="' + itemId + '"]');
- },
- findForm: function () {
- //找到最可能的上级容器
- var container = this.closest('form');
- if (container.length > 0) return container;
- container = this.up('group');
- if (container.length > 0) return container;
- container = this.up('tab');
- if (container.length > 0) return container;
- container = this.up('dialog');
- return container;
- },
- formGet: function () {
- return $.yvan.formGet.apply(this, arguments);
- },
- formSet: function () {
- return $.yvan.formSet.apply(this, arguments);
- },
- formClear: function () {
- return $.yvan.formClear.apply(this, arguments);
- },
- showItem: function (validate) {
- var tabs = this.up('tabs');
- if (tabs.length <= 0) {
- return;
- }
- var tab = this.up('tab');
- var i = tabs.tabs('getTabIndex', tab);
- if (i >= 0) {
- var i = this.up('tabs').tabs('select', i);
- }
- if (validate) {
- //this.form('enableValidation').form('validate');
- this.find(".validatebox-invalid:first").focus();
- }
- }
- });
- $.extend(true, $.fn.combobox.defaults, {
- loader: function (param, success, error) {
- var me = $(this);
- var opts = me.combobox("options");
- $.yvan.easyuiAjax(opts, param, success, error);
- },
- width: 280,
- labelWidth: 120,
- editable: false,
- panelHeight: 'auto',
- labelAlign: 'right',
- valueField: 'id',
- textField: 'text'
- });
- $.extend($.fn.menubutton.defaults, {
- hasDownArrow: false,
- showEvent: 'click',
- menuAlign: 'left',
- iconCls: 'fa fa-caret-down'
- });
- $.extend($.fn.textbox.defaults, {
- width: 280,
- labelWidth: 120,
- labelAlign: 'right'
- });
- $.extend($.fn.passwordbox.defaults, {
- width: 280,
- labelWidth: 120,
- labelAlign: 'right'
- });
- $.extend($.fn.checkbox.defaults, {
- labelPosition: 'after'
- });
- $.extend($.fn.radiobutton.defaults, {
- labelPosition: 'after'
- });
- $.extend($.fn.maskedbox.defaults, {
- width: 280,
- labelWidth: 120,
- labelAlign: 'right'
- });
- $.extend($.fn.numberbox.defaults, {
- width: 280,
- labelWidth: 120,
- labelAlign: 'right'
- });
- $.extend($.fn.datebox.defaults, {
- panelMinWidth: 272,
- panelHeight: 301,
- width: 280,
- labelWidth: 120,
- labelAlign: 'right',
- validType: "dateValidate"
- });
- $.extend($.fn.filebox.defaults, {
- width: 280,
- labelWidth: 120,
- labelAlign: 'right',
- label: '上传文件',
- buttonText: '选择文件'
- });
- $.extend($.fn.datetimebox.defaults, {
- panelMinWidth: 422,
- panelHeight: 301,
- width: 280,
- labelWidth: 120,
- labelAlign: 'right'
- });
- $.extend($.fn.datetimebox.defaults, {
- width: 280,
- labelWidth: 120,
- labelAlign: 'right',
- });
- $.extend($.fn.searchbox.defaults, {
- width: 280,
- labelWidth: 120,
- labelAlign: 'right'
- });
- $.extend($.fn.treegrid.defaults, {
- loader: function (param, success, error) {
- var me = $(this);
- var opts = me.treegrid("options");
- $.yvan.easyuiAjax(opts, param, success, error);
- },
- method: 'get',
- });
- $.extend($.fn.tree.defaults, {
- loader: function (param, success, error) {
- var me = $(this);
- var opts = me.tree("options");
- $.yvan.easyuiAjax(opts, param, success, error);
- },
- method: 'get',
- });
- $.extend($.fn.datalist.defaults, {
- loader: function (param, success, error) {
- var me = $(this);
- var opts = me.datalist("options");
- $.yvan.easyuiAjax(opts, param, success, error);
- },
- lines: true,
- method: 'get',
- pagination: false,
- emptyMsg: "暂无数据",
- pageSize: 50,
- pageList: [10, 30, 50, 100],
- remoteSort: true,
- striped: true,
- fitColumns: true,
- fit: true,
- singleSelect: true,
- onLoadSuccess: function (data) {
- if (data.rows.length > 0) {
- $(this).datalist('selectRow', 0);
- }
- }
- });
- $.extend($.fn.combogrid.defaults, {
- loader: function (param, success, error) {
- var me = $(this);
- var opts = me.datagrid("options");
- $.yvan.easyuiAjax(opts, param, success, error);
- },
- width: 280,
- editable: false,
- labelWidth: 120,
- labelAlign: 'right',
- method: 'get',
- pagination: false,
- rownumbers: true,
- pageList: [10, 30, 50, 100],
- pageSize: 10,
- remoteSort: true,
- striped: true,
- fitColumns: true,
- singleSelect: true
- });
- $.extend($.fn.tabs.defaults, {
- tabPosition: 'top',
- plain: true,
- fit: true
- });
- $.extend($.fn.datagrid.defaults, {
- loader: function (param, success, error) {
- var me = $(this);
- var opts = me.datagrid("options");
- $.yvan.easyuiAjax(opts, param, success, error);
- },
- method: 'get',
- pagination: true,
- rownumbers: true,
- emptyMsg: "暂无数据",
- pageSize: 50,
- pageList: [10, 30, 50, 100],
- remoteSort: true,
- striped: true,
- fitColumns: true,
- fit: true,
- singleSelect: true
- });
- if ($.fn.edatagrid) {
- $.extend($.fn.edatagrid.defaults, {
- loader: function (param, success, error) {
- var me = $(this);
- var opts = me.datagrid("options");
- $.yvan.easyuiAjax(opts, param, success, error);
- },
- method: 'get',
- pagination: true,
- rownumbers: true,
- emptyMsg: "暂无数据",
- pageList: [10, 30, 50, 100],
- pageSize: 50,
- remoteSort: true,
- striped: true,
- fitColumns: true,
- fit: true,
- singleSelect: true
- });
- }
- })(jQuery);
|