var wcp = wcp || {}; (function ($) { //日志 wcp.log = wcp.log || {}; wcp.log.levels = { DEBUG: 1, INFO: 2, WARN: 3, ERROR: 4, FATAL: 5 }; wcp.log.level = wcp.log.levels.DEBUG; wcp.log.log = function (logObject, logLevel) { if (!window.console || !window.console.log) { return; } if (logLevel != undefined && logLevel < wcp.log.level) { return; } console.log(logObject); }; wcp.log.debug = function (logObject) { wcp.log.log("DEBUG: ", wcp.log.levels.DEBUG); wcp.log.log(logObject, wcp.log.levels.DEBUG); }; wcp.log.info = function (logObject) { wcp.log.log("INFO: ", wcp.log.levels.INFO); wcp.log.log(logObject, wcp.log.levels.INFO); }; wcp.log.warn = function (logObject) { wcp.log.log("WARN: ", wcp.log.levels.WARN); wcp.log.log(logObject, wcp.log.levels.WARN); }; wcp.log.error = function (logObject) { wcp.log.log("ERROR: ", wcp.log.levels.ERROR); wcp.log.log(logObject, wcp.log.levels.ERROR); }; wcp.log.fatal = function (logObject) { wcp.log.log("FATAL: ", wcp.log.levels.FATAL); wcp.log.log(logObject, wcp.log.levels.FATAL); }; //提醒 wcp.notify = wcp.notify || {}; wcp.notify.success = function (message, title, options) { wcp.log.warn('wcp.notify.success 没有定义!'); }; wcp.notify.info = function (message, title, options) { wcp.log.warn('wcp.notify.info 没有定义!'); }; wcp.notify.warn = function (message, title, options) { wcp.log.warn('wcp.notify.warn 没有定义!'); }; wcp.notify.error = function (message, title, options) { wcp.log.warn('wcp.notify.error 没有定义!'); }; //消息 wcp.message = wcp.message || {}; var showMessage = function (message, title) { alert((title || '') + ' ' + message); if (!$) { wcp.log.warn('jQuery 没有定义,wcp.message 将不能返回 promise!'); return null; } return $.Deferred(function ($dfd) { $dfd.resolve(); }); }; wcp.message.info = function (message, title) { wcp.log.warn('wcp.message.info 没有定义!'); return showMessage(message, title); }; wcp.message.success = function (message, title) { wcp.log.warn('wcp.message.success 没有定义!'); return showMessage(message, title); }; wcp.message.warn = function (message, title) { wcp.log.warn('wcp.message.warn 没有定义!'); return showMessage(message, title); }; wcp.message.error = function (message, title) { wcp.log.warn('wcp.message.error 没有定义!'); return showMessage(message, title); }; wcp.message.confirm = function (message, titleOrCallback, callback) { wcp.log.warn('wcp.message.confirm 没有定义!'); if (titleOrCallback && !(typeof titleOrCallback == 'string')) { callback = titleOrCallback; } var result = confirm(message); callback && callback(result); if (!$) { wcp.log.warn('jQuery 没有定义,wcp.message 将不能返回 promise!'); return null; } return $.Deferred(function ($dfd) { $dfd.resolve(); }); }; /* UI *******************************************************/ wcp.ui = wcp.ui || {}; /* UI BLOCK */ //Defines UI Block API, not implements it wcp.ui.block = function (elm) { wcp.log.warn('wcp.ui.block 没有定义!'); }; wcp.ui.unblock = function (elm) { wcp.log.warn('wcp.ui.unblock 没有定义!'); }; /* UI BUSY */ //Defines UI Busy API, not implements it wcp.ui.setBusy = function (elm, optionsOrPromise) { wcp.log.warn('wcp.ui.setBusy 没有定义!'); }; wcp.ui.clearBusy = function (elm) { wcp.log.warn('wcp.ui.clearBusy 没有定义!'); }; //* Picker 资源选择器*******************************************************/ wcp.picker = wcp.picker || {}; wcp.picker.selectUser = function (elm) { wcp.log.warn('wcp.picker.selectUser 没有定义!'); }; wcp.picker.selectDept = function (elm) { wcp.log.warn('wcp.picker.selectDept 没有定义!'); }; wcp.picker.selectGroup = function (elm) { wcp.log.warn('wcp.picker.selectGroup 没有定义!'); }; wcp.picker.selectRole = function (elm) { wcp.log.warn('wcp.picker.selectRole 没有定义!'); }; wcp.picker.selectForm = function (elm) { wcp.log.warn('wcp.picker.selectForm 没有定义!'); }; wcp.picker.selectTable = function (elm) { wcp.log.warn('wcp.picker.selectTable 没有定义!'); }; wcp.picker.selectTableColumn = function (elm) { wcp.log.warn('wcp.picker.selectTableColumn 没有定义!'); }; wcp.picker.selectModule = function (elm) { wcp.log.warn('wcp.picker.selectModule 没有定义!'); }; wcp.picker.selectIcon = function (elm) { wcp.log.warn('wcp.picker.selectIcon 没有定义!'); }; wcp.picker.selectKeyword = function (elm) { wcp.log.warn('wcp.picker.selectKeyword 没有定义!'); }; wcp.picker.selectResource = function (elm) { wcp.log.warn('wcp.picker.selectResource 没有定义!'); }; wcp.picker.selectPlanDetailTemplate = function (elm) { wcp.log.warn('wcp.picker.selectPlanTemplate 没有定义!'); }; wcp.picker.selectOperation = function (elm) { wcp.log.warn('wcp.picker.selectOperation 没有定义!'); }; //事件总线 wcp.event = (function () { var _callbacks = {}; var on = function (eventName, callback) { if (!_callbacks[eventName]) { _callbacks[eventName] = []; } _callbacks[eventName].push(callback); }; var off = function (eventName, callback) { var callbacks = _callbacks[eventName]; if (!callbacks) { return; } var index = -1; for (var i = 0; i < callbacks.length; i++) { if (callbacks[i] === callback) { index = i; break; } } if (index < 0) { return; } _callbacks[eventName].splice(index, 1); }; var trigger = function (eventName) { var callbacks = _callbacks[eventName]; if (!callbacks || !callbacks.length) { return; } var args = Array.prototype.slice.call(arguments, 1); for (var i = 0; i < callbacks.length; i++) { callbacks[i].apply(this, args); } }; // 公共接口 /////////////////////////////////////////////////// return { on: on, off: off, trigger: trigger }; })(); //工具类 wcp.utils = wcp.utils || {}; /* 创建一个命名空间 * 例如: * var taskService = wcp.utils.createNamespace(wcp, 'services.task'); * taskService will be equal to wcp.services.task * first argument (root) must be defined first ************************************************************/ wcp.utils.createNamespace = function (root, ns) { var parts = ns.split('.'); for (var i = 0; i < parts.length; i++) { if (typeof root[parts[i]] == 'undefined') { root[parts[i]] = {}; } root = root[parts[i]]; } return root; }; /* 字符串替换 * given string (str). * Example: * wcp.utils.replaceAll('This is a test string', 'is', 'X') = 'ThX X a test string' ************************************************************/ wcp.utils.replaceAll = function (str, search, replacement) { var fix = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return str.replace(new RegExp(fix, 'g'), replacement); }; /* 格式化字符串 * Example: * wcp.utils.formatString('Hello {0}','Tuana') = 'Hello Tuana' ************************************************************/ wcp.utils.formatString = function () { if (arguments.length < 1) { return null; } var str = arguments[0]; for (var i = 1; i < arguments.length; i++) { var placeHolder = '{' + (i - 1) + '}'; str = wcp.utils.replaceAll(str, placeHolder, arguments[i]); } return str; }; /* 字符串转换为PascalCase模式 * ************************************************************/ wcp.utils.toPascalCase = function (str) { if (!str || !str.length) { return str; } if (str.length === 1) { return str.charAt(0).toUpperCase(); } return str.charAt(0).toUpperCase() + str.substr(1); } /* 字符串转换为camelCase模式 * ************************************************************/ wcp.utils.toCamelCase = function (str) { if (!str || !str.length) { return str; } if (str.length === 1) { return str.charAt(0).toLowerCase(); } return str.charAt(0).toLowerCase() + str.substr(1); } /* 截断字符串 * ************************************************************/ wcp.utils.truncateString = function (str, maxLength) { if (!str || !str.length || str.length <= maxLength) { return str; } return str.substr(0, maxLength); }; /* 截断字符串,并增加后缀 * ************************************************************/ wcp.utils.truncateStringWithPostfix = function (str, maxLength, postfix) { postfix = postfix || '...'; if (!str || !str.length || str.length <= maxLength) { return str; } if (maxLength <= postfix.length) { return postfix.substr(0, maxLength); } return str.substr(0, maxLength - postfix.length) + postfix; }; /* 判断是否为函数 * ************************************************************/ wcp.utils.isFunction = function (obj) { if ($) { //Prefer to use jQuery if possible return $.isFunction(obj); } //alternative for $.isFunction return !!(obj && obj.constructor && obj.call && obj.apply); }; /* 使用Base64编码Url参数,并且替换+、/、=,防止传输出现问题 * 需要引入plugins中的crypto-js文件夹的core.js和enc-base64.js * @param urlParam */ wcp.utils.encodeBase64UrlParam = function (urlParam) { var wordArray = CryptoJS.enc.Utf8.parse(urlParam); var base64 = CryptoJS.enc.Base64.stringify(wordArray); var ret = wcp.utils.replaceAll(base64, "+", "*"); ret = wcp.utils.replaceAll(ret, "/", "-"); ret = wcp.utils.replaceAll(ret, "=", "."); return ret; } /* 生成查询字符串 * parameterInfos should be an array of { name, value } objects * where name is query string parameter name and value is it's value. * includeQuestionMark is true by default. */ wcp.utils.buildQueryString = function (parameterInfos, includeQuestionMark) { if (includeQuestionMark === undefined) { includeQuestionMark = true; } var qs = ''; function addSeperator() { if (!qs.length) { if (includeQuestionMark) { qs = qs + '?'; } } else { qs = qs + '&'; } } for (var i = 0; i < parameterInfos.length; ++i) { var parameterInfo = parameterInfos[i]; if (parameterInfo.value === undefined) { continue; } if (parameterInfo.value === null) { parameterInfo.value = ''; } addSeperator(); if (parameterInfo.value.toJSON && typeof parameterInfo.value.toJSON === "function") { qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value.toJSON()); } else if (Array.isArray(parameterInfo.value) && parameterInfo.value.length) { for (var j = 0; j < parameterInfo.value.length; j++) { if (j > 0) { addSeperator(); } qs = qs + parameterInfo.name + '[' + j + ']=' + encodeURIComponent(parameterInfo.value[j]); } } else { qs = qs + parameterInfo.name + '=' + encodeURIComponent(parameterInfo.value); } } return qs; } /* * 设置Cookie * This is a simple implementation created to be used by wcp. * Please use a complete cookie library if you need. * @param {string} key * @param {string} value * @param {Date} expireDate (optional). If not specified the cookie will expire at the end of session. * @param {string} path (optional) */ wcp.utils.setCookieValue = function (key, value, expireDate, path) { var cookieValue = encodeURIComponent(key) + '='; if (value) { cookieValue = cookieValue + encodeURIComponent(value); } if (expireDate) { cookieValue = cookieValue + "; expires=" + expireDate.toUTCString(); } if (path) { cookieValue = cookieValue + "; path=" + path; } document.cookie = cookieValue; }; /* * 获取一个Cookie * This is a simple implementation created to be used by wcp. * Please use a complete cookie library if you need. * @param {string} key * @returns {string} Cookie value or null */ wcp.utils.getCookieValue = function (key) { var equalities = document.cookie.split('; '); for (var i = 0; i < equalities.length; i++) { if (!equalities[i]) { continue; } var splitted = equalities[i].split('='); if (splitted.length != 2) { continue; } if (decodeURIComponent(splitted[0]) === key) { return decodeURIComponent(splitted[1] || ''); } } return null; }; /* * 删除一个Cookie * This is a simple implementation created to be used by wcp. * Please use a complete cookie library if you need. * @param {string} key * @param {string} path (optional) */ wcp.utils.deleteCookie = function (key, path) { var cookieValue = encodeURIComponent(key) + '='; cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString(); if (path) { cookieValue = cookieValue + "; path=" + path; } document.cookie = cookieValue; } /* * 按比例缩放图片 */ wcp.utils.autoResizeImage = function (maxWidth, maxHeight, imgId) { var img = new Image(); img.src = $("#" + imgId).attr('src'); var hRatio; var wRatio; var ratio = 1; var w = img.width; var h = img.height; wRatio = maxWidth / w; hRatio = maxHeight / h; if (maxWidth == 0 && maxHeight == 0) { ratio = 1; } else if (maxWidth == 0) { // if (hRatio < 1) ratio = hRatio; } else if (maxHeight == 0) { if (wRatio < 1) ratio = wRatio; } else if (wRatio < 1 || hRatio < 1) { ratio = (wRatio <= hRatio ? wRatio : hRatio); } if (ratio < 1) { w = w * ratio; h = h * ratio; } $("#" + imgId).height(h); $("#" + imgId).width(w); } /* 时间处理 *****************************************/ wcp.timing = wcp.timing || {}; wcp.timing.utcClockProvider = (function () { var toUtc = function (date) { return Date.UTC( date.getUTCFullYear() , date.getUTCMonth() , date.getUTCDate() , date.getUTCHours() , date.getUTCMinutes() , date.getUTCSeconds() , date.getUTCMilliseconds() ); } var now = function () { return new Date(); }; var normalize = function (date) { if (!date) { return date; } return new Date(toUtc(date)); }; // Public interface /////////////////////////////////////////////////// return { now: now, normalize: normalize, supportsMultipleTimezone: true }; })(); wcp.timing.localClockProvider = (function () { var toLocal = function (date) { return new Date( date.getFullYear() , date.getMonth() , date.getDate() , date.getHours() , date.getMinutes() , date.getSeconds() , date.getMilliseconds() ); } var now = function () { return toLocal(new Date()); } var normalize = function (date) { if (!date) { return date; } return toLocal(date); } // Public interface /////////////////////////////////////////////////// return { now: now, normalize: normalize, supportsMultipleTimezone: false }; })(); wcp.timing.unspecifiedClockProvider = (function () { var now = function () { return new Date(); } var normalize = function (date) { return date; } // Public interface /////////////////////////////////////////////////// return { now: now, normalize: normalize, supportsMultipleTimezone: false }; })(); wcp.timing.convertToUserTimezone = function (date) { var localTime = date.getTime(); var utcTime = localTime + (date.getTimezoneOffset() * 60000); var targetTime = parseInt(utcTime) + parseInt(wcp.timing.timeZoneInfo.windows.currentUtcOffsetInMilliseconds); return new Date(targetTime); }; /* 时钟 *****************************************/ wcp.clock = wcp.clock || {}; wcp.clock.now = function () { if (wcp.clock.provider) { return wcp.clock.provider.now(); } return new Date(); } wcp.clock.normalize = function (date) { if (wcp.clock.provider) { return wcp.clock.provider.normalize(date); } return date; } wcp.clock.provider = wcp.timing.unspecifiedClockProvider; })(jQuery); //加载日期控件只能对yyy-MM格式化 function loadDateControl(columnId) { $('#' + columnId).datebox({ //显示日趋选择对象后再触发弹出月份层的事件,初始化时没有生成月份层 onShowPanel: function () { //触发click事件弹出月份层 span.trigger('click'); if (!tds) //延时触发获取月份对象,因为上面的事件触发和对象生成有时间间隔 setTimeout(function () { tds = p.find('div.calendar-menu-month-inner td'); tds.click(function (e) { //禁止冒泡执行easyui给月份绑定的事件 e.stopPropagation(); //得到年份 var year = /\d{4}/.exec(span.html())[0], //月份 //之前是这样的month = parseInt($(this).attr('abbr'), 10) + 1; month = parseInt($(this).attr('abbr'), 10); //隐藏日期对象 $('#' + columnId).datebox('hidePanel') //设置日期的值 .datebox('setValue', year + '-' + month); }); }, 0); }, //配置parser,返回选择的日期 parser: function (s) { if (!s) return new Date(); var arr = s.split('-'); return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1); }, //配置formatter,只返回年月 之前是这样的d.getFullYear() + '-' +(d.getMonth()); formatter: function (d) { var currentMonth = (d.getMonth() + 1); var currentMonthStr = currentMonth < 10 ? ('0' + currentMonth) : (currentMonth + ''); return d.getFullYear() + '-' + currentMonthStr; } }); //日期选择对 象 var p = $('#' + columnId).datebox('panel'), //日期选择对象中月份 tds = false, //显示月份层的触发控件 span = p.find('span.calendar-text'); var curr_time = new Date(); //设置前当月 $("#" + columnId).datebox("setValue", myformatter(curr_time)); } //格式化日期 function myformatter(date) { //获取年份 var y = date.getFullYear(); //获取月份 var m = date.getMonth() + 1; return y + '-' + m; } //年格式化 function yearFormatter(date) { var y = date.getFullYear(); var m = date.getMonth() + 1; var d = date.getDate(); return y; } //获取年份 function yearParser(s) { if (!s) return new Date(); var y = s; var date; if (!isNaN(y)) { return new Date(y, 0, 1); } else { return new Date(); } } //通过Tab页签打开窗口 wcp.openUrlByTab = function (pageUrl, title, ids, refreshData) { if (top.addPanel) { top.addPanel(pageUrl, title); var curTabWin = null; var curTab = top.$('#tt').tabs('getSelected'); if (curTab && curTab.find('iframe').length > 0) { curTabWin = curTab.find('iframe')[0].contentWindow; //将回调函数传给子页面 $(curTabWin).load(function () { if (curTabWin.params) { //将回调函数传给子页面 if (ids) { curTabWin.params.ids = ids; } curTabWin.params.parent = window; curTabWin.params.callBack = refreshData; } }); } } else { wcp.openUrlByWindow(pageUrl); } } //通过Layer打开窗口 wcp.openUrlByLayer = function (pageWidth, pageHeight, pageUrl, title, ids, refreshData) { top.layer.open({ title: title, type: 2, area: [pageWidth + "px", pageHeight + "px"], fixed: false, content: pageUrl, success: function (layero, index) { var body = window.top.layer.getChildFrame('body', index); var iframeWin = window.top[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象 //将回调函数传给子页面 if (iframeWin.params) { if (ids) { iframeWin.params.ids = ids; } iframeWin.params.parent = window; iframeWin.params.callBack = refreshData; } } }); } wcp.openUrlByWindow = function (pageUrl, ids, refreshData) { var myWindow = window.open(pageUrl); //将回调函数传给子页面 $(myWindow).load(function () { if (myWindow.params) { if (ids) { myWindow.params.ids = ids; } myWindow.params.parent = window; myWindow.params.callBack = refreshData; } }); } //居中显示打开窗口 wcp.openUrlByWindow = function (pageUrl, ids, name, iWidth, iHeight, refreshData) { var iTop = (window.screen.height - 30 - iHeight) / 2; //获得窗口的垂直位置; var iLeft = (window.screen.width - 10 - iWidth) / 2; //获得窗口的水平位置; var myWindow; if (iWidth == 0) { myWindow = window.open(pageUrl); } else { myWindow = window.open(pageUrl, name.replace(":", ""), 'height=' + iHeight + ',innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no'); } //将回调函数传给子页面 $(myWindow).load(function () { if (myWindow.params) { if (ids) { myWindow.params.ids = ids; } myWindow.params.parent = window; myWindow.params.callBack = refreshData; } }); }