PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/jeasyui/src/jquery.window.js

https://gitlab.com/deesoft/yii2-biz3distro
JavaScript | 376 lines | 308 code | 22 blank | 46 comment | 55 complexity | db0ce0ff8205c7d95c350e2fb3510a50 MD5 | raw file
  1. /**
  2. * jQuery EasyUI 1.4.5
  3. *
  4. * Copyright (c) 2009-2016 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. * window - jQuery EasyUI
  12. *
  13. * Dependencies:
  14. * panel
  15. * draggable
  16. * resizable
  17. *
  18. */
  19. (function($){
  20. function moveWindow(target, param){
  21. var state = $.data(target, 'window');
  22. if (param){
  23. if (param.left != null) state.options.left = param.left;
  24. if (param.top != null) state.options.top = param.top;
  25. }
  26. $(target).panel('move', state.options);
  27. if (state.shadow){
  28. state.shadow.css({
  29. left: state.options.left,
  30. top: state.options.top
  31. });
  32. }
  33. }
  34. /**
  35. * center the window only horizontally
  36. */
  37. function hcenter(target, tomove){
  38. var opts = $.data(target, 'window').options;
  39. var pp = $(target).window('panel');
  40. var width = pp._outerWidth();
  41. if (opts.inline){
  42. var parent = pp.parent();
  43. opts.left = Math.ceil((parent.width() - width) / 2 + parent.scrollLeft());
  44. } else {
  45. opts.left = Math.ceil(($(window)._outerWidth() - width) / 2 + $(document).scrollLeft());
  46. }
  47. if (tomove){moveWindow(target);}
  48. }
  49. /**
  50. * center the window only vertically
  51. */
  52. function vcenter(target, tomove){
  53. var opts = $.data(target, 'window').options;
  54. var pp = $(target).window('panel');
  55. var height = pp._outerHeight();
  56. if (opts.inline){
  57. var parent = pp.parent();
  58. opts.top = Math.ceil((parent.height() - height) / 2 + parent.scrollTop());
  59. } else {
  60. opts.top = Math.ceil(($(window)._outerHeight() - height) / 2 + $(document).scrollTop());
  61. }
  62. if (tomove){moveWindow(target);}
  63. }
  64. function create(target){
  65. var state = $.data(target, 'window');
  66. var opts = state.options;
  67. var win = $(target).panel($.extend({}, state.options, {
  68. border: false,
  69. doSize: true, // size the panel, the property undefined in window component
  70. closed: true, // close the panel
  71. cls: 'window ' + (!opts.border?'window-thinborder window-noborder ':(opts.border=='thin'?'window-thinborder ':'')) + (opts.cls || ''),
  72. headerCls: 'window-header ' + (opts.headerCls || ''),
  73. bodyCls: 'window-body ' + (opts.noheader ? 'window-body-noheader ' : ' ') + (opts.bodyCls||''),
  74. onBeforeDestroy: function(){
  75. if (opts.onBeforeDestroy.call(target) == false){return false;}
  76. if (state.shadow){state.shadow.remove();}
  77. if (state.mask){state.mask.remove();}
  78. },
  79. onClose: function(){
  80. if (state.shadow){state.shadow.hide();}
  81. if (state.mask){state.mask.hide();}
  82. opts.onClose.call(target);
  83. },
  84. onOpen: function(){
  85. if (state.mask){
  86. state.mask.css($.extend({
  87. display:'block',
  88. zIndex: $.fn.window.defaults.zIndex++
  89. }, $.fn.window.getMaskSize(target)));
  90. }
  91. if (state.shadow){
  92. state.shadow.css({
  93. display:'block',
  94. zIndex: $.fn.window.defaults.zIndex++,
  95. left: opts.left,
  96. top: opts.top,
  97. width: state.window._outerWidth(),
  98. height: state.window._outerHeight()
  99. });
  100. }
  101. state.window.css('z-index', $.fn.window.defaults.zIndex++);
  102. opts.onOpen.call(target);
  103. },
  104. onResize: function(width, height){
  105. var popts = $(this).panel('options');
  106. $.extend(opts, {
  107. width: popts.width,
  108. height: popts.height,
  109. left: popts.left,
  110. top: popts.top
  111. });
  112. if (state.shadow){
  113. state.shadow.css({
  114. left: opts.left,
  115. top: opts.top,
  116. width: state.window._outerWidth(),
  117. height: state.window._outerHeight()
  118. });
  119. }
  120. opts.onResize.call(target, width, height);
  121. },
  122. onMinimize: function(){
  123. if (state.shadow){state.shadow.hide();}
  124. if (state.mask){state.mask.hide();}
  125. state.options.onMinimize.call(target);
  126. },
  127. onBeforeCollapse: function(){
  128. if (opts.onBeforeCollapse.call(target) == false){return false;}
  129. if (state.shadow){state.shadow.hide();}
  130. },
  131. onExpand: function(){
  132. if (state.shadow){state.shadow.show();}
  133. opts.onExpand.call(target);
  134. }
  135. }));
  136. state.window = win.panel('panel');
  137. // create mask
  138. if (state.mask){state.mask.remove();}
  139. if (opts.modal){
  140. state.mask = $('<div class="window-mask" style="display:none"></div>').insertAfter(state.window);
  141. }
  142. // create shadow
  143. if (state.shadow){state.shadow.remove();}
  144. if (opts.shadow){
  145. state.shadow = $('<div class="window-shadow" style="display:none"></div>').insertAfter(state.window);
  146. }
  147. // center and open the window
  148. var closed = opts.closed;
  149. if (opts.left == null){hcenter(target);}
  150. if (opts.top == null){vcenter(target);}
  151. moveWindow(target);
  152. if (!closed){win.window('open');}
  153. }
  154. /**
  155. * set window drag and resize property
  156. */
  157. function setProperties(target){
  158. var state = $.data(target, 'window');
  159. state.window.draggable({
  160. handle: '>div.panel-header>div.panel-title',
  161. disabled: state.options.draggable == false,
  162. onBeforeDrag: function(e){
  163. if (state.mask) state.mask.css('z-index', $.fn.window.defaults.zIndex++);
  164. if (state.shadow) state.shadow.css('z-index', $.fn.window.defaults.zIndex++);
  165. state.window.css('z-index', $.fn.window.defaults.zIndex++);
  166. },
  167. onStartDrag: function(e){
  168. if (!state.proxy){
  169. state.proxy = $('<div class="window-proxy"></div>').insertAfter(state.window);
  170. }
  171. state.proxy.css({
  172. display:'none',
  173. zIndex: $.fn.window.defaults.zIndex++,
  174. left: e.data.left,
  175. top: e.data.top
  176. });
  177. state.proxy._outerWidth(state.window._outerWidth());
  178. state.proxy._outerHeight(state.window._outerHeight());
  179. setTimeout(function(){
  180. if (state.proxy) state.proxy.show();
  181. }, 500);
  182. },
  183. onDrag: function(e){
  184. state.proxy.css({
  185. display:'block',
  186. left: e.data.left,
  187. top: e.data.top
  188. });
  189. return false;
  190. },
  191. onStopDrag: function(e){
  192. state.options.left = e.data.left;
  193. state.options.top = e.data.top;
  194. $(target).window('move');
  195. state.proxy.remove();
  196. state.proxy = null;
  197. }
  198. });
  199. state.window.resizable({
  200. disabled: state.options.resizable == false,
  201. onStartResize:function(e){
  202. if (state.pmask){state.pmask.remove();}
  203. state.pmask = $('<div class="window-proxy-mask"></div>').insertAfter(state.window);
  204. state.pmask.css({
  205. zIndex: $.fn.window.defaults.zIndex++,
  206. left: e.data.left,
  207. top: e.data.top,
  208. width: state.window._outerWidth(),
  209. height: state.window._outerHeight()
  210. });
  211. if (state.proxy){state.proxy.remove();}
  212. state.proxy = $('<div class="window-proxy"></div>').insertAfter(state.window);
  213. state.proxy.css({
  214. zIndex: $.fn.window.defaults.zIndex++,
  215. left: e.data.left,
  216. top: e.data.top
  217. });
  218. state.proxy._outerWidth(e.data.width)._outerHeight(e.data.height);
  219. },
  220. onResize: function(e){
  221. state.proxy.css({
  222. left: e.data.left,
  223. top: e.data.top
  224. });
  225. state.proxy._outerWidth(e.data.width);
  226. state.proxy._outerHeight(e.data.height);
  227. return false;
  228. },
  229. onStopResize: function(e){
  230. $(target).window('resize', e.data);
  231. state.pmask.remove();
  232. state.pmask = null;
  233. state.proxy.remove();
  234. state.proxy = null;
  235. }
  236. });
  237. }
  238. // function getPageArea() {
  239. // if (document.compatMode == 'BackCompat') {
  240. // return {
  241. // width: Math.max(document.body.scrollWidth, document.body.clientWidth),
  242. // height: Math.max(document.body.scrollHeight, document.body.clientHeight)
  243. // }
  244. // } else {
  245. // return {
  246. // width: Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth),
  247. // height: Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight)
  248. // }
  249. // }
  250. // }
  251. // when window resize, reset the width and height of the window's mask
  252. $(window).resize(function(){
  253. $('body>div.window-mask').css({
  254. width: $(window)._outerWidth(),
  255. height: $(window)._outerHeight()
  256. });
  257. setTimeout(function(){
  258. $('body>div.window-mask').css($.fn.window.getMaskSize());
  259. }, 50);
  260. });
  261. $.fn.window = function(options, param){
  262. if (typeof options == 'string'){
  263. var method = $.fn.window.methods[options];
  264. if (method){
  265. return method(this, param);
  266. } else {
  267. return this.panel(options, param);
  268. }
  269. }
  270. options = options || {};
  271. return this.each(function(){
  272. var state = $.data(this, 'window');
  273. if (state){
  274. $.extend(state.options, options);
  275. } else {
  276. state = $.data(this, 'window', {
  277. options: $.extend({}, $.fn.window.defaults, $.fn.window.parseOptions(this), options)
  278. });
  279. if (!state.options.inline){
  280. document.body.appendChild(this);
  281. }
  282. }
  283. create(this);
  284. setProperties(this);
  285. });
  286. };
  287. $.fn.window.methods = {
  288. options: function(jq){
  289. var popts = jq.panel('options');
  290. var wopts = $.data(jq[0], 'window').options;
  291. return $.extend(wopts, {
  292. closed: popts.closed,
  293. collapsed: popts.collapsed,
  294. minimized: popts.minimized,
  295. maximized: popts.maximized
  296. });
  297. },
  298. window: function(jq){
  299. return $.data(jq[0], 'window').window;
  300. },
  301. move: function(jq, param){
  302. return jq.each(function(){
  303. moveWindow(this, param);
  304. });
  305. },
  306. hcenter: function(jq){
  307. return jq.each(function(){
  308. hcenter(this, true);
  309. });
  310. },
  311. vcenter: function(jq){
  312. return jq.each(function(){
  313. vcenter(this, true);
  314. });
  315. },
  316. center: function(jq){
  317. return jq.each(function(){
  318. hcenter(this);
  319. vcenter(this);
  320. moveWindow(this);
  321. });
  322. }
  323. };
  324. $.fn.window.getMaskSize = function(target){
  325. var state = $(target).data('window');
  326. var inline = (state && state.options.inline);
  327. return {
  328. width: (inline ? '100%' : $(document).width()),
  329. height: (inline ? '100%' : $(document).height())
  330. };
  331. };
  332. $.fn.window.parseOptions = function(target){
  333. return $.extend({}, $.fn.panel.parseOptions(target), $.parser.parseOptions(target, [
  334. {draggable:'boolean',resizable:'boolean',shadow:'boolean',modal:'boolean',inline:'boolean'}
  335. ]));
  336. };
  337. // Inherited from $.fn.panel.defaults
  338. $.fn.window.defaults = $.extend({}, $.fn.panel.defaults, {
  339. zIndex: 9000,
  340. draggable: true,
  341. resizable: true,
  342. shadow: true,
  343. modal: false,
  344. border: true, // possible values are: true,false,'thin','thick'
  345. inline: false, // true to stay inside its parent, false to go on top of all elements
  346. // window's property which difference from panel
  347. title: 'New Window',
  348. collapsible: true,
  349. minimizable: true,
  350. maximizable: true,
  351. closable: true,
  352. closed: false
  353. });
  354. })(jQuery);