jquery.parser.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * jQuery EasyUI 1.4.4
  3. *
  4. * Copyright (c) 2009-2015 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 - jQuery EasyUI
  12. *
  13. */
  14. (function($){
  15. $.parser = {
  16. auto: true,
  17. onComplete: function(context){},
  18. plugins:['draggable','droppable','resizable','pagination','tooltip',
  19. 'linkbutton','menu','menubutton','splitbutton','switchbutton','progressbar',
  20. 'tree','textbox','filebox','combo','combobox','combotree','combogrid','numberbox','validatebox','searchbox',
  21. 'spinner','numberspinner','timespinner','datetimespinner','calendar','datebox','datetimebox','slider',
  22. 'layout','panel','datagrid','propertygrid','treegrid','datalist','tabs','accordion','window','dialog','form'
  23. ],
  24. parse: function(context){
  25. var aa = [];
  26. for(var i=0; i<$.parser.plugins.length; i++){
  27. var name = $.parser.plugins[i];
  28. var r = $('.easyui-' + name, context);
  29. if (r.length){
  30. if (r[name]){
  31. r[name]();
  32. } else {
  33. aa.push({name:name,jq:r});
  34. }
  35. }
  36. }
  37. if (aa.length && window.easyloader){
  38. var names = [];
  39. for(var i=0; i<aa.length; i++){
  40. names.push(aa[i].name);
  41. }
  42. easyloader.load(names, function(){
  43. for(var i=0; i<aa.length; i++){
  44. var name = aa[i].name;
  45. var jq = aa[i].jq;
  46. jq[name]();
  47. }
  48. $.parser.onComplete.call($.parser, context);
  49. });
  50. } else {
  51. $.parser.onComplete.call($.parser, context);
  52. }
  53. },
  54. parseValue: function(property, value, parent, delta){
  55. delta = delta || 0;
  56. var v = $.trim(String(value||''));
  57. var endchar = v.substr(v.length-1, 1);
  58. if (endchar == '%'){
  59. v = parseInt(v.substr(0, v.length-1));
  60. if (property.toLowerCase().indexOf('width') >= 0){
  61. v = Math.floor((parent.width()-delta) * v / 100.0);
  62. } else {
  63. v = Math.floor((parent.height()-delta) * v / 100.0);
  64. }
  65. } else {
  66. v = parseInt(v) || undefined;
  67. }
  68. return v;
  69. },
  70. /**
  71. * parse options, including standard 'data-options' attribute.
  72. *
  73. * calling examples:
  74. * $.parser.parseOptions(target);
  75. * $.parser.parseOptions(target, ['id','title','width',{fit:'boolean',border:'boolean'},{min:'number'}]);
  76. */
  77. parseOptions: function(target, properties){
  78. var t = $(target);
  79. var options = {};
  80. var s = $.trim(t.attr('data-options'));
  81. if (s){
  82. if (s.substring(0, 1) != '{'){
  83. s = '{' + s + '}';
  84. }
  85. options = (new Function('return ' + s))();
  86. }
  87. $.map(['width','height','left','top','minWidth','maxWidth','minHeight','maxHeight'], function(p){
  88. var pv = $.trim(target.style[p] || '');
  89. if (pv){
  90. if (pv.indexOf('%') == -1){
  91. pv = parseInt(pv) || undefined;
  92. }
  93. options[p] = pv;
  94. }
  95. });
  96. if (properties){
  97. var opts = {};
  98. for(var i=0; i<properties.length; i++){
  99. var pp = properties[i];
  100. if (typeof pp == 'string'){
  101. opts[pp] = t.attr(pp);
  102. } else {
  103. for(var name in pp){
  104. var type = pp[name];
  105. if (type == 'boolean'){
  106. opts[name] = t.attr(name) ? (t.attr(name) == 'true') : undefined;
  107. } else if (type == 'number'){
  108. opts[name] = t.attr(name)=='0' ? 0 : parseFloat(t.attr(name)) || undefined;
  109. }
  110. }
  111. }
  112. }
  113. $.extend(options, opts);
  114. }
  115. return options;
  116. }
  117. };
  118. $(function(){
  119. var d = $('<div style="position:absolute;top:-1000px;width:100px;height:100px;padding:5px"></div>').appendTo('body');
  120. $._boxModel = d.outerWidth()!=100;
  121. d.remove();
  122. d = $('<div style="position:fixed"></div>').appendTo('body');
  123. $._positionFixed = (d.css('position') == 'fixed');
  124. d.remove();
  125. if (!window.easyloader && $.parser.auto){
  126. $.parser.parse();
  127. }
  128. });
  129. /**
  130. * extend plugin to set box model width
  131. */
  132. $.fn._outerWidth = function(width){
  133. if (width == undefined){
  134. if (this[0] == window){
  135. return this.width() || document.body.clientWidth;
  136. }
  137. return this.outerWidth()||0;
  138. }
  139. return this._size('width', width);
  140. };
  141. /**
  142. * extend plugin to set box model height
  143. */
  144. $.fn._outerHeight = function(height){
  145. if (height == undefined){
  146. if (this[0] == window){
  147. return this.height() || document.body.clientHeight;
  148. }
  149. return this.outerHeight()||0;
  150. }
  151. return this._size('height', height);
  152. };
  153. $.fn._scrollLeft = function(left){
  154. if (left == undefined){
  155. return this.scrollLeft();
  156. } else {
  157. return this.each(function(){$(this).scrollLeft(left)});
  158. }
  159. };
  160. $.fn._propAttr = $.fn.prop || $.fn.attr;
  161. $.fn._size = function(options, parent){
  162. if (typeof options == 'string'){
  163. if (options == 'clear'){
  164. return this.each(function(){
  165. $(this).css({width:'',minWidth:'',maxWidth:'',height:'',minHeight:'',maxHeight:''});
  166. });
  167. } else if (options == 'fit'){
  168. return this.each(function(){
  169. _fit(this, this.tagName=='BODY' ? $('body') : $(this).parent(), true);
  170. });
  171. } else if (options == 'unfit'){
  172. return this.each(function(){
  173. _fit(this, $(this).parent(), false);
  174. });
  175. } else {
  176. if (parent == undefined){
  177. return _css(this[0], options);
  178. } else {
  179. return this.each(function(){
  180. _css(this, options, parent);
  181. });
  182. }
  183. }
  184. } else {
  185. return this.each(function(){
  186. parent = parent || $(this).parent();
  187. $.extend(options, _fit(this, parent, options.fit)||{});
  188. var r1 = _setSize(this, 'width', parent, options);
  189. var r2 = _setSize(this, 'height', parent, options);
  190. if (r1 || r2){
  191. $(this).addClass('easyui-fluid');
  192. } else {
  193. $(this).removeClass('easyui-fluid');
  194. }
  195. });
  196. }
  197. function _fit(target, parent, fit){
  198. if (!parent.length){return false;}
  199. var t = $(target)[0];
  200. var p = parent[0];
  201. var fcount = p.fcount || 0;
  202. if (fit){
  203. if (!t.fitted){
  204. t.fitted = true;
  205. p.fcount = fcount + 1;
  206. $(p).addClass('panel-noscroll');
  207. if (p.tagName == 'BODY'){
  208. $('html').addClass('panel-fit');
  209. }
  210. }
  211. return {
  212. width: ($(p).width()||1),
  213. height: ($(p).height()||1)
  214. };
  215. } else {
  216. if (t.fitted){
  217. t.fitted = false;
  218. p.fcount = fcount - 1;
  219. if (p.fcount == 0){
  220. $(p).removeClass('panel-noscroll');
  221. if (p.tagName == 'BODY'){
  222. $('html').removeClass('panel-fit');
  223. }
  224. }
  225. }
  226. return false;
  227. }
  228. }
  229. function _setSize(target, property, parent, options){
  230. var t = $(target);
  231. var p = property;
  232. var p1 = p.substr(0,1).toUpperCase() + p.substr(1);
  233. var min = $.parser.parseValue('min'+p1, options['min'+p1], parent);// || 0;
  234. var max = $.parser.parseValue('max'+p1, options['max'+p1], parent);// || 99999;
  235. var val = $.parser.parseValue(p, options[p], parent);
  236. var fluid = (String(options[p]||'').indexOf('%') >= 0 ? true : false);
  237. if (!isNaN(val)){
  238. var v = Math.min(Math.max(val, min||0), max||99999);
  239. if (!fluid){
  240. options[p] = v;
  241. }
  242. t._size('min'+p1, '');
  243. t._size('max'+p1, '');
  244. t._size(p, v);
  245. } else {
  246. t._size(p, '');
  247. t._size('min'+p1, min);
  248. t._size('max'+p1, max);
  249. }
  250. return fluid || options.fit;
  251. }
  252. function _css(target, property, value){
  253. var t = $(target);
  254. if (value == undefined){
  255. value = parseInt(target.style[property]);
  256. if (isNaN(value)){return undefined;}
  257. if ($._boxModel){
  258. value += getDeltaSize();
  259. }
  260. return value;
  261. } else if (value === ''){
  262. t.css(property, '');
  263. } else {
  264. if ($._boxModel){
  265. value -= getDeltaSize();
  266. if (value < 0){value = 0;}
  267. }
  268. t.css(property, value+'px');
  269. }
  270. function getDeltaSize(){
  271. if (property.toLowerCase().indexOf('width') >= 0){
  272. return t.outerWidth() - t.width();
  273. } else {
  274. return t.outerHeight() - t.height();
  275. }
  276. }
  277. }
  278. };
  279. })(jQuery);
  280. /**
  281. * support for mobile devices
  282. */
  283. (function($){
  284. var longTouchTimer = null;
  285. var dblTouchTimer = null;
  286. var isDblClick = false;
  287. function onTouchStart(e){
  288. if (e.touches.length != 1){return}
  289. if (!isDblClick){
  290. isDblClick = true;
  291. dblClickTimer = setTimeout(function(){
  292. isDblClick = false;
  293. }, 500);
  294. } else {
  295. clearTimeout(dblClickTimer);
  296. isDblClick = false;
  297. fire(e, 'dblclick');
  298. // e.preventDefault();
  299. }
  300. longTouchTimer = setTimeout(function(){
  301. fire(e, 'contextmenu', 3);
  302. }, 1000);
  303. fire(e, 'mousedown');
  304. if ($.fn.draggable.isDragging || $.fn.resizable.isResizing){
  305. e.preventDefault();
  306. }
  307. }
  308. function onTouchMove(e){
  309. if (e.touches.length != 1){return}
  310. if (longTouchTimer){
  311. clearTimeout(longTouchTimer);
  312. }
  313. fire(e, 'mousemove');
  314. if ($.fn.draggable.isDragging || $.fn.resizable.isResizing){
  315. e.preventDefault();
  316. }
  317. }
  318. function onTouchEnd(e){
  319. // if (e.touches.length > 0){return}
  320. if (longTouchTimer){
  321. clearTimeout(longTouchTimer);
  322. }
  323. fire(e, 'mouseup');
  324. if ($.fn.draggable.isDragging || $.fn.resizable.isResizing){
  325. e.preventDefault();
  326. }
  327. }
  328. function fire(e, name, which){
  329. var event = new $.Event(name);
  330. event.pageX = e.changedTouches[0].pageX;
  331. event.pageY = e.changedTouches[0].pageY;
  332. event.which = which || 1;
  333. $(e.target).trigger(event);
  334. }
  335. if (document.addEventListener){
  336. document.addEventListener("touchstart", onTouchStart, true);
  337. document.addEventListener("touchmove", onTouchMove, true);
  338. document.addEventListener("touchend", onTouchEnd, true);
  339. }
  340. })(jQuery);