jquery.parser.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /**
  2. * EasyUI for jQuery 1.5.5.6
  3. *
  4. * Copyright (c) 2009-2018 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the freeware license: http://www.jeasyui.com/license_freeware.php
  7. * To use it on other terms please contact us: info@jeasyui.com
  8. *
  9. */
  10. /**
  11. * parser - EasyUI for jQuery
  12. *
  13. */
  14. (function($){
  15. $.easyui = {
  16. /**
  17. * Get the index of array item, return -1 when the item is not found.
  18. */
  19. indexOfArray: function(a, o, id){
  20. for(var i=0,len=a.length; i<len; i++){
  21. if (id == undefined){
  22. if (a[i] == o){return i;}
  23. } else {
  24. if (a[i][o] == id){return i;}
  25. }
  26. }
  27. return -1;
  28. },
  29. /**
  30. * Remove array item, 'o' parameter can be item object or id field name.
  31. * When 'o' parameter is the id field name, the 'id' parameter is valid.
  32. */
  33. removeArrayItem: function(a, o, id){
  34. if (typeof o == 'string'){
  35. for(var i=0,len=a.length; i<len; i++){
  36. if (a[i][o] == id){
  37. a.splice(i, 1);
  38. return;
  39. }
  40. }
  41. } else {
  42. var index = this.indexOfArray(a,o);
  43. if (index != -1){
  44. a.splice(index, 1);
  45. }
  46. }
  47. },
  48. /**
  49. * Add un-duplicate array item, 'o' parameter is the id field name, if the 'r' object is exists, deny the action.
  50. */
  51. addArrayItem: function(a, o, r){
  52. var index = this.indexOfArray(a, o, r ? r[o] : undefined);
  53. if (index == -1){
  54. a.push(r ? r : o);
  55. } else {
  56. a[index] = r ? r : o;
  57. }
  58. },
  59. getArrayItem: function(a, o, id){
  60. var index = this.indexOfArray(a, o, id);
  61. return index==-1 ? null : a[index];
  62. },
  63. forEach: function(data, deep, callback){
  64. var nodes = [];
  65. for(var i=0; i<data.length; i++){
  66. nodes.push(data[i]);
  67. }
  68. while(nodes.length){
  69. var node = nodes.shift();
  70. if (callback(node) == false){return;}
  71. if (deep && node.children){
  72. for(var i=node.children.length-1; i>=0; i--){
  73. nodes.unshift(node.children[i]);
  74. }
  75. }
  76. }
  77. }
  78. };
  79. $.parser = {
  80. auto: true,
  81. onComplete: function(context){},
  82. plugins:['draggable','droppable','resizable','pagination','tooltip',
  83. 'linkbutton','menu','sidemenu','menubutton','splitbutton','switchbutton','progressbar','radiobutton','checkbox',
  84. 'tree','textbox','passwordbox','maskedbox','filebox','combo','combobox','combotree','combogrid','combotreegrid','tagbox','numberbox','validatebox','searchbox',
  85. 'spinner','numberspinner','timespinner','datetimespinner','calendar','datebox','datetimebox','slider',
  86. 'layout','panel','datagrid','propertygrid','treegrid','datalist','tabs','accordion','window','dialog','form'
  87. ],
  88. parse: function(context){
  89. var aa = [];
  90. for(var i=0; i<$.parser.plugins.length; i++){
  91. var name = $.parser.plugins[i];
  92. var r = $('.easyui-' + name, context);
  93. if (r.length){
  94. if (r[name]){
  95. r.each(function(){
  96. $(this)[name]($.data(this, 'options')||{});
  97. });
  98. } else {
  99. aa.push({name:name,jq:r});
  100. }
  101. }
  102. }
  103. if (aa.length && window.easyloader){
  104. var names = [];
  105. for(var i=0; i<aa.length; i++){
  106. names.push(aa[i].name);
  107. }
  108. easyloader.load(names, function(){
  109. for(var i=0; i<aa.length; i++){
  110. var name = aa[i].name;
  111. var jq = aa[i].jq;
  112. jq.each(function(){
  113. $(this)[name]($.data(this, 'options')||{});
  114. });
  115. }
  116. $.parser.onComplete.call($.parser, context);
  117. });
  118. } else {
  119. $.parser.onComplete.call($.parser, context);
  120. }
  121. },
  122. parseValue: function(property, value, parent, delta){
  123. delta = delta || 0;
  124. var v = $.trim(String(value||''));
  125. var endchar = v.substr(v.length-1, 1);
  126. if (endchar == '%'){
  127. v = parseFloat(v.substr(0, v.length-1));
  128. if (property.toLowerCase().indexOf('width') >= 0){
  129. v = Math.floor((parent.width()-delta) * v / 100.0);
  130. } else {
  131. v = Math.floor((parent.height()-delta) * v / 100.0);
  132. }
  133. } else {
  134. v = parseInt(v) || undefined;
  135. }
  136. return v;
  137. },
  138. /**
  139. * parse options, including standard 'data-options' attribute.
  140. *
  141. * calling examples:
  142. * $.parser.parseOptions(target);
  143. * $.parser.parseOptions(target, ['id','title','width',{fit:'boolean',border:'boolean'},{min:'number'}]);
  144. */
  145. parseOptions: function(target, properties){
  146. var t = $(target);
  147. var options = {};
  148. var s = $.trim(t.attr('data-options'));
  149. if (s){
  150. if (s.substring(0, 1) != '{'){
  151. s = '{' + s + '}';
  152. }
  153. options = (new Function('return ' + s))();
  154. }
  155. $.map(['width','height','left','top','minWidth','maxWidth','minHeight','maxHeight'], function(p){
  156. var pv = $.trim(target.style[p] || '');
  157. if (pv){
  158. if (pv.indexOf('%') == -1){
  159. pv = parseInt(pv);
  160. if (isNaN(pv)){
  161. pv = undefined;
  162. }
  163. }
  164. options[p] = pv;
  165. }
  166. });
  167. if (properties){
  168. var opts = {};
  169. for(var i=0; i<properties.length; i++){
  170. var pp = properties[i];
  171. if (typeof pp == 'string'){
  172. opts[pp] = t.attr(pp);
  173. } else {
  174. for(var name in pp){
  175. var type = pp[name];
  176. if (type == 'boolean'){
  177. opts[name] = t.attr(name) ? (t.attr(name) == 'true') : undefined;
  178. } else if (type == 'number'){
  179. opts[name] = t.attr(name)=='0' ? 0 : parseFloat(t.attr(name)) || undefined;
  180. }
  181. }
  182. }
  183. }
  184. $.extend(options, opts);
  185. }
  186. return options;
  187. }
  188. };
  189. $(function(){
  190. var d = $('<div style="position:absolute;top:-1000px;width:100px;height:100px;padding:5px"></div>').appendTo('body');
  191. $._boxModel = d.outerWidth()!=100;
  192. d.remove();
  193. d = $('<div style="position:fixed"></div>').appendTo('body');
  194. $._positionFixed = (d.css('position') == 'fixed');
  195. d.remove();
  196. if (!window.easyloader && $.parser.auto){
  197. $.parser.parse();
  198. }
  199. });
  200. /**
  201. * extend plugin to set box model width
  202. */
  203. $.fn._outerWidth = function(width){
  204. if (width == undefined){
  205. if (this[0] == window){
  206. return this.width() || document.body.clientWidth;
  207. }
  208. return this.outerWidth()||0;
  209. }
  210. return this._size('width', width);
  211. };
  212. /**
  213. * extend plugin to set box model height
  214. */
  215. $.fn._outerHeight = function(height){
  216. if (height == undefined){
  217. if (this[0] == window){
  218. return this.height() || document.body.clientHeight;
  219. }
  220. return this.outerHeight()||0;
  221. }
  222. return this._size('height', height);
  223. };
  224. $.fn._scrollLeft = function(left){
  225. if (left == undefined){
  226. return this.scrollLeft();
  227. } else {
  228. return this.each(function(){$(this).scrollLeft(left)});
  229. }
  230. };
  231. $.fn._propAttr = $.fn.prop || $.fn.attr;
  232. $.fn._size = function(options, parent){
  233. if (typeof options == 'string'){
  234. if (options == 'clear'){
  235. return this.each(function(){
  236. $(this).css({width:'',minWidth:'',maxWidth:'',height:'',minHeight:'',maxHeight:''});
  237. });
  238. } else if (options == 'fit'){
  239. return this.each(function(){
  240. _fit(this, this.tagName=='BODY' ? $('body') : $(this).parent(), true);
  241. });
  242. } else if (options == 'unfit'){
  243. return this.each(function(){
  244. _fit(this, $(this).parent(), false);
  245. });
  246. } else {
  247. if (parent == undefined){
  248. return _css(this[0], options);
  249. } else {
  250. return this.each(function(){
  251. _css(this, options, parent);
  252. });
  253. }
  254. }
  255. } else {
  256. return this.each(function(){
  257. parent = parent || $(this).parent();
  258. $.extend(options, _fit(this, parent, options.fit)||{});
  259. var r1 = _setSize(this, 'width', parent, options);
  260. var r2 = _setSize(this, 'height', parent, options);
  261. if (r1 || r2){
  262. $(this).addClass('easyui-fluid');
  263. } else {
  264. $(this).removeClass('easyui-fluid');
  265. }
  266. });
  267. }
  268. function _fit(target, parent, fit){
  269. if (!parent.length){return false;}
  270. var t = $(target)[0];
  271. var p = parent[0];
  272. var fcount = p.fcount || 0;
  273. if (fit){
  274. if (!t.fitted){
  275. t.fitted = true;
  276. p.fcount = fcount + 1;
  277. $(p).addClass('panel-noscroll');
  278. if (p.tagName == 'BODY'){
  279. $('html').addClass('panel-fit');
  280. }
  281. }
  282. return {
  283. width: ($(p).width()||1),
  284. height: ($(p).height()||1)
  285. };
  286. } else {
  287. if (t.fitted){
  288. t.fitted = false;
  289. p.fcount = fcount - 1;
  290. if (p.fcount == 0){
  291. $(p).removeClass('panel-noscroll');
  292. if (p.tagName == 'BODY'){
  293. $('html').removeClass('panel-fit');
  294. }
  295. }
  296. }
  297. return false;
  298. }
  299. }
  300. function _setSize(target, property, parent, options){
  301. var t = $(target);
  302. var p = property;
  303. var p1 = p.substr(0,1).toUpperCase() + p.substr(1);
  304. var min = $.parser.parseValue('min'+p1, options['min'+p1], parent);// || 0;
  305. var max = $.parser.parseValue('max'+p1, options['max'+p1], parent);// || 99999;
  306. var val = $.parser.parseValue(p, options[p], parent);
  307. var fluid = (String(options[p]||'').indexOf('%') >= 0 ? true : false);
  308. if (!isNaN(val)){
  309. var v = Math.min(Math.max(val, min||0), max||99999);
  310. if (!fluid){
  311. options[p] = v;
  312. }
  313. t._size('min'+p1, '');
  314. t._size('max'+p1, '');
  315. t._size(p, v);
  316. } else {
  317. t._size(p, '');
  318. t._size('min'+p1, min);
  319. t._size('max'+p1, max);
  320. }
  321. return fluid || options.fit;
  322. }
  323. function _css(target, property, value){
  324. var t = $(target);
  325. if (value == undefined){
  326. value = parseInt(target.style[property]);
  327. if (isNaN(value)){return undefined;}
  328. if ($._boxModel){
  329. value += getDeltaSize();
  330. }
  331. return value;
  332. } else if (value === ''){
  333. t.css(property, '');
  334. } else {
  335. if ($._boxModel){
  336. value -= getDeltaSize();
  337. if (value < 0){value = 0;}
  338. }
  339. t.css(property, value+'px');
  340. }
  341. function getDeltaSize(){
  342. if (property.toLowerCase().indexOf('width') >= 0){
  343. return t.outerWidth() - t.width();
  344. } else {
  345. return t.outerHeight() - t.height();
  346. }
  347. }
  348. }
  349. };
  350. })(jQuery);
  351. /**
  352. * support for mobile devices
  353. */
  354. (function($){
  355. var longTouchTimer = null;
  356. var dblTouchTimer = null;
  357. var isDblClick = false;
  358. function onTouchStart(e){
  359. if (e.touches.length != 1){return}
  360. if (!isDblClick){
  361. isDblClick = true;
  362. dblClickTimer = setTimeout(function(){
  363. isDblClick = false;
  364. }, 500);
  365. } else {
  366. clearTimeout(dblClickTimer);
  367. isDblClick = false;
  368. fire(e, 'dblclick');
  369. // e.preventDefault();
  370. }
  371. longTouchTimer = setTimeout(function(){
  372. fire(e, 'contextmenu', 3);
  373. }, 1000);
  374. fire(e, 'mousedown');
  375. if ($.fn.draggable.isDragging || $.fn.resizable.isResizing){
  376. e.preventDefault();
  377. }
  378. }
  379. function onTouchMove(e){
  380. if (e.touches.length != 1){return}
  381. if (longTouchTimer){
  382. clearTimeout(longTouchTimer);
  383. }
  384. fire(e, 'mousemove');
  385. if ($.fn.draggable.isDragging || $.fn.resizable.isResizing){
  386. e.preventDefault();
  387. }
  388. }
  389. function onTouchEnd(e){
  390. // if (e.touches.length > 0){return}
  391. if (longTouchTimer){
  392. clearTimeout(longTouchTimer);
  393. }
  394. fire(e, 'mouseup');
  395. if ($.fn.draggable.isDragging || $.fn.resizable.isResizing){
  396. e.preventDefault();
  397. }
  398. }
  399. function fire(e, name, which){
  400. var event = new $.Event(name);
  401. event.pageX = e.changedTouches[0].pageX;
  402. event.pageY = e.changedTouches[0].pageY;
  403. event.which = which || 1;
  404. $(e.target).trigger(event);
  405. }
  406. if (document.addEventListener){
  407. document.addEventListener("touchstart", onTouchStart, true);
  408. document.addEventListener("touchmove", onTouchMove, true);
  409. document.addEventListener("touchend", onTouchEnd, true);
  410. }
  411. })(jQuery);