dialog.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. __DIALOG_WRAPPER__ = {};
  2. /* IE6有个Bug,如果不给定对话框的宽度的话,在IE6下,对话框将以100%宽度显示 */
  3. DialogManager = {
  4. 'create' :function(id){
  5. var d = {};
  6. if (!__DIALOG_WRAPPER__[id])
  7. {
  8. d = new Dialog(id);
  9. __DIALOG_WRAPPER__[id] = d;
  10. }
  11. else
  12. {
  13. d = DialogManager.get(id);
  14. }
  15. return d;
  16. },
  17. 'get' :function(id){
  18. return __DIALOG_WRAPPER__[id];
  19. },
  20. 'close' :function(id){
  21. if (__DIALOG_WRAPPER__[id].close())
  22. {
  23. __DIALOG_WRAPPER__[id] = null;
  24. }
  25. },
  26. 'onClose' :function (){
  27. return true;
  28. },
  29. /* 加载对话框样式 */
  30. 'loadStyle' :function(){
  31. var _dialog_js_path = $('#dialog_js').attr('src');
  32. var _path = _dialog_js_path.split('/');
  33. var _dialog_css = _path.slice(0, _path.length - 1).join('/') + '/dialog.css';
  34. $('#dialog_js').after('<link href="' + _dialog_css + '" rel="stylesheet" type="text/css" />');
  35. }
  36. };
  37. ScreenLocker = {
  38. 'style' : {
  39. 'position' : 'absolute',
  40. 'top' : '0px',
  41. 'left' : '0px',
  42. 'backgroundColor' : '#000',
  43. 'opacity' : 0.2,
  44. 'zIndex' : 999
  45. },
  46. 'masker' : null,
  47. 'lock' : function(zIndex){
  48. if (this.masker !== null)
  49. {
  50. this.masker.width($(document).width()).height($(document).height());
  51. return true;
  52. }
  53. this.masker = $('<div id="dialog_manage_screen_locker"></div>');
  54. /* 样式 */
  55. this.masker.css(this.style);
  56. if (zIndex)
  57. {
  58. this.masker.css('zIndex', zIndex);
  59. }
  60. /* 整个文档的宽高 */
  61. this.masker.width($(document).width()).height($(document).height());
  62. $(document.body).append(this.masker);
  63. $("#dialog_manage_screen_locker").show();
  64. },
  65. 'unlock' : function(){
  66. if (this.masker === null)
  67. {
  68. return true;
  69. }
  70. this.masker.remove();
  71. this.masker = null;
  72. }
  73. };
  74. Dialog = function (id){
  75. /* 构造函数生成对话框代码,并加入到文档中 */
  76. this.id = id;
  77. this.init();
  78. };
  79. Dialog.prototype = {
  80. /* 唯一标识 */
  81. 'id' : null,
  82. /* 文档对象 */
  83. 'dom' : null,
  84. 'lastPos' : null,
  85. 'status' : 'complete',
  86. 'onClose' : function (){
  87. return true;
  88. },
  89. 'tmp' : {},
  90. /* 初始化 */
  91. 'init' : function(){
  92. this.dom = {'wrapper' : null, 'body':null, 'head':null, 'title':null, 'close_button':null, 'content':null};
  93. /* 创建外层容器 */
  94. this.dom.wrapper = $('<div id="fwin_' + this.id + '" class="dialog_wrapper"></div>').get(0);
  95. /* 创建对话框主体 */
  96. this.dom.body = $('<div class="dialog_body"></div>').get(0);
  97. /* 创建标题栏 */
  98. this.dom.head = $('<h3 class="dialog_head"></h3>').get(0);
  99. /* 创建标题文本 */
  100. this.dom.title = $('<span class="dialog_title_icon"></span>').get(0);
  101. /* 创建关闭按钮 */
  102. this.dom.close_button = $('<span class="dialog_close_button">close</span>').get(0);
  103. /* 创建内容区域 */
  104. this.dom.content = $('<div class="dialog_content"></div>').get(0);
  105. /* 组合 */
  106. $(this.dom.head).append($('<span class="dialog_title"></span>').append(this.dom.title)).append(this.dom.close_button);
  107. $(this.dom.body).append(this.dom.head).append(this.dom.content);
  108. $(this.dom.wrapper).append(this.dom.body).append('<div style="clear:both; display:block;"></div>');
  109. /* 初始化样式 */
  110. $(this.dom.wrapper).css({
  111. 'zIndex' : 1100,
  112. 'display' : 'none',
  113. 'position' : 'absolute'
  114. });
  115. $(this.dom.body).css({
  116. 'position' : 'relative'
  117. });
  118. $(this.dom.head).css({
  119. 'cursor' : 'move'
  120. });
  121. $(this.dom.close_button).css({
  122. 'position' : 'absolute',
  123. 'text-indent': '-9999px',
  124. 'cursor' : 'pointer',
  125. 'overflow' : 'hidden'
  126. });
  127. $(this.dom.content).css({
  128. 'margin' : '0px',
  129. 'padding' : '0px'
  130. });
  131. var self = this;
  132. /* 初始化组件事件 */
  133. $(this.dom.close_button).click(function(){
  134. DialogManager.close(self.id);
  135. });
  136. /* 可拖放 */
  137. // if(typeof draggable != 'undefined'){
  138. // $(this.dom.wrapper).draggable({
  139. // 'handle' : this.dom.head
  140. // });
  141. // }
  142. /* 放入文档流 */
  143. $(document.body).append(this.dom.wrapper);
  144. },
  145. /* 隐藏 */
  146. 'hide' : function(){
  147. $(this.dom.wrapper).hide();
  148. },
  149. /* 显示 */
  150. 'show' : function(pos,lock){
  151. if (pos)
  152. {
  153. this.setPosition(pos);
  154. }
  155. /* 锁定屏幕 */
  156. if (lock == 1) ScreenLocker.lock(999);
  157. $(this.dom.wrapper).draggable({
  158. 'handle' : this.dom.head
  159. });
  160. /* 显示对话框 */
  161. $(this.dom.wrapper).show();
  162. },
  163. /* 关闭 */
  164. 'close' : function(lock){
  165. if (!this.onClose())
  166. {
  167. return false;
  168. }
  169. /* 关闭对话框 */
  170. $(this.dom.wrapper).remove();
  171. /* 解锁屏幕 */
  172. if (typeof lock == 'undefined'){
  173. ScreenLocker.unlock();
  174. }
  175. return true;
  176. },
  177. /* 对话框标题 */
  178. 'setTitle' : function(title){
  179. $(this.dom.title).html(title);
  180. },
  181. /* 改变对话框内容 */
  182. 'setContents' : function(type, options){
  183. contents = this.createContents(type, options);
  184. if (typeof(contents) == 'string')
  185. {
  186. $(this.dom.content).html(contents);
  187. }
  188. else
  189. {
  190. $(this.dom.content).empty();
  191. $(this.dom.content).append(contents);
  192. }
  193. },
  194. /* 设置对话框样式 */
  195. 'setStyle' : function(style){
  196. if (typeof(style) == 'object')
  197. {
  198. /* 否则为CSS */
  199. $(this.dom.wrapper).css(style);
  200. }
  201. else
  202. {
  203. /* 如果是字符串,则认为是样式名 */
  204. $(this.dom.wrapper).addClass(style);
  205. }
  206. },
  207. 'setWidth' : function(width){
  208. this.setStyle({'width' : width + 'px'});
  209. },
  210. 'setHeight' : function(height){
  211. this.setStyle({'height' : height + 'px'});
  212. },
  213. /* 生成对话框内容 */
  214. 'createContents' : function(type, options){
  215. var _html = '',
  216. self = this,
  217. status= 'complete';
  218. if (!options)
  219. {
  220. /* 如果只有一个参数,则认为其传递的是HTML字符串 */
  221. this.setStatus(status);
  222. return type;
  223. }
  224. switch(type){
  225. case 'ajax':
  226. /* 通过Ajax取得HTML,显示到页面上,此过程是异步的 */
  227. $.get(options, function(data){
  228. self.setContents(data);
  229. /* 使用上次定位重新定位窗口位置 */
  230. self.setPosition(self.lastPos);
  231. });
  232. /* 先提示正在加载 */
  233. _html = this.createContents('loading', {'text' : 'loading...'});
  234. break;
  235. case 'ajax_notice':
  236. /* 通过Ajax取得HTML,显示到页面上,此过程是异步的 */
  237. $.get(options, function(data) {
  238. var json = eval('(' + data + ')');
  239. var MsgTxt = '<div class="dialog_message_body"></div><div class="dialog_message_contents dialog_message_notice">' + json.Msg + '</div><div class="dialog_buttons_bar"></div>'
  240. self.setContents(MsgTxt);
  241. /* 使用上次定位重新定位窗口位置 */
  242. self.setPosition(self.lastPos);
  243. });
  244. /* 先提示正在加载 */
  245. _html = this.createContents('loading', { 'text': '正在处理...' });
  246. break;
  247. /* 以下是内置的几种对话框类型 */
  248. case 'loading':
  249. _html = '<div class="dialog_loading"><div class="dialog_loading_text">' + options.text + '</div></div>';
  250. status = 'loading';
  251. break;
  252. case 'message':
  253. var type = 'notice';
  254. if (options.type)
  255. {
  256. type = options.type;
  257. }
  258. _message_body = $('<div class="dialog_message_body"></div>');
  259. _message_contents = $('<div class="dialog_message_contents dialog_message_' + type + '">' + options.text + '</div>');
  260. _buttons_bar = $('<div class="dialog_buttons_bar"></div>');
  261. switch (type){
  262. case 'notice':
  263. case 'warning':
  264. var button_name = '确定';
  265. if (options.button_name)
  266. {
  267. button_name = options.button_name;
  268. }
  269. _ok_button = $('<input type="button" class="btn1" value="' + button_name + '" />');
  270. $(_ok_button).click(function(){
  271. if (options.onclick)
  272. {
  273. if(!options.onclick.call())
  274. {
  275. return;
  276. }
  277. }
  278. DialogManager.close(self.id);
  279. });
  280. $(_buttons_bar).append(_ok_button);
  281. break;
  282. case 'confirm':
  283. var yes_button_name = "确定";
  284. var no_button_name = "取消";
  285. if (options.yes_button_name)
  286. {
  287. yes_button_name = options.yes_button_name;
  288. }
  289. if (options.no_button_name)
  290. {
  291. no_button_name = options.no_button_name;
  292. }
  293. _yes_button = $('<input type="button" class="btn1" value="' + yes_button_name + '" />');
  294. _no_button = $('<input type="button" class="btn2" value="' + no_button_name + '" />');
  295. $(_yes_button).click(function(){
  296. if (options.onClickYes)
  297. {
  298. if (options.onClickYes.call() === false)
  299. {
  300. return;
  301. }
  302. }
  303. DialogManager.close(self.id);
  304. });
  305. $(_no_button).click(function(){
  306. if (options.onClickNo)
  307. {
  308. if (!options.onClickNo.call())
  309. {
  310. return;
  311. }
  312. }
  313. DialogManager.close(self.id);
  314. });
  315. $(_buttons_bar).append(_yes_button).append(_no_button);
  316. break;
  317. }
  318. _html = $(_message_body).append(_message_contents).append(_buttons_bar);
  319. break;
  320. }
  321. this.setStatus(status);
  322. return _html;
  323. },
  324. /* 定位 */
  325. 'setPosition' : function(pos){
  326. /* 上次定位 */
  327. this.lastPos = pos;
  328. if (typeof(pos) == 'string')
  329. {
  330. switch(pos){
  331. case 'center':
  332. var left = 0;
  333. var top = 0;
  334. var dialog_width = $(this.dom.wrapper).width();
  335. var dialog_height = $(this.dom.wrapper).height();
  336. /* left=滚动条的宽度 + (当前可视区的宽度 - 对话框的宽度 ) / 2 */
  337. left = $(window).scrollLeft() + ($(window).width() - dialog_width) / 2;
  338. /* top =滚动条的高度 + (当前可视区的高度 - 对话框的高度 ) / 2 */
  339. top = $(window).scrollTop() + ($(window).height() - dialog_height) / 2;
  340. $(this.dom.wrapper).css({left:left + 'px', top:top + 'px'});
  341. break;
  342. }
  343. }
  344. else
  345. {
  346. var _pos = {};
  347. if (typeof(pos.left) != 'undefined')
  348. {
  349. _pos.left = pos.left;
  350. }
  351. if (typeof(pos.top) != 'undefined')
  352. {
  353. _pos.top = pos.top;
  354. }
  355. $(this.dom.wrapper).css(_pos);
  356. }
  357. },
  358. /* 设置状态 */
  359. 'setStatus' : function(code){
  360. this.status = code;
  361. },
  362. /* 获取状态 */
  363. 'getStatus' : function(){
  364. return this.status;
  365. },
  366. 'disableClose' : function(msg){
  367. this.tmp['oldOnClose'] = this.onClose;
  368. this.onClose = function(){
  369. if(msg)alert(msg);
  370. return false;
  371. };
  372. },
  373. 'enableClose' : function(){
  374. this.onClose = this.tmp['oldOnClose'];
  375. this.tmp['oldOnClose'] = null;
  376. }
  377. };
  378. DialogManager.loadStyle();