common.js

  1 define(["utils/common"], function() {
  2     var util = function() {};
  3     util.prototype = {
  4         /**
  5          * 函数描述:下载图片,构造img  ok
  6          * @param {string} url 请求路径
  7          * @return {object} img
  8          */
  9         _loadImage: function (imgId, imgUrl, callback) {
 10             var img = new Image();
 11             img.src = imgUrl;
 12             img.id = imgId;
 13             img.onload = function () { //图片下载完毕时异步调用callback函数。
 14                 callback({'img': img});
 15             };
 16         },
 17         /**
 18          * 函数描述:将portal中的时间格式化 例如:2016-12-06 
 19          * @param {string} dd
 20          * @return {string} time 
 21          */
 22         formatDate: function(dd) {
 23             var now = new Date(dd);
 24             var year = now.getFullYear();
 25             var month = now.getMonth() + 1;
 26             var date = now.getDate();
 27             var hour = now.getHours();
 28             var minute = now.getMinutes();
 29             var second = now.getSeconds();
 30             var newMonth = (month < 10) ? "0" + month : month;
 31             var newDate = (date < 10) ? "0" + date : date;
 32             var newHour = (hour < 10) ? "0" + hour : hour;
 33             var newMinute = (minute < 10) ? "0" + minute : minute;
 34             var newSecond = (second < 10) ? "0" + second : second;
 35             return year + "-" + newMonth + "-" + newDate;
 36         },
 37         getQueryStringByKey: function(key, toEnd /* optional */) {
 38             var regSuffix = toEnd ? "=*(.+$)" : "=(.*?)(?=&|$)";
 39             return (document.location.search.match(new RegExp("(?:^\\?|&)" + key + regSuffix)) || ['', null])[1];
 40         },
 41         replaceQueryStringValue: function(key, value, changePage) {
 42             var newhref = '';
 43             //default is true, change page to 1
 44             changePage = ((changePage == null && key != 'page') ? true : changePage);
 45             if (document.location.search.match(new RegExp("(?:^\\?|&)" + key + "=(.*?)(?=&|$)"))) {
 46                 var queryString = document.location.search.match(new RegExp("(?:^\\?|&)" + key + "=(.*?)(?=&|$)"));
 47                 var newQuery = queryString[0].replace(queryString[1], value);
 48                 newhref = document.location.href.replace(queryString[0], newQuery);
 49             } else {
 50                 if (document.location.href.indexOf('?') > -1)
 51                     newhref = (document.location.href + '&' + key + '=' + value)
 52                 else
 53                     newhref = (document.location.href + '?' + key + '=' + value)
 54             }
 55 
 56             if (changePage && document.location.search.match(new RegExp("(?:^\\?|&)" + 'page' + "=(.*?)(?=&|$)"))) {
 57                 var pageString = document.location.search.match(new RegExp("(?:^\\?|&)" + 'page' + "=(.*?)(?=&|$)"));
 58                 var newPage = pageString[0].replace(pageString[1], '1');
 59                 newhref = newhref.replace(pageString[0], newPage);
 60             }
 61 
 62             return newhref;
 63         },
 64         deleteQueryStringByKey: function(key) {
 65             var queryString = document.location.search.match(new RegExp("(?:^\\?|&)" + key + "=(.*?)(?=&|$)"));
 66             if (queryString == null) return document.location.href;
 67             var queryCount = queryString.input.split('&').length;
 68 
 69             if (queryCount == 1)
 70                 return document.location.href.replace(queryString[0], '');
 71             else {
 72                 if (queryString[0].indexOf('?') > -1)
 73                     return document.location.href.replace(queryString[0] + '&', '?');
 74                 else
 75                     return document.location.href.replace(queryString[0], '');
 76             }
 77 
 78         },
 79         setCookie: function(name, value, hours, path,domain) {
 80             var name = escape(name);
 81             var value = escape(value);
 82             var expires = new Date();
 83             expires.setTime(expires.getTime() + hours * 3600000);
 84             path = path == "" ? "" : ";path=" + path;
 85             domain = domain =="" ?"" : ";domain=" + domain;
 86             _expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString();
 87             document.cookie = name + "=" + value + _expires + path + domain;
 88         },
 89         getCookieValue: function(name) {
 90             var name = escape(name);
 91             var allcookies = document.cookie;
 92             name += "=";
 93             var pos = allcookies.indexOf(name);
 94             if (pos != -1) {
 95                 var start = pos + name.length;
 96                 var end = allcookies.indexOf(";", start);
 97                 if (end == -1) end = allcookies.length;
 98                 var value = allcookies.substring(start, end);
 99                 return unescape(value);
100             } else return "";
101         },
102         deleteCookie: function(name, path) {
103             var name = escape(name);
104             var expires = new Date(0);
105             path = path == "" ? "" : ";path=" + path;
106             document.cookie = name + "=" + ";expires=" + expires.toUTCString() + path;
107         },
108         disabledItem: function(select) {
109             $(select).attr('disabled', 'disabled');
110         },
111         enabledItem: function(select) {
112             $(select).removeAttr('disabled');
113         },
114         newDate: function(dateString) {
115             if (/msie/.test(navigator.userAgent.toLowerCase())) {
116                 var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
117                     date = new Date(NaN),
118                     month,
119                     parts = isoExp.exec(dateString);
120 
121                 if (parts) {
122                     month = +parts[2];
123                     date.setFullYear(parts[1], month - 1, parts[3]);
124                     if (month != date.getMonth() + 1) {
125                         date.setTime(NaN);
126                     }
127                 }
128                 return date;
129             } else {
130                 return new Date(dateString);
131             }
132         },
133         fixBug: function() {
134 
135         },
136 
137         /*****************************CHEN START******************************/
138 
139 
140         formatDateYear: function(dd) {
141             var now = new Date(dd);
142             var year = now.getFullYear();
143             return year;
144         },
145         //html转义
146         htmlEncode: function (str) {
147             var s = "";
148             if (!str || str.length == 0) return "";
149             s = str.replace(/&/g, "&gt;");
150             s = s.replace(/</g, "&lt;");
151             s = s.replace(/>/g, "&gt;");
152             s = s.replace(/ /g, "&nbsp;");
153             s = s.replace(/\'/g, "&#39;");
154             s = s.replace(/\"/g, "&quot;");
155             s = s.replace(/\n/g, "<br>");
156             return s;
157         },
158 
159         //HTML反转义
160         htmlDecode: function (str) {
161             var s = "";
162             if (!str || str.length == 0) return "";
163             s = str.replace(/&gt;/g, "&");
164             s = s.replace(/&lt;/g, "<");
165             s = s.replace(/&gt;/g, ">");
166             s = s.replace(/&nbsp;/g, " ");
167             s = s.replace(/&#39;/g, "\'");
168             s = s.replace(/&quot;/g, "\"");
169             s = s.replace(/<br>/g, "\n");
170             return s;
171         },
172 
173         rgb2hex: function(rgb) {
174             rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
175 
176             function hex(x) {
177                 return ("0" + parseInt(x).toString(16)).slice(-2);
178             }
179             return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
180         },
181 
182         fullScreen: function() {
183             var el = document.documentElement,
184                 rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen,
185                 wscript;
186             if (typeof rfs != "undefined" && rfs) {
187                 rfs.call(el);
188                 return;
189             }
190             if (typeof window.ActiveXObject != "undefined") {
191                 wscript = new ActiveXObject("WScript.Shell");
192                 if (wscript) {
193                     wscript.SendKeys("{F11}");
194                 }
195             }
196         },
197         exitFullScreen: function() {
198             var el = document,
199                 cfs = el.cancelFullScreen || el.webkitCancelFullScreen || el.mozCancelFullScreen || el.exitFullScreen,
200                 wscript;
201             if (typeof cfs != "undefined" && cfs) {
202                 cfs.call(el);
203                 return;
204             }
205             if (typeof window.ActiveXObject != "undefined") {
206                 wscript = new ActiveXObject("WScript.Shell");
207                 if (wscript != null) {
208                     wscript.SendKeys("{F11}");
209                 }
210             }
211         },
212         /*****************************CHEN END******************************/
213                 //触发回车事件兼容,ie、Firefox、Chrome
214         quickQueryCust: function(evt) {
215             evt = (evt) ? evt : ((window.event) ? window.event : ""); //兼容IE和Firefox获得keyBoardEvent对象
216             var key = evt.keyCode ? evt.keyCode : evt.which; //兼容IE和Firefox获得keyBoardEvent对象的键值
217             if (key == 13) { //判断是否是回车事件。
218                 //根据需要执行某种操作。
219                 return true; //return false是为了停止表单提交,如果return true或者不写的话,表单照样是会提交的。
220             }
221             return false;
222         },
223         scroll: function(obj, time) {
224             var height = $(obj).offset().top;
225             $("body,html").animate({
226                 scrollTop: $(obj).offset().top
227             }, time);
228         },
229         //平滑滚动到某元素中部
230         scrollMiddle: function(obj, time) {
231             $(document.body).animate({
232                 scrollTop: $(obj).offset().top + $(selector).height() / 2 - $(window).height() / 2
233             }, time);
234         },
235         fixExtention: function() {
236             //ie trim
237             if (!String.prototype.trim) {
238                 String.prototype.trim = function() {
239                     return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
240                 };
241             }
242             //ie Array
243             if (!Array.prototype.indexOf) {
244                 Array.prototype.indexOf = function(elt /*, from*/ ) {
245                     var len = this.length >>> 0;
246                     var from = Number(arguments[1]) || 0;
247                     from = (from < 0) ? Math.ceil(from) : Math.floor(from);
248                     if (from < 0)
249                         from += len;
250                     for (; from < len; from++) {
251                         if (from in this &&
252                             this[from] === elt)
253                             return from;
254                     }
255                     return -1;
256                 };
257             }
258 
259             // 对Date的扩展,将 Date 转化为指定格式的String
260             Date.prototype.Format = function(fmt) {
261                 var o = {
262                     "M+": this.getMonth() + 1, //月份
263                     "d+": this.getDate(), //
264                     "h+": this.getHours(), //小时
265                     "m+": this.getMinutes(), //
266                     "s+": this.getSeconds(), //
267                     "q+": Math.floor((this.getMonth() + 3) / 3), //季度
268                     "S": this.getMilliseconds() //毫秒
269                 };
270                 if (/(y+)/.test(fmt))
271                     fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
272                 for (var k in o)
273                     if (new RegExp("(" + k + ")").test(fmt))
274                         fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
275                 return fmt;
276             };
277         },
278         transformData: function(attr) {
279             var firstlevel = [];
280             for (var i = 0; i < attr.length; i++) {
281                 for (var j = 0; j < attr.length; j++) {
282                     if (attr[i].id == attr[j].parentid) {
283                         if (!attr[i].child) {
284                             attr[i].child = [];
285                         }
286                         attr[i].child.push(attr[j]);
287                     }
288                 }
289             }
290             for (var i = 0; i < attr.length; i++) {
291                 if (attr[i].parentid == -1) {
292                     firstlevel.push(attr[i])
293                 }
294             }
295             return firstlevel;
296         }
297     };
298     return util;
299 });

 

posted @ 2016-12-07 09:44  lihubadboy  阅读(157)  评论(0编辑  收藏  举报