syExtJquery.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. var sy = sy || {};
  2. sy.data = sy.data || {};// 用于存放临时的数据或者对象
  3. /**
  4. * 屏蔽右键
  5. *
  6. * @author Jeffrey
  7. *
  8. * @requires jQuery
  9. */
  10. $(document).bind('contextmenu', function() {
  11. // return false;
  12. });
  13. /**
  14. * 禁止复制
  15. *
  16. * @author Jeffrey
  17. *
  18. * @requires jQuery
  19. */
  20. $(document).bind('selectstart', function() {
  21. // return false;
  22. });
  23. /**
  24. * @author Jeffrey
  25. *
  26. * 增加命名空间功能
  27. *
  28. * 使用方法:sy.ns('jQuery.bbb.ccc','jQuery.eee.fff');
  29. */
  30. sy.ns = function() {
  31. var o = {}, d;
  32. for (var i = 0; i < arguments.length; i++) {
  33. d = arguments[i].split(".");
  34. o = window[d[0]] = window[d[0]] || {};
  35. for (var k = 0; k < d.slice(1).length; k++) {
  36. o = o[d[k + 1]] = o[d[k + 1]] || {};
  37. }
  38. }
  39. return o;
  40. };
  41. /**
  42. * 将form表单元素的值序列化成对象
  43. *
  44. * @example sy.serializeObject($('#formId'))
  45. *
  46. * @author Jeffrey
  47. *
  48. * @requires jQuery
  49. *
  50. * @returns object
  51. */
  52. sy.serializeObject = function(form) {
  53. var o = {};
  54. $.each(form.serializeArray(), function(index) {
  55. if (this['value'] != undefined && this['value'].length > 0) {// 如果表单项的值非空,才进行序列化操作
  56. if (o[this['name']]) {
  57. o[this['name']] = o[this['name']] + "," + this['value'];
  58. } else {
  59. o[this['name']] = this['value'];
  60. }
  61. }
  62. });
  63. return o;
  64. };
  65. /**
  66. * 增加formatString功能
  67. *
  68. * @author Jeffrey
  69. *
  70. * @example sy.formatString('字符串{0}字符串{1}字符串','第一个变量','第二个变量');
  71. *
  72. * @returns 格式化后的字符串
  73. */
  74. sy.formatString = function(str) {
  75. for (var i = 0; i < arguments.length - 1; i++) {
  76. str = str.replace("{" + i + "}", arguments[i + 1]);
  77. }
  78. return str;
  79. };
  80. /**
  81. * 接收一个以逗号分割的字符串,返回List,list里每一项都是一个字符串
  82. *
  83. * @author Jeffrey
  84. *
  85. * @returns list
  86. */
  87. sy.stringToList = function(value) {
  88. if (value != undefined && value != '') {
  89. var values = [];
  90. var t = value.split(',');
  91. for (var i = 0; i < t.length; i++) {
  92. values.push('' + t[i]);/* 避免他将ID当成数字 */
  93. }
  94. return values;
  95. } else {
  96. return [];
  97. }
  98. };
  99. /**
  100. * JSON对象转换成String
  101. *
  102. * @param o
  103. * @returns
  104. */
  105. sy.jsonToString = function(o) {
  106. var r = [];
  107. if (typeof o == "string")
  108. return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
  109. if (typeof o == "object") {
  110. if (!o.sort) {
  111. for ( var i in o)
  112. r.push(i + ":" + sy.jsonToString(o[i]));
  113. if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
  114. r.push("toString:" + o.toString.toString());
  115. }
  116. r = "{" + r.join() + "}";
  117. } else {
  118. for (var i = 0; i < o.length; i++)
  119. r.push(sy.jsonToString(o[i]));
  120. r = "[" + r.join() + "]";
  121. }
  122. return r;
  123. }
  124. return o.toString();
  125. };
  126. /**
  127. * Create a cookie with the given key and value and other optional parameters.
  128. *
  129. * @example sy.cookie('the_cookie', 'the_value');
  130. * @desc Set the value of a cookie.
  131. * @example sy.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
  132. * @desc Create a cookie with all available options.
  133. * @example sy.cookie('the_cookie', 'the_value');
  134. * @desc Create a session cookie.
  135. * @example sy.cookie('the_cookie', null);
  136. * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain used when the cookie was set.
  137. *
  138. * @param String
  139. * key The key of the cookie.
  140. * @param String
  141. * value The value of the cookie.
  142. * @param Object
  143. * options An object literal containing key/value pairs to provide optional cookie attributes.
  144. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. If a negative value is specified (e.g. a date in the past), the cookie will be deleted. If set to null or omitted, the cookie will be a session cookie and will not be retained when the the browser exits.
  145. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
  146. * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
  147. * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will require a secure protocol (like HTTPS).
  148. * @type undefined
  149. *
  150. * @name sy.cookie
  151. * @cat Plugins/Cookie
  152. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  153. *
  154. * Get the value of a cookie with the given key.
  155. *
  156. * @example sy.cookie('the_cookie');
  157. * @desc Get the value of a cookie.
  158. *
  159. * @param String
  160. * key The key of the cookie.
  161. * @return The value of the cookie.
  162. * @type String
  163. *
  164. * @name sy.cookie
  165. * @cat Plugins/Cookie
  166. * @author Klaus Hartl/klaus.hartl@stilbuero.de
  167. */
  168. sy.cookie = function(key, value, options) {
  169. if (arguments.length > 1 && (value === null || typeof value !== "object")) {
  170. options = $.extend({}, options);
  171. if (value === null) {
  172. options.expires = -1;
  173. }
  174. if (typeof options.expires === 'number') {
  175. var days = options.expires, t = options.expires = new Date();
  176. t.setDate(t.getDate() + days);
  177. }
  178. return (document.cookie = [ encodeURIComponent(key), '=', options.raw ? String(value) : encodeURIComponent(String(value)), options.expires ? '; expires=' + options.expires.toUTCString() : '', options.path ? '; path=' + options.path : '', options.domain ? '; domain=' + options.domain : '', options.secure ? '; secure' : '' ].join(''));
  179. }
  180. options = value || {};
  181. var result, decode = options.raw ? function(s) {
  182. return s;
  183. } : decodeURIComponent;
  184. return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
  185. };
  186. /**
  187. * 改变jQuery的AJAX默认属性和方法
  188. *
  189. * @author Jeffrey
  190. *
  191. * @requires jQuery
  192. *
  193. */
  194. $.ajaxSetup({
  195. type : 'POST',
  196. error : function(XMLHttpRequest, textStatus, errorThrown) {
  197. try {
  198. parent.$.messager.progress('close');
  199. parent.$.messager.alert('错误', XMLHttpRequest.responseText);
  200. } catch (e) {
  201. alert(XMLHttpRequest.responseText);
  202. }
  203. }
  204. });
  205. /**
  206. * 解决class="iconImg"的img标记,没有src的时候,会出现边框问题
  207. *
  208. * @author Jeffrey
  209. *
  210. * @requires jQuery
  211. */
  212. $(function() {
  213. $('.iconImg').attr('src', sy.pixel_0);
  214. });
  215. //对Date的扩展,将 Date 转化为指定格式的String
  216. //月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
  217. //年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  218. //例子:
  219. //(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  220. //(new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
  221. Date.prototype.Format = function (fmt) { //author: meizz
  222. var o = {
  223. "M+": this.getMonth() + 1, //月份
  224. "d+": this.getDate(), //日
  225. "h+": this.getHours(), //小时
  226. "m+": this.getMinutes(), //分
  227. "s+": this.getSeconds(), //秒
  228. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  229. "S": this.getMilliseconds() //毫秒
  230. };
  231. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  232. for (var k in o)
  233. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  234. return fmt;
  235. };
  236. jQuery.download = function(url, data, method){
  237. // 获得url和data
  238. if( url && data ){
  239. // data 是 string 或者 array/object
  240. data = typeof data == 'string' ? data : decodeURIComponent(jQuery.param(data));
  241. // 把参数组装成 form的 input
  242. var inputs = '';
  243. jQuery.each(data.split('&'), function(){
  244. var pair = this.split('=');
  245. inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />';
  246. });
  247. // request发送请求
  248. jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
  249. .appendTo('body').submit().remove();
  250. };
  251. };