extend.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //自定义validate规则
  2. $.extend($.fn.validatebox.defaults.rules, {
  3. //最大长度
  4. maxLength: {
  5. validator: function(value, param){
  6. return value.length <= param[0];
  7. },
  8. message: '您输入的文字太多了! '
  9. },
  10. picUrl: {
  11. validator: function(value, param){
  12. console.log(value)
  13. return /^http:\/\/.*?\/.*?\.(jpg|png|gif|jpeg)/i.test(value.toLowerCase());
  14. },
  15. message: '必须是上传的图片地址哦'
  16. },
  17. //最小长度
  18. minLength: {
  19. validator: function(value, param){
  20. return value.length >= param[0];
  21. },
  22. message: '您输入的文字太少了! '
  23. },
  24. mobile: {
  25. validator: function(value){
  26. return /^(13|15|18)\d{9}$/i.test(value);
  27. },
  28. message: '手机号码格式不正确! '
  29. },
  30. domainSeo: {
  31. validator: function(value){
  32. return /^[\w]+\.([\w]+\.)*((me)|(co)|(am)|(ca)|(com)|(net)|(org)|(gov\.cn)|(info)|(cc)|(com\.cn)|(net\.cn)|(org\.cn)|(name)|(biz)|(tv)|(cn)|(mobi)|(name)|(sh)|(ac)|(io)|(tw)|(com\.tw)|(hk)|(com\.hk)|(ws)|(travel)|(us)|(tm)|(la)|(me\.uk)|(org\.uk)|(ltd\.uk)|(plc\.uk)|(in)|(eu)|(it)|(jp))$/.test(value);
  33. },
  34. message: '请输入正确的域名! '
  35. },
  36. htmlColor: {
  37. validator: function(value){
  38. return /^\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(value);
  39. },
  40. message: '请输入正确的颜色名.'
  41. },
  42. equals: {
  43. validator: function(value,param){
  44. return value == $(param[0]).val();
  45. },
  46. message: '两次输入的内容不匹配.'
  47. }
  48. });
  49. $.extend($.fn.datagrid.methods, {
  50. //datagrid反选
  51. reverseSelect: function(jq){
  52. return jq.each(function(){
  53. var select_rows = $(this).datagrid('getSelections');
  54. var select_index;
  55. var now_index;
  56. var select_index_arr = [];
  57. for (var X in select_rows){
  58. select_index = $(this).datagrid('getRowIndex', select_rows[X]);
  59. select_index_arr.push(select_index);
  60. }
  61. var all_rows = $(this).datagrid('getRows');
  62. for (var X in all_rows){
  63. now_index = $(this).datagrid('getRowIndex', all_rows[X]);
  64. if ($.inArray(now_index, select_index_arr) == -1){
  65. $(this).datagrid('selectRow', now_index);
  66. } else {
  67. $(this).datagrid('unselectRow', now_index);
  68. }
  69. }
  70. })
  71. },
  72. //保持当前的选择状态
  73. keepSelect: function(jq){
  74. return jq.each(function(){
  75. var self = $(this);
  76. var select_rows = self.datagrid('getSelections');
  77. var select_index;
  78. var now_index;
  79. var select_index_arr = [];
  80. for (var X in select_rows){
  81. select_index = self.datagrid('getRowIndex', select_rows[X]);
  82. select_index_arr.push(select_index);
  83. }
  84. setTimeout(function(){
  85. self.datagrid('unselectAll');
  86. for (var Y in select_index_arr){
  87. self.datagrid('selectRow', select_index_arr[Y]);
  88. }
  89. }, 100);
  90. })
  91. },
  92. //增加编辑器
  93. addEditor : function(jq, param) {
  94. if (param instanceof Array) {
  95. $.each(param, function(index, item) {
  96. var e = $(jq).datagrid('getColumnOption', item.field);
  97. e.editor = item.editor;
  98. });
  99. } else {
  100. var e = $(jq).datagrid('getColumnOption', param.field);
  101. e.editor = param.editor;
  102. }
  103. },
  104. //移除编辑器
  105. removeEditor : function(jq, param) {
  106. if (param == null){
  107. param = $(jq).datagrid('getColumnFields');
  108. }
  109. if (param instanceof Array) {
  110. $.each(param, function(index, item) {
  111. var e = $(jq).datagrid('getColumnOption', item);
  112. e.editor = {};
  113. });
  114. } else {
  115. var e = $(jq).datagrid('getColumnOption', param);
  116. e.editor = {};
  117. }
  118. }
  119. });
  120. $.extend($.fn.datagrid.defaults.editors, {
  121. timespinner: {
  122. init: function(container, options){
  123. var input = $('<input class="easyui-timespinner">').appendTo(container);
  124. options.formatter = function(time){
  125. return new Date(time).format("hh:mm");
  126. };
  127. return input.timespinner(options);
  128. },
  129. getValue: function(target){
  130. return $(target).timespinner('getValue');
  131. },
  132. setValue: function(target, value){
  133. $(target).timespinner('setValue', value);
  134. },
  135. resize: function(target, width){
  136. $(target).timespinner('resize', width);
  137. }
  138. },
  139. combotree: {
  140. init: function(container, options){
  141. var input = $('<input class="easyui-combotree">').appendTo(container);
  142. return input.combotree(options);
  143. },
  144. getValue: function(target){
  145. var values = $(target).combotree('getValues');
  146. var real_values = [];
  147. for (var x = 0,len = values.length;x < len;x ++){
  148. if (values[x] && values[x] != ''){
  149. real_values.push(values[x]);
  150. }
  151. }
  152. return real_values.join(',');
  153. },
  154. setValue: function(target, value){
  155. setTimeout(function(){
  156. var values = value.split(',');
  157. $(target).combotree('setValues', values);
  158. var tree = $(target).combotree('tree');
  159. var nodes = tree.tree('getChecked');
  160. var parent;
  161. for (var x = 0,len = nodes.length;x < len;x ++){
  162. parent = tree.tree('getParent', nodes[x]['target']);
  163. while(parent != null){
  164. tree.tree('expand', parent.target);
  165. parent = tree.tree('getParent', parent['target']);
  166. }
  167. }
  168. }, 100);
  169. },
  170. resize: function(target, width){
  171. $(target).combotree('resize', width);
  172. }
  173. },
  174. combobox: {
  175. init: function(container, options){
  176. var input = $('<input class="easyui-combobox">').appendTo(container);
  177. return input.combobox(options);
  178. },
  179. getValue : function(jq) {
  180. var opts = $(jq).combobox('options');
  181. if(opts.multiple){
  182. var values = $(jq).combobox('getValues');
  183. if(values.length>0){
  184. if(values[0]==''||values[0]==' '){
  185. return values.join(',').substring(1);
  186. }
  187. }
  188. return values.join(',');
  189. }
  190. else
  191. return $(jq).combobox("getValue");
  192. },
  193. setValue : function(jq, value) {
  194. var opts = $(jq).combobox('options');
  195. if(opts.multiple&&value.indexOf(opts.separator)!=-1){//多选且不只一个值
  196. var values = value.split(opts.separator);
  197. $(jq).combobox("setValues", values);
  198. }
  199. else
  200. $(jq).combobox("setValue", value);
  201. },
  202. resize: function(target, width){
  203. $(target).combobox('resize', width);
  204. }
  205. },
  206. datetimebox: {
  207. init: function(container, options){
  208. var input = $('<input class="easyui-datetimebox">').appendTo(container);
  209. return input.datetimebox(options);
  210. },
  211. getValue : function(jq) {
  212. return $(jq).datetimebox("getValue");
  213. },
  214. setValue : function(jq, value) {
  215. $(jq).datetimebox("setValue", value);
  216. },
  217. resize: function(target, width){
  218. $(target).datetimebox('resize', width);
  219. }
  220. },
  221. uploadbox: {
  222. init: function(container, options){
  223. var input = $('<input class="easyui-validatebox" readonly=true>').appendTo(container);
  224. return input.validatebox(options);
  225. },
  226. destroy: function(target){
  227. $(target).remove();
  228. },
  229. getValue : function(target) {
  230. return $(target).val();
  231. },
  232. setValue : function(target, value) {
  233. $(target).val(value);
  234. },
  235. resize: function(target, width){
  236. $(target)._outerWidth(width);
  237. }
  238. }
  239. });
  240. /**
  241. * 将输入框的foucs和blur效果整合
  242. * params: f_color: focus的颜色, b_color: blur的颜色, prompt: 提示的文字
  243. */
  244. (function($){
  245. $.fn.extend({
  246. focus_and_blur: function(options){
  247. var opts = $.extend({}, $.fn.focus_and_blur.defaults, options);
  248. return this.each(function(){
  249. $(this).val(options.prompt);
  250. $(this).focus(function(){
  251. $(this).css('background-color', opts.f_color);
  252. var text = $(this).val();
  253. if (text == options.prompt){
  254. $(this).val('');
  255. }
  256. });
  257. $(this).blur(function(){
  258. $(this).css('background-color', opts.b_color);
  259. var text = $(this).val();
  260. if (text == ''){
  261. $(this).val(opts.prompt);
  262. }
  263. });
  264. })
  265. }
  266. });
  267. $.fn.focus_and_blur.defaults = {
  268. f_color: '#FFFFCC',
  269. b_color: 'white',
  270. prompt: ''
  271. };
  272. })(jQuery);
  273. (function($){
  274. $.extend({
  275. cookie : function (key, value, options) {
  276. // key and value given, set cookie...
  277. if (arguments.length > 1 && (value === null || typeof value !== "object")) {
  278. options = jQuery.extend({}, options);
  279. if (value === null) {
  280. options.expires = -1;
  281. }
  282. if (typeof options.expires === 'number') {
  283. var days = options.expires, t = options.expires = new Date();
  284. t.setDate(t.getDate() + days);
  285. }
  286. return (document.cookie = [
  287. encodeURIComponent(key), '=',
  288. options.raw ? String(value) : encodeURIComponent(String(value)),
  289. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  290. options.path ? '; path=' + options.path : '',
  291. options.domain ? '; domain=' + options.domain : '',
  292. options.secure ? '; secure' : ''
  293. ].join(''));
  294. }
  295. // key and possibly options given, get cookie...
  296. options = value || {};
  297. var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
  298. return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
  299. }
  300. })
  301. })(jQuery);