212fb531e7afba5c280c21b99065894bf106eeb1.svn-base 11 KB

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