dialog.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /**
  2. * Window for Youth - (1.2 base jQuery), also need jquery.myext.js
  3. *
  4. * please reserving all of the comment, otherwise you should not use those code
  5. *
  6. * @Author muqiao
  7. * @Version 1.2
  8. * @Copyright 2008 (c) muqiao (http://hi.baidu.com/emkiao)
  9. * @Revision $Id: window_src.js 3 2008-07-10 15:47:40Z emkiao@qq.com $
  10. */
  11. Array.prototype.concat || (Array.prototype.concat = function(){
  12. // specially for opera
  13. var arr = [];
  14. for(var i = 0, l = this.length; i < l; i++) arr.push(this[i]);
  15. for(var i = 0, l = arguments.length; i < l; i++){
  16. if(typeof arguments[i] == 'undefined') continue;
  17. if(arguments[i].constructor == Array){
  18. for(var j = 0, ll = arguments[i].length; j < ll; j++){
  19. arr.push(arguments[i][j]);
  20. }
  21. }else{
  22. arr.push(arguments[i]);
  23. }
  24. }
  25. return arr;
  26. });
  27. (function($){
  28. $.fn.extend({
  29. getDimensions: function(){
  30. var el = this[0];
  31. var display = this.css('display');
  32. if (display != 'none' && display != null) // Safari bug
  33. return {width: el.offsetWidth, height: el.offsetHeight};
  34. var els = el.style, oV = els.visibility, oP = els.position, oD = els.display;
  35. els.visibility = 'hidden';
  36. els.position = 'absolute';
  37. els.display = 'block';
  38. var oW = el.clientWidth, oH = el.clientHeight;
  39. els.display = oD;
  40. els.position = oP;
  41. els.visibility = oV;
  42. return {width: oW, height: oH};
  43. }
  44. });
  45. $.extend({
  46. toFloat:function(obj){
  47. obj = parseFloat(obj);
  48. isNaN(obj) && (obj = arguments[1]||0);
  49. return obj;
  50. },
  51. /**
  52. * parseInt失败时返回 0
  53. */
  54. toInt:function(obj){
  55. obj = parseInt(obj);
  56. isNaN(obj) && (obj = arguments[1]||0);
  57. return obj;
  58. },
  59. /**
  60. * 克隆一个对象
  61. * @param {Object} object
  62. */
  63. clone: function(obj){
  64. var newobj = {};
  65. for(var key in obj){
  66. typeof obj[key] != 'undefined' && (newobj[key] = obj[key]);
  67. }
  68. return newobj;
  69. },
  70. /**
  71. * convert any object to array
  72. * @param {Object} iterable
  73. */
  74. array:function(iterable){
  75. if(!iterable){
  76. return typeof iterable == 'undefined' ? [] : [iterable];
  77. }
  78. if(iterable.constructor == Array){
  79. return iterable;
  80. }
  81. var i = iterable.length, s = [], t;
  82. if(typeof i != 'number' || (t = typeof iterable) && t == 'string' || t == 'function' || iterable.setInterval){
  83. s[0] = iterable;
  84. }else{
  85. while(i){
  86. s[--i] = iterable[i];
  87. }
  88. }
  89. return s;
  90. },
  91. getWindowScroll:function(){
  92. var T, L, W, H,win = window, dom = document.documentElement, doc = document.body;
  93. T = dom && dom.scrollTop || doc && doc.scrollTop || 0;
  94. L = dom && dom.scrollLeft || doc && doc.scrollLeft || 0;
  95. if(win.innerWidth){
  96. W = win.innerWidth;
  97. H = win.innerHeight;
  98. }else{
  99. W = dom && dom.clientWidth || doc && doc.clientWidth;
  100. H = dom && dom.clientHeight || doc && doc.clientHeight;
  101. }
  102. return { top: T, left: L, width: W, height: H };
  103. },
  104. getPageSize:function(){
  105. var windowWidth, windowHeight,
  106. xScroll, yScroll,
  107. win = window, dom = document.documentElement, doc = document.body;
  108. if (win.innerHeight && win.scrollMaxY) {
  109. xScroll = doc.scrollWidth;
  110. yScroll = win.innerHeight + win.scrollMaxY;
  111. }else{
  112. xScroll = Math.max(dom ? dom.scrollWidth : 0,doc.scrollWidth,doc.offsetWidth);
  113. yScroll = Math.max(dom ? dom.scrollHeight : 0,doc.scrollHeight,doc.offsetHeight);
  114. }
  115. if(win.innerHeight){
  116. windowWidth = win.innerWidth;
  117. windowHeight = win.innerHeight;
  118. }else{
  119. windowWidth = dom && dom.clientWidth || doc && doc.clientWidth;
  120. windowHeight = dom && dom.clientHeight || doc && doc.clientHeight;
  121. }
  122. yScroll < windowHeight && (yScroll = windowHeight);
  123. xScroll < windowWidth && (xScroll = windowWidth);
  124. return {pageWidth: xScroll ,pageHeight: yScroll , windowWidth: windowWidth, windowHeight: windowHeight};
  125. },
  126. /**
  127. * 闭包一个函数
  128. * @param {Object} bind 作用域
  129. * @param {Array} args 附加参数
  130. * @example
  131. * function test(param1,param1){
  132. * alert(this.tagName);// this == document.body
  133. * alert('I have arguments:'+param1+', '+param1);
  134. * }
  135. * test.bind(document.body,['param1','param2']);
  136. * test();// result 'BODY'
  137. */
  138. fbind:function(fn,bind,args){
  139. return function(){
  140. return fn.apply(bind||null,$.array(args).concat(arguments));
  141. }
  142. },
  143. /**
  144. * 闭包一个函数作为事件监听程式
  145. * @param {Object} bind 作用域
  146. * @param {Array} args 附加参数
  147. * @example 于上一个函数差不多 只是参数列表里面多一个event对象
  148. * function test(evt,param){
  149. * alert(evt.pageX);
  150. * alert(this);// this == document
  151. * }
  152. * test.bindE(document,['param']);
  153. * $(document).click(test);
  154. * // 单击则 弹出提示
  155. */
  156. fbindE:function(fn,bind,args){
  157. return function(e){
  158. e = $.event.fix(window.event || e || {});
  159. var ret = fn.apply(bind||null,[e].concat(args));
  160. if(typeof ret == 'undefined'){
  161. e.preventDefault();
  162. e.stopPropagation();
  163. }
  164. return ret;
  165. };
  166. }
  167. });
  168. })(jQuery);
  169. (function($){
  170. var namespace = arguments[1];
  171. var IE6 = $.browser.msie && parseInt($.browser.version) <= 7;
  172. var Dialog = window[namespace] = function(){
  173. var optionIndex = 0;
  174. if(arguments.length > 0){
  175. if(typeof arguments[0] == "string" ){
  176. this.id = arguments[0];
  177. optionIndex = 1;
  178. }else{
  179. this.id = arguments[0] ? arguments[0].id : null;
  180. }
  181. }
  182. if(!this.id){
  183. this.id = "window_" + new Date().getTime();
  184. }
  185. if(Dialogs.dialogs[this.id]){
  186. return Dialogs.dialogs[this.id].show();
  187. }
  188. this.options = $.extend($.clone(Dialogs.settings),arguments[optionIndex] || {});
  189. if(document.getElementById(this.id)){
  190. this.options.content = document.getElementById(this.id).innerHTML;
  191. }
  192. this.relchilds = [];
  193. this.parnetwin = null;
  194. Dialogs.relation(this.options.parentId,this);
  195. this.parent = $(document.body);
  196. this.below = null;
  197. this.above = null;
  198. this.dialog = null;
  199. Dialogs.dialogs[this.id] = this;
  200. return this;
  201. };
  202. Dialog.prototype = {
  203. toggle:function(){
  204. return this[this.visible ? 'hide' : 'show'];
  205. },
  206. show:function(show){
  207. if(this.visible) return this.toFront();
  208. this.visible = true;
  209. this.dialog == null && _createWin(this);
  210. var o = this.options,
  211. after = $.fbind(function(){
  212. this.lightbox && this.lightbox.show();
  213. this.toFront();
  214. },this),
  215. gon = true;
  216. typeof o.onShow == 'function' && (gon = o.onShow.call(this)!==false);
  217. if(!gon) return this;
  218. typeof show == 'function' && (show.call(this.dialog,this,after)||true) ||
  219. typeof o.show == 'function' && (o.show.call(this.dialog,this,after)||true) ||
  220. this.dialog.show(1,after);
  221. return this;
  222. },
  223. hide:function(hide){
  224. if(!this.visible) return this;
  225. this.visible = false;
  226. var o = this.options,
  227. after = $.fbind(function(){
  228. this.lightbox && this.lightbox.hide();
  229. },this),
  230. gon = true;
  231. typeof o.onHide == 'function' && (gon = o.onHide.call(this)!==false);
  232. typeof hide == 'function' && (hide.call(this.dialog,this,after)||true) ||
  233. typeof o.hide == 'function' && (o.hide.call(this.dialog,this,after)||true) ||
  234. this.dialog.hide(1,after);
  235. return this;
  236. },
  237. close:function(){
  238. if($.datepicker)
  239. $.datepicker._checkExternalClick('');
  240. var clean = true;
  241. if(!this.options) return;
  242. if(typeof this.options.onClose == 'function'){
  243. clean = this.options.onClose.call(this);
  244. }
  245. if(clean === false) return;
  246. this.dowhat && this.endDrag && this.endDrag();
  247. this.dialog.remove();
  248. if(this.lightbox){
  249. this.lightbox.remove();
  250. }
  251. if(typeof this.options.afterClose=='function'){
  252. this.options.afterClose();
  253. }
  254. if(this.autoposition){
  255. $(window).unbind('scroll',this.autoposition).unbind('resize',this.autoposition);
  256. }
  257. delete Dialogs.dialogs[this.id];
  258. Dialogs.dialogs[this.id] != undefined && (Dialogs.dialogs[this.id] = null);
  259. if(Dialogs.calls[this.id]){
  260. var l = Dialogs.calls[this.id].length;
  261. while(l){
  262. delete Dialogs.calls[this.id][--l];
  263. Dialogs.calls[this.id][l] != undefined && (Dialogs.calls[this.id][l] = null);
  264. }
  265. delete Dialogs.calls[this.id];
  266. Dialogs.calls[this.id] != undefined && (Dialogs.calls[this.id] = null);
  267. }
  268. if(Dialogs.focusedWindow == this){
  269. delete Dialogs.focusedWindow;
  270. Dialogs.focusedWindow = this.below;
  271. }
  272. Dialogs.shift(this);
  273. for(var k in this){
  274. this[k] = null;
  275. }
  276. },
  277. html:function(content){
  278. var o = this.options;
  279. if(content==undefined){
  280. return o.content||'';
  281. }
  282. content==''&& (content = "&nbsp;");
  283. if(this.dialog != null){
  284. if(o.url){
  285. this.content.src = null;
  286. o.url = null;
  287. this.content = $('<div class="dialog_content"> </div>');
  288. $('#'+this.id+'_table_content',this.dialog).empty().append(this.content);
  289. }
  290. this.content.empty().append(content);
  291. o.autosize && this.autoAdaptSize();
  292. o.autocenter && this.center();
  293. try{$('input:visible, textarea',this.content)[0].focus()}catch(e){}
  294. }
  295. o.content = content;
  296. return this;
  297. },
  298. center:function(){
  299. var wsize = $.getWindowScroll();
  300. var top = (wsize.height - (this.height + this.heightN + this.heightS))/2;
  301. var left = (wsize.width - (this.width + this.widthW + this.widthE))/2;
  302. if(!this.posfixed){
  303. top += wsize.top;
  304. left += wsize.left;
  305. }
  306. this.dialog.css({top:top,left:left});
  307. },
  308. load:function(url){
  309. var dialog = this;
  310. $.ajax({// Request the remote document
  311. url: url,
  312. cache:false,
  313. type: 'GET',
  314. dataType: "html",
  315. success: function(data){
  316. dialog.html(data);
  317. },
  318. error:function(){
  319. dialog.html('<font color="red">请求异常</font>');
  320. }
  321. });
  322. return this;
  323. },
  324. status:function(status){
  325. if(status==undefined){
  326. return this.options.status||'';
  327. }
  328. if(status==''){
  329. status = "&nbsp;";
  330. }
  331. if(this.dialog!=null){
  332. $('#'+this.id+'_bottom',this.dialog).empty().append(status);
  333. }
  334. this.options.status = status;
  335. return this;
  336. },
  337. title:function(title){
  338. if(title==undefined){
  339. return this.options.title||'';
  340. }
  341. if(title==''){
  342. title = "&nbsp;";
  343. }
  344. if(this.dialog!=null){
  345. $('#'+this.id+'_top',this.dialog).empty().append(title);
  346. }
  347. this.options.title = title;
  348. return this;
  349. },
  350. href:function(url){
  351. if(url == undefined){
  352. return this.options.url || '';
  353. }
  354. if(url==''){
  355. url = "";
  356. }
  357. if (this.dialog != null) {
  358. if(this.options.url){
  359. this.content.attr('src',url);
  360. }
  361. // Not an url content, change div to iframe
  362. else{
  363. this.content = $("<iframe frameborder='0' name='" + this.id + "_content' src='" + url + "' width='" + this.width + "' height='" + this.height + "'> </iframe>");
  364. $('#' + this.id + "_table_content").empty().append(this.content);
  365. }
  366. }
  367. this.options.url = url;
  368. return this;
  369. },
  370. setSize:function(width,height){
  371. // Check min size
  372. (!width || (!this.minimized && width < this.options.minWidth)) && (width = this.options.minWidth);
  373. (!height || (!this.minimized && height < this.options.minHeight)) && (height = this.options.minHeight);
  374. this.width = width;
  375. this.height = height;
  376. this.dialog.css({width:width+ this.widthW + this.widthE,height:this.height+ this.heightN + this.heightS});
  377. this.content.css({height:height,width:width});
  378. this.masker && this.masker.css('display') !='none' && this.masker.css({width:width ,height:height});
  379. return this;
  380. },
  381. autoAdaptSize:function(){
  382. var w = this.content[0].scrollWidth;
  383. var h = this.content[0].scrollHeight;
  384. if(w==this.width && h==this.hright) return;
  385. this.setSize(w,h);
  386. //{{{ hack get real scrollWidth and scrollHeight
  387. var i = 1;
  388. do{
  389. this.content[0].scrollLeft += 20;
  390. if(this.content[0].scrollLeft < 20*i){
  391. break;
  392. }
  393. }while(i++);
  394. i = 1;
  395. do{
  396. this.content[0].scrollTop += 20
  397. if(this.content[0].scrollTop < 20*i){
  398. break;
  399. }
  400. }while(i++);
  401. w += this.content[0].scrollLeft;
  402. h += this.content[0].scrollTop;
  403. this.content[0].scrollLeft = 0;
  404. this.content[0].scrollTop = 0;
  405. //}}} hack end
  406. var wsize = $.getWindowScroll();
  407. var maxH = wsize.height - this.heightN - this.heightS - 10;
  408. var maxW = wsize.width - this.widthW - this.widthE - 10;
  409. h > maxH && (h = maxH);
  410. w > maxW && (w = maxW);
  411. this.setSize(w,h);
  412. },
  413. setLocation:function(top,left){
  414. top = $.toFloat(top,this.top);
  415. left = $.toFloat(left,this.left);
  416. this.dialog.css({top:top,left:left});
  417. return this;
  418. },
  419. minimize:function(){
  420. var r2 = $('#'+this.id + "_row2");
  421. if(!this.minimized){
  422. this.minimized = true;
  423. var dh = r2.height();
  424. this.r2Height = dh;
  425. var h = this.dialog.height() - dh;
  426. this.height -= dh;
  427. r2.hide();
  428. this.dialog.css('height',h);
  429. }else{
  430. this.minimized = false;
  431. var dh = this.r2Height;
  432. this.r2Height = null;
  433. var h = this.dialog.height() + dh;
  434. this.height += dh;
  435. this.dialog.css('height',h);
  436. r2.show();
  437. this.toFront();
  438. }
  439. return this;
  440. },
  441. maximize:function(){
  442. if(this.minimized) return this;
  443. if(this.storedLocation != null){
  444. _restoreLocation(this);
  445. this.maximized = false;
  446. }else{
  447. _storeLocation(this);
  448. this.maximized = true;
  449. var win = $(window),doc = $(document);
  450. var width = win.width() - this.widthW - this.widthE;
  451. var height= win.height() - this.heightN - this.heightS;
  452. this.setSize(width, height);
  453. this.posfixed ?
  454. this.dialog.css({top:0,left:0}) :
  455. this.dialog.css({top:(doc.scrollTop() || 0), left:(doc.scrollLeft() || 0)});
  456. }
  457. return this.toFront();
  458. },
  459. toFront:function(){
  460. Dialogs.toFront(this);
  461. return this;
  462. },
  463. ZIndex: function(zindex){
  464. if(zindex == undefined)
  465. return $.toInt(this.dialog.css('zIndex'));
  466. this.dialog.css('zIndex',zindex);
  467. return this;
  468. },
  469. blur:function(){
  470. var can = true;
  471. typeof this.options.onBlur == 'function' && (can = this.options.onBlur.call(this) !== false);
  472. return can;
  473. },
  474. focus:function(){
  475. typeof this.options.onFocus == 'function' && this.options.onFocus.call(this);
  476. return this;
  477. }
  478. };
  479. var Dialogs = window[namespace+'s'] = {
  480. maxZIndex:99,
  481. focusedWindow:null,
  482. dialogs:{},
  483. calls:{},
  484. settings:{
  485. minWidth: 80,
  486. minHeight: 50,
  487. closable: true,
  488. resizable: false,
  489. minimizable: false,
  490. maximizable: false,
  491. draggable: false,
  492. autosize: false,
  493. lightbox: false,
  494. autocenter: false,
  495. autopos: null, // 'center' / 'fixed'
  496. left: 'auto',
  497. top: 'auto',
  498. title: '&nbsp;',
  499. status: '&nbsp;',
  500. width: 0,
  501. height: 0
  502. },
  503. prevsets:function(settings){
  504. $.extend(Dialogs.settings,settings||{});
  505. },
  506. toFront:function(win){
  507. if(!Dialogs.focusedWindow){
  508. Dialogs.focusedWindow = win;
  509. win.ZIndex(++Dialogs.maxZIndex);
  510. win.focus();
  511. return;
  512. }
  513. if(win == Dialogs.focusedWindow){
  514. return;
  515. }
  516. if($.inArray(Dialogs.focusedWindow,win.relchilds)!=-1){
  517. return;
  518. }
  519. if(win.parentwin){
  520. Dialogs.toFront(win.parentwin);
  521. }
  522. if(Dialogs.focusedWindow.blur() != false){
  523. Dialogs.shift(win);
  524. win.below = Dialogs.focusedWindow;
  525. win.above = null;
  526. Dialogs.focusedWindow.above = win;
  527. Dialogs.focusedWindow = win;
  528. var origI = win.ZIndex();
  529. win.ZIndex(++Dialogs.maxZIndex);
  530. var dI = Dialogs.maxZIndex - origI;
  531. var l = win.relchilds.length;
  532. while(l){
  533. var w = win.relchilds[--l];
  534. if(!w.options) continue;
  535. var I = w.ZIndex()+dI;
  536. w.ZIndex(I);
  537. Dialogs.maxZIndex < I && (Dialogs.maxZIndex = I);
  538. }
  539. win.focus();
  540. }
  541. try{$('input:visible, textarea',Dialogs.focusedWindow.content)[0].focus()}catch(e){}
  542. },
  543. relation:function(Id,win){
  544. if(Id == undefined) return;
  545. var pwin = Id == 'auto' ? Dialogs.focusedWindow : Dialogs.dialogs[Id];
  546. if(pwin instanceof Dialog){
  547. pwin.relchilds.push(win);
  548. win.parentwin = pwin;
  549. }
  550. },
  551. shift:function(win){
  552. (win.below instanceof Dialog) && (win.below.above = win.above);
  553. (win.above instanceof Dialog) && (win.above.below = win.below);
  554. },
  555. close: function(id){
  556. var win = id==undefined ? Dialogs.focusedWindow : Dialogs.dialogs[id];
  557. if(win instanceof Dialog){
  558. win.close();
  559. }
  560. },
  561. show: function(id){
  562. if(Dialogs.dialogs[id] instanceof Dialog){
  563. Dialogs.dialogs[id].show();
  564. }
  565. },
  566. alert:function(message,options){
  567. options || (options = {});
  568. options.button = options.button || {};
  569. options.button.ok = options.button.ok || ['确定',function(){this.close();}];
  570. return Dialogs.opendialog(message,options);
  571. },
  572. confirm:function(message,options){
  573. options || (options = {});
  574. options.button = options.button || {};
  575. options.button.ok = options.button.ok || ['确定',function(){this.close();}];
  576. options.button.cancel = options.button.cancel||['取消',function(){this.close();}];
  577. return Dialogs.opendialog(message,options);
  578. },
  579. info:function(message,timeout,options){
  580. options || (options = {});
  581. if(timeout){
  582. options.onShow = function(){
  583. setTimeout($.fbind(this.close,this),$.toInt(timeout,5000));
  584. };
  585. }
  586. return Dialogs.opendialog(message,options);
  587. },
  588. redirect:function(message,timeout,url,options){
  589. options || (options = {});
  590. options.afterClose = function(){
  591. window.location = url||location.href;
  592. };
  593. if(timeout){
  594. timeout = $.toInt(timeout,5000);
  595. }else{
  596. timeout = 5000;
  597. }
  598. options.onShow = function(){
  599. this.timeout = timeout;
  600. this.timer = setInterval($.fbind(function(){
  601. if((this.timeout -= 1000) < 0 ){
  602. clearInterval(this.timer);
  603. this.close();
  604. return;
  605. }
  606. this.status('<b>leave : '+(this.timeout/1000)+ ' second</b>');
  607. },this),1000);
  608. };
  609. return Dialogs.opendialog(message,options);
  610. },
  611. load:function(url,params,options){
  612. options || (options = {});
  613. var dialog = (new Dialog(options)).html('<div class="dialog_progress"> </div>').show();
  614. $.ajax({// Request the remote document
  615. url: url,
  616. type: 'GET',
  617. cache:false,
  618. data:params,
  619. dataType: "html",
  620. success: function(data,status){
  621. dialog.html(data);// data.replace(/<script(.|\s)*?\/script>/g,'')
  622. if(typeof options.loadsuccess =='function'){
  623. options.loadsuccess.call(dialog);
  624. }
  625. },
  626. error:function(xhr,status){
  627. dialog.html('<font color="red">请求异常!</font>');
  628. if(typeof options.loaderror =='function'){
  629. options.loaderror.call(dialog);
  630. }
  631. }
  632. });
  633. return dialog;
  634. },
  635. opendialog:function(message,options){
  636. options || (options = {});
  637. options.lightbox = typeof options.lightbox =='undefined' ? true : options.lightbox;
  638. options.draggable = typeof options.draggable =='undefined' ? true : options.draggable;
  639. return (new Dialog(options)).html(message).show();
  640. },
  641. callBack:function(id,i){
  642. Dialogs.calls[id] && typeof Dialogs.calls[id][i] == 'function' && Dialogs.calls[id][i]();
  643. }
  644. };
  645. //{{{ private method
  646. var _createWin = function(obj){
  647. if(obj.dialog != null){
  648. return obj;
  649. }
  650. var o = obj.options;
  651. obj.dialog = $('<div id="'+obj.id+'" class="dialog"></div>');
  652. if(o.url){
  653. obj.content = $('<iframe frameborder="0" name="' + obj.id + '_content" src="' + o.url + '"> </iframe>');
  654. }else{
  655. obj.content = $('<div class="dialog_content">'+(o.content||'&nbsp;')+'</div>');
  656. }
  657. var closeDiv = o.closable ? "<div class='dialog_close' id='"+ obj.id +"_close'> </div>" : "";
  658. var minDiv = o.minimizable ? "<div class='dialog_minimize' id='"+ obj.id +"_minimize'> </div>" : "";
  659. var maxDiv = o.maximizable ? "<div class='dialog_maximize' id='"+ obj.id +"_maximize'> </div>" : "";
  660. var seAttributes = o.resizable ? "class='dialog_sizer' id='" + obj.id + "_sizer'" : "class='dialog_se'";
  661. obj.dialog.append("<table id='"+ obj.id
  662. +"_row1' class=\"top table_window\"><tr><td class='dialog_nw'></td><td class='dialog_n'><div id='"
  663. + obj.id+"_top' class='dialog_title title_window'>"+ o.title
  664. +"</div>"+closeDiv + maxDiv+ minDiv +"</td><td class='dialog_ne'></td></tr></table><table id='"+obj.id
  665. +"_row2' class=\"mid table_window\"><tr><td class='dialog_w'></td><td id='"
  666. + obj.id +"_table_content' class='dialog_content' valign='top'></td><td class='dialog_e'></td></tr>"
  667. +"</table><table id='"+ obj.id
  668. +"_row3' class=\"bot table_window\"><tr><td class='dialog_sw'></td><td class='dialog_s'><div id='"
  669. + obj.id+"_bottom' class='status_bar'>"+o.status+"</div></td><td "
  670. + seAttributes+ "></td></tr></table>");
  671. function mouseout(){
  672. this.style.backgroundPosition = "top";
  673. }
  674. function mouseover(){
  675. this.style.backgroundPosition = "center";
  676. }
  677. function mousedown(){
  678. this.style.backgroundPosition = "bottom";
  679. }
  680. o.closable &&
  681. $('#'+obj.id+'_close',obj.dialog).bind("mouseover",mouseover).bind("mouseout",mouseout).bind("mousedown",mousedown).bind("mouseup",mouseover).bind("click",$.fbind(obj.close,obj));
  682. o.minimizable &&
  683. $('#'+obj.id+'_minimize',obj.dialog).bind("mouseover",mouseover).bind("mouseout",mouseout).bind("mousedown",mousedown).bind("mouseup",mouseover).bind("click",$.fbind(obj.minimize,obj));
  684. o.maximizable &&
  685. $('#'+obj.id+'_maximize',obj.dialog).bind("mouseover",mouseover).bind("mouseout",mouseout).bind("mousedown",mousedown).bind("mouseup",mouseover).bind("click",$.fbind(obj.maximize,obj));
  686. var buttons = '';
  687. if(o.button){
  688. Dialogs.calls[obj.id] = [];
  689. buttons = '<div id="'+obj.id+'_button" class="dialog_buttons">';
  690. for(var key in o.button){
  691. var l = Dialogs.calls[obj.id].length;
  692. buttons += '<button type="button" class="dialog_btn" onclick="Dialogs.callBack(\''+obj.id+'\','+l+')">'+o.button[key][0]+'</button>';
  693. Dialogs.calls[obj.id][l] = $.fbind(o.button[key][1],obj);
  694. }
  695. buttons += '</div>';
  696. }
  697. $('#' + obj.id + "_table_content",obj.dialog).append(obj.content).append(buttons);
  698. typeof o.onload == 'function' &&
  699. obj.content.bind('load',$.fbind(o.onload,obj));
  700. if(o.draggable || o.resizable){
  701. obj.initDrag = $.fbindE(_initDrag,obj);
  702. obj.endDrag = $.fbindE(_endDrag,obj);
  703. obj.updateDrag = $.fbindE(_updateDrag,obj);
  704. obj.masker = $('<div class="dialog_masker" style="display:none"></div>').insertBefore(obj.content);
  705. }
  706. if(o.draggable){
  707. var topbar = $('#'+obj.id+'_top',obj.dialog).bind("mousedown",obj.initDrag).parent().addClass("top_draggable").bind("mousedown",obj.initDrag);
  708. topbar.prev().addClass("top_draggable").bind("mousedown",obj.initDrag);
  709. topbar.next().addClass("top_draggable").bind("mousedown",obj.initDrag);
  710. }
  711. o.maximizable && $('#'+obj.id+'_top',obj.dialog).parent().bind("dblclick",$.fbind(obj.maximize,obj));
  712. o.resizable && (obj.sizer = $('#'+obj.id+'_sizer',obj.dialog).bind("mousedown",obj.initDrag));
  713. obj.content.bind("mousedown",$.fbind(obj.toFront,obj));
  714. o.lightbox && _initLightbox(obj);
  715. obj.parent.prepend(obj.dialog.css('visibility','hidden'));
  716. _getWindowBorderSize(obj);
  717. obj.width = o.width || 0;
  718. obj.height = o.height || 0;
  719. obj.setSize(o.width, o.height);
  720. o.autosize && obj.autoAdaptSize();
  721. var wsize = $.getWindowScroll();
  722. if(o.autopos == 'fixed'){
  723. if(IE6){
  724. obj.autoposition = function(){
  725. if(obj.dowhat) return obj;
  726. var newscroll = $.getWindowScroll();
  727. var top = parseFloat(obj.dialog.css('top')) + newscroll.top - obj.scrollcoords.top;
  728. var left = parseFloat(obj.dialog.css('left')) + newscroll.left - obj.scrollcoords.left;
  729. obj.dialog.css({top:top,left:left});
  730. obj.scrollcoords = newscroll;
  731. }
  732. obj.scrollcoords = wsize;
  733. $(window).bind('scroll',obj.autoposition);
  734. }else{
  735. obj.dialog.css('position','fixed');
  736. obj.posfixed = true;
  737. }
  738. }else if(o.autopos == 'center'){
  739. IE6 || (obj.dialog.css('position','fixed') && (obj.posfixed = true));
  740. obj.autoposition = function(){
  741. var newscroll = $.getWindowScroll();
  742. var top = (newscroll.height - (obj.height + obj.heightN + obj.heightS))/2;
  743. var left = (newscroll.width - (obj.width + obj.widthW + obj.widthE))/2;
  744. if(IE6){
  745. top += newscroll.top;
  746. left += newscroll.left;
  747. }
  748. obj.dialog.css({top:top,left:left});
  749. };
  750. $(window).bind('resize scroll',obj.autoposition);
  751. }
  752. if(o.top=='auto'){
  753. o.top = (wsize.height - (obj.height + obj.heightN + obj.heightS))/2;
  754. obj.posfixed || (o.top += wsize.top);
  755. }
  756. if(o.left=='auto'){
  757. o.left = (wsize.width - (obj.width + obj.widthW + obj.widthE))/2;
  758. obj.posfixed || (o.left += wsize.left);
  759. }
  760. obj.dialog.css({top:$.toFloat(o.top),left:$.toFloat(o.left),visibility:'visible'});
  761. _checkIEOverlapping(obj);
  762. };
  763. var _initDrag = function(evt){
  764. var target = evt.target;
  765. if(((this.sizer && target == this.sizer[0]) && this.minimized)
  766. || ((this.sizer && target != this.sizer[0]) && this.maximized))
  767. return;
  768. if($.inArray(target.getAttribute('id'),[this.id+'_close',this.id+'_maximize',this.id+'_minimize']) != -1){
  769. return;
  770. }
  771. this.dowhat = null;
  772. this.pointer = {X: evt.pageX,Y: evt.pageY};
  773. if(this.sizer && target == this.sizer[0]){
  774. this.dowhat = 'resizing';
  775. this.widthOrg = this.width;
  776. this.heightOrg = this.height;
  777. }
  778. else{
  779. this.dowhat = 'dragging';
  780. if(target.getAttribute('id')==this.id+'_close'){
  781. return;
  782. }
  783. }
  784. this.toFront();
  785. if(!this.minimized) {
  786. this.masker.css({
  787. zIndex: 1,
  788. width: this.width,
  789. height: this.height
  790. }).show();
  791. IE6 && $('select',this.content).each(function(){
  792. // hide select
  793. this.oldVisibility = this.style.visibility ? this.style.visibility : "visible";
  794. this.style.visibility = "hidden";
  795. });
  796. }
  797. $(document).bind('mouseup',this.endDrag).bind('mousemove',this.updateDrag);
  798. document.body.ondrag = function () { return false; };
  799. document.body.onselectstart = function () { return false; };
  800. };
  801. var _updateDrag = function(evt){
  802. if(this.dowhat == null){
  803. return;
  804. }
  805. var pointer = {X: evt.pageX,Y: evt.pageY};
  806. var dx = pointer.X - this.pointer.X;
  807. var dy = pointer.Y - this.pointer.Y;
  808. var w = this.widthOrg + dx;
  809. var h = this.heightOrg + dy;
  810. var left = parseFloat(this.dialog.css('left')) + dx;
  811. var top = parseFloat(this.dialog.css('top')) + dy;
  812. // Resize case, update width/height
  813. if(this.dowhat == 'resizing'){
  814. this.setSize(w ,h);
  815. }
  816. // Move case, update top/left
  817. else if(this.dowhat == 'dragging'){
  818. this.pointer = pointer;
  819. this.dialog.css({top:top,left:left});
  820. }
  821. };
  822. var _endDrag = function(evt){
  823. this.dowhat = null;
  824. if(!this.minimized){
  825. this.masker.hide();
  826. IE6 && $('select',this.content).each(function(){
  827. if(typeof this.oldVisibility != 'undefined'){
  828. try{
  829. this.style.visibility = this.oldVisibility;
  830. }catch(e){
  831. this.style.visibility = "visible";
  832. }
  833. this.oldVisibility = null;
  834. }else{
  835. this.style.visibility && (this.style.visibility = "visible");
  836. }
  837. });
  838. }
  839. $(document).unbind('mouseup',this.endDrag).unbind('mousemove',this.updateDrag);
  840. document.body.ondrag = null;
  841. document.body.onselectstart = null;
  842. };
  843. var _storeLocation = function(obj){
  844. if(obj.storedLocation == null){
  845. obj.storedLocation = {
  846. top: obj.dialog.css('top'),
  847. left: obj.dialog.css('left'),
  848. width: obj.width, height: obj.height
  849. };
  850. }
  851. };
  852. var _restoreLocation = function(obj){
  853. obj.dialog.css({top: obj.storedLocation.top,left: obj.storedLocation.left});
  854. obj.setSize(obj.storedLocation.width, obj.storedLocation.height);
  855. obj.storedLocation = null;
  856. };
  857. var _getWindowBorderSize = function(obj){
  858. var _createHiddenDiv = function(className){
  859. return $('<div class="'+className+' style="display:none"></div>').prependTo(document.body);
  860. }
  861. // Hack to get real window border size!!
  862. var div = _createHiddenDiv("dialog_n");
  863. obj.heightN = div.getDimensions().height;
  864. div.remove();
  865. div = _createHiddenDiv("dialog_s");
  866. obj.heightS = div.getDimensions().height;
  867. div.remove();
  868. obj.options.button && (obj.heightS += $('#'+obj.id+'_button',obj.dialog).getDimensions().height);
  869. div = _createHiddenDiv("dialog_e");
  870. obj.widthE = div.getDimensions().width;
  871. div.remove();
  872. div = _createHiddenDiv("dialog_w");
  873. obj.widthW = div.getDimensions().width;
  874. div.remove();
  875. div = null;
  876. };
  877. var _checkIEOverlapping = function(obj){
  878. if(obj.iefix || !IE6){
  879. return;
  880. }
  881. obj.iefix = $('<iframe style="display:none;position:absolute;" src="javascript:false;document.write(\'\')" frameborder="0" scrolling="no"></iframe>');
  882. if(obj.lightbox){
  883. obj.lightbox.html('<p style="width:100%;height:100%;">').prepend(obj.iefix);
  884. }else{
  885. obj.dialog.prepend(obj.iefix);
  886. }
  887. obj.iefix.css({opacity:0,top: 0,left: 0,zIndex: -1,width: '100%',height: '100%',display:'block'});
  888. try{$('input:visible, textarea',obj.content)[0].focus()}catch(e){}
  889. };
  890. var _initLightbox = function(obj){
  891. if(obj.lightbox){
  892. obj.lightbox.css('zIndex',(++Dialogs.maxZIndex));
  893. }else{
  894. obj.lightbox = $('<div id="'+obj.id+'_overlay" class="overlay_dialog"></div>').prependTo(obj.parent);
  895. obj.lightbox.css({display:'none',top:0,left:0,zIndex:(++Dialogs.maxZIndex),width:'100%',position:'fixed',height:'100%'});
  896. if(IE6){// use setExpression instead
  897. var o = obj.lightbox.css('position','absolute')[0];
  898. o.style.setExpression('width','(_=$.getPageSize().pageWidth)+"px"');
  899. o.style.setExpression('height','(_=$.getPageSize().pageHeight)+"px"');
  900. }
  901. }
  902. };
  903. //}}} end private method
  904. })(jQuery,'Dialog');