jquery15.artDialog.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. (function($, window, undefined) {
  2. var _version = '6.0.0';
  3. var _count = 0;
  4. var _expando = new Date() - 0;
  5. var _isIE6 = !('minWidth' in $('html')[0].style);
  6. var _isMobile = 'createTouch' in document && !('onmousemove' in document)
  7. || /(iPhone|iPad|iPod)/i.test(navigator.userAgent);
  8. var _isFixed = !_isIE6 && !_isMobile;
  9. // defaults
  10. var defaults = {
  11. /* -----已注释的配置继承自 popup.js,仍可以再这里重新定义它----- */
  12. // 对齐方式
  13. //align: 'bottom left',
  14. // 是否固定定位
  15. //fixed: false,
  16. // 对话框叠加高度值(重要:此值不能超过浏览器最大限制)
  17. //zIndex: 1024,
  18. // 设置遮罩背景颜色
  19. //backdropBackground: '#000',
  20. // 设置遮罩透明度
  21. //backdropOpacity: 0.7,
  22. // 消息内容
  23. content: '<span class="ui-dialog-loading">Loading..</span>',
  24. // 标题
  25. title: '',
  26. // 对话框状态栏区域 HTML 代码
  27. statusbar: '',
  28. // 自定义按钮
  29. button: null,
  30. // 确定按钮回调函数
  31. ok: null,
  32. // 取消按钮回调函数
  33. cancel: null,
  34. // 确定按钮文本
  35. okValue: 'ok',
  36. // 取消按钮文本
  37. cancelValue: 'cancel',
  38. // 内容宽度
  39. width: '',
  40. // 内容高度
  41. height: '',
  42. // 内容与边界填充距离
  43. padding: '',
  44. // 对话框自定义 className
  45. skin: '',
  46. // 是否支持快捷关闭(点击遮罩层自动关闭)
  47. quickClose: false,
  48. // css 文件路径,留空则不会使用 js 自动加载样式
  49. // 注意:css 只允许加载一个
  50. cssUri: '../css/ui-dialog.css',
  51. // 模板(使用 table 解决 IE7 宽度自适应的 BUG)
  52. // js 使用 i="***" 属性识别结构,其余的均可自定义
  53. innerHTML:
  54. '<div i="dialog" class="ui-dialog">'
  55. + '<div class="ui-dialog-arrow-a"></div>'
  56. + '<div class="ui-dialog-arrow-b"></div>'
  57. + '<table class="ui-dialog-grid">'
  58. + '<tr>'
  59. + '<td i="header" class="ui-dialog-header">'
  60. + '<a i="close" class="ui-dialog-close">&#215;</a>' //@edit:将之前的 <button> 修改为 <a> //@edit: 去掉关闭按钮
  61. + '<div i="title" class="ui-dialog-title"></div>'
  62. + '</td>'
  63. + '</tr>'
  64. + '<tr>'
  65. + '<td i="body" class="ui-dialog-body">'
  66. + '<div i="content" class="ui-dialog-content"></div>'
  67. + '</td>'
  68. + '</tr>'
  69. + '<tr>'
  70. + '<td i="footer" class="ui-dialog-footer">'
  71. + '<div i="statusbar" class="ui-dialog-statusbar"></div>'
  72. + '<div i="button" class="ui-dialog-button"></div>'
  73. + '</td>'
  74. + '</tr>'
  75. + '</table>'
  76. +'</div>',
  77. innerHTML_NC: //@edit: 增加NC框架使用的Dialog
  78. '<div i="dialog" class="ui-dialog" style="background-color:#fff; padding:10px">'
  79. + '<div class="ui-dialog-arrow-a"></div>'
  80. + '<div class="ui-dialog-arrow-b"></div>'
  81. + '<div class="ui-dialog-header" style="border: none">'
  82. + '<a i="close" title="关闭" class="ui-dialog-close">×</a>' //@edit: 去掉关闭按钮
  83. + '</div>'
  84. + '<div i="content" class="ui-dialog-content">'
  85. + '</div>'
  86. +'</div>'
  87. };
  88. // Popup 定义开始
  89. var Popup = function () {
  90. this.destroyed = false;
  91. this.__popup = $('<div />')
  92. .attr({
  93. tabindex: '-1'
  94. })
  95. .css({
  96. display: 'none',
  97. position: 'absolute',
  98. left: 0,
  99. top: 0,
  100. bottom: 'auto',
  101. right: 'auto',
  102. margin: 0,
  103. padding: 0,
  104. outline: 0,
  105. border: '0 none',
  106. background: 'transparent'
  107. })
  108. .html(this.innerHTML)
  109. .appendTo('body');
  110. this.__backdrop = $('<div />');
  111. // 使用 HTMLElement 作为外部接口使用,而不是 jquery 对象
  112. // 统一的接口利于未来 Popup 移植到其他 DOM 库中
  113. this.node = this.__popup[0];
  114. this.backdrop = this.__backdrop[0];
  115. _count ++;
  116. };
  117. $.extend(Popup.prototype, {
  118. /**
  119. * 初始化完毕事件,在 show()、showModal() 执行
  120. * @name Popup.prototype.onshow
  121. * @event
  122. */
  123. /**
  124. * 关闭事件,在 close() 执行
  125. * @name Popup.prototype.onclose
  126. * @event
  127. */
  128. /**
  129. * 销毁前事件,在 remove() 前执行
  130. * @name Popup.prototype.onbeforeremove
  131. * @event
  132. */
  133. /**
  134. * 销毁事件,在 remove() 执行
  135. * @name Popup.prototype.onremove
  136. * @event
  137. */
  138. /**
  139. * 重置事件,在 reset() 执行
  140. * @name Popup.prototype.onreset
  141. * @event
  142. */
  143. /**
  144. * 焦点事件,在 foucs() 执行
  145. * @name Popup.prototype.onfocus
  146. * @event
  147. */
  148. /**
  149. * 失焦事件,在 blur() 执行
  150. * @name Popup.prototype.onblur
  151. * @event
  152. */
  153. /** 浮层 DOM 素节点 */
  154. node: null,
  155. /** 遮罩 DOM 节点 */
  156. backdrop: null,
  157. /** 是否开启固定定位 */
  158. fixed: false,
  159. /** 判断对话框是否删除 */
  160. destroyed: true,
  161. /** 判断对话框是否显示 */
  162. open: false,
  163. /** close 返回值 */
  164. returnValue: '',
  165. /** 是否自动聚焦 */
  166. autofocus: true,
  167. /** 对齐方式 */
  168. align: 'bottom left',
  169. /** 设置遮罩背景颜色 */
  170. backdropBackground: '#000',
  171. /** 设置遮罩透明度 */
  172. backdropOpacity: 0.7,
  173. /** 内部的 HTML 字符串 */
  174. innerHTML: '',
  175. /** 类名 */
  176. className: 'ui-popup',
  177. /**
  178. * 显示浮层
  179. * @param {HTMLElement, Event} 指定位置(可选)
  180. */
  181. show: function (anchor) {
  182. if (this.destroyed) {
  183. return this;
  184. }
  185. var that = this;
  186. var popup = this.__popup;
  187. this.__activeElement = this.__getActive();
  188. this.open = true;
  189. this.follow = anchor || this.follow;
  190. if (!this.__ready) {
  191. popup.addClass(this.className);
  192. if (this.modal) {
  193. this.__lock();
  194. }
  195. if (!popup[0].innerHTML) { // @edit: 原来的判断为 "!popup.html()",但这种方式会造成 ng-include失效,所以修改为 "!popup[0].innerHTML"
  196. popup.html(this.innerHTML);
  197. }
  198. if (!_isIE6) {
  199. $(window).bind('resize', this.__onresize = function () {
  200. that.reset();
  201. });
  202. }
  203. this.__ready = true;
  204. }
  205. popup
  206. .addClass(this.className + '-show')
  207. .attr('role', this.modal ? 'alertdialog' : 'dialog')
  208. .css('position', this.fixed ? 'fixed' : 'absolute')
  209. .show();
  210. this.__backdrop.show();
  211. this.reset().focus();
  212. this.__dispatchEvent('show');
  213. return this;
  214. },
  215. /** 显示模态浮层。参数参见 show() */
  216. showModal: function () {
  217. this.modal = true;
  218. return this.show.apply(this, arguments);
  219. },
  220. /** 关闭浮层 */
  221. close: function (result) {
  222. if (!this.destroyed && this.open) {
  223. if (result !== undefined) {
  224. this.returnValue = result;
  225. }
  226. this.__popup.hide().removeClass(this.className + '-show');
  227. this.__backdrop.hide();
  228. this.open = false;
  229. this.blur();
  230. this.__dispatchEvent('close',this.returnValue); //@edit: 增加返回值参数
  231. }
  232. if (this.destroyed) {
  233. return this;
  234. }
  235. this.__dispatchEvent('beforeremove');
  236. if (Popup.current === this) {
  237. Popup.current = null;
  238. }
  239. this.__unlock();
  240. this.__popup.remove();
  241. this.__backdrop.remove();
  242. // 恢复焦点,照顾键盘操作的用户
  243. this.blur();
  244. if (!_isIE6) {
  245. $(window).unbind('resize', this.__onresize);
  246. }
  247. this.__dispatchEvent('remove');
  248. for (var i in this) {
  249. delete this[i];
  250. }
  251. return this;
  252. },
  253. /** 销毁浮层 */
  254. remove: function () {
  255. if (this.destroyed) {
  256. return this;
  257. }
  258. this.__dispatchEvent('beforeremove');
  259. if (Popup.current === this) {
  260. Popup.current = null;
  261. }
  262. this.__unlock();
  263. this.__popup.remove();
  264. this.__backdrop.remove();
  265. // 恢复焦点,照顾键盘操作的用户
  266. this.blur();
  267. if (!_isIE6) {
  268. $(window).unbind('resize', this.__onresize);
  269. }
  270. this.__dispatchEvent('remove');
  271. for (var i in this) {
  272. delete this[i];
  273. }
  274. return this;
  275. },
  276. /** 手动刷新位置 */
  277. reset: function () {
  278. var elem = this.follow;
  279. if (elem) {
  280. this.__follow(elem);
  281. } else {
  282. this.__center();
  283. }
  284. this.__dispatchEvent('reset');
  285. return this;
  286. },
  287. /** 让浮层获取焦点 */
  288. focus: function () {
  289. var node = this.node;
  290. var current = Popup.current;
  291. if (current && current !== this) {
  292. current.blur(false);
  293. }
  294. // 检查焦点是否在浮层里面
  295. if (!$.contains(node, this.__getActive())) {
  296. var autofocus = this.__popup.find('[autofocus]')[0];
  297. if (!this._autofocus && autofocus) {
  298. this._autofocus = true;
  299. } else {
  300. autofocus = node;
  301. }
  302. this.__focus(autofocus);
  303. }
  304. Popup.current = this;
  305. this.__popup.addClass(this.className + '-focus');
  306. this.__zIndex();
  307. this.__dispatchEvent('focus');
  308. this.reset();
  309. return this;
  310. },
  311. /** 让浮层失去焦点。将焦点退还给之前的元素,照顾视力障碍用户 */
  312. blur: function () {
  313. var activeElement = this.__activeElement;
  314. var isBlur = arguments[0];
  315. if (isBlur !== false) {
  316. this.__focus(activeElement);
  317. }
  318. this._autofocus = false;
  319. this.__popup.removeClass(this.className + '-focus');
  320. this.__dispatchEvent('blur');
  321. return this;
  322. },
  323. /**
  324. * 添加事件
  325. * @param {String} 事件类型
  326. * @param {Function} 监听函数
  327. */
  328. addEventListener: function (type, callback) {
  329. this.__getEventListener(type).push(callback);
  330. return this;
  331. },
  332. /**
  333. * 删除事件
  334. * @param {String} 事件类型
  335. * @param {Function} 监听函数
  336. */
  337. removeEventListener: function (type, callback) {
  338. var listeners = this.__getEventListener(type);
  339. for (var i = 0; i < listeners.length; i ++) {
  340. if (callback === listeners[i]) {
  341. listeners.splice(i--, 1);
  342. }
  343. }
  344. return this;
  345. },
  346. // 获取事件缓存
  347. __getEventListener: function (type) {
  348. var listener = this.__listener;
  349. if (!listener) {
  350. listener = this.__listener = {};
  351. }
  352. if (!listener[type]) {
  353. listener[type] = [];
  354. }
  355. return listener[type];
  356. },
  357. // 派发事件
  358. __dispatchEvent: function (type,args) { //@edit: 增加args参数,方便传值
  359. var listeners = this.__getEventListener(type);
  360. if (this['on' + type]) {
  361. if(args){
  362. this['on' + type](args);
  363. }else{
  364. this['on' + type]();
  365. }
  366. }
  367. for (var i = 0; i < listeners.length; i ++) {
  368. listeners[i].call(this);
  369. }
  370. },
  371. // 对元素安全聚焦
  372. __focus: function (elem) {
  373. // 防止 iframe 跨域无权限报错
  374. // 防止 IE 不可见元素报错
  375. try {
  376. // ie11 bug: iframe 页面点击会跳到顶部
  377. if (this.autofocus && !/^iframe$/i.test(elem.nodeName)) {
  378. elem.focus();
  379. }
  380. } catch (e) {}
  381. },
  382. // 获取当前焦点的元素
  383. __getActive: function () {
  384. try {// try: ie8~9, iframe #26
  385. var activeElement = document.activeElement;
  386. var contentDocument = activeElement.contentDocument;
  387. var elem = contentDocument && contentDocument.activeElement || activeElement;
  388. return elem;
  389. } catch (e) {}
  390. },
  391. // 置顶浮层
  392. __zIndex: function () {
  393. var index = Popup.zIndex ++;
  394. // 设置叠加高度
  395. this.__popup.css('zIndex', index);
  396. this.__backdrop.css('zIndex', index - 1);
  397. this.zIndex = index;
  398. },
  399. // 居中浮层
  400. __center: function () {
  401. var popup = this.__popup;
  402. var $window = $(window);
  403. var $document = $(document);
  404. var fixed = this.fixed;
  405. var dl = fixed ? 0 : $document.scrollLeft();
  406. var dt = fixed ? 0 : $document.scrollTop();
  407. var ww = $window.width();
  408. var wh = $window.height();
  409. var ow = popup.width();
  410. var oh = popup.height();
  411. var left = (ww - ow) / 2 + dl;
  412. var top = (wh - oh) * 382 / 1000 + dt;// 黄金比例
  413. var style = popup[0].style;
  414. style.left = Math.max(parseInt(left), dl) + 'px';
  415. style.top = Math.max(parseInt(top), dt) + 'px';
  416. },
  417. // 指定位置 @param {HTMLElement, Event} anchor
  418. __follow: function (anchor) {
  419. var $elem = anchor.parentNode && $(anchor);
  420. var popup = this.__popup;
  421. if (this.__followSkin) {
  422. popup.removeClass(this.__followSkin);
  423. }
  424. // 隐藏元素不可用
  425. if ($elem) {
  426. var o = $elem.offset();
  427. if (o.left * o.top < 0) {
  428. return this.__center();
  429. }
  430. }
  431. var that = this;
  432. var fixed = this.fixed;
  433. var $window = $(window);
  434. var $document = $(document);
  435. var winWidth = $window.width();
  436. var winHeight = window.document.body.clientHeight; //@edit: 原来为 “$window.height()”,在某些chrome版本中取值不对,改成原生JS
  437. var docLeft = $document.scrollLeft();
  438. var docTop = $document.scrollTop();
  439. var popupWidth = popup.width();
  440. var popupHeight = popup.height();
  441. var width = $elem ? $elem.outerWidth() : 0;
  442. var height = $elem ? $elem.outerHeight() : 0;
  443. var offset = this.__offset(anchor);
  444. var x = offset.left;
  445. var y = offset.top;
  446. var left = fixed ? x - docLeft : x;
  447. var top = fixed ? y - docTop : y;
  448. var minLeft = fixed ? 0 : docLeft;
  449. var minTop = fixed ? 0 : docTop;
  450. var maxLeft = minLeft + winWidth - popupWidth;
  451. var maxTop = minTop + winHeight - popupHeight;
  452. var css = {};
  453. var align = this.align.split(' ');
  454. var className = this.className + '-';
  455. var reverse = {top: 'bottom', bottom: 'top', left: 'right', right: 'left'};
  456. var name = {top: 'top', bottom: 'top', left: 'left', right: 'left'};
  457. var temp = [{
  458. top: top - popupHeight,
  459. bottom: top + height,
  460. left: left - popupWidth,
  461. right: left + width
  462. }, {
  463. top: top,
  464. bottom: top - popupHeight + height,
  465. left: left,
  466. right: left - popupWidth + width
  467. }];
  468. var center = {
  469. left: left + width / 2 - popupWidth / 2,
  470. top: top + height / 2 - popupHeight / 2
  471. };
  472. var range = {
  473. left: [minLeft, maxLeft],
  474. top: [minTop, maxTop]
  475. };
  476. // 超出可视区域重新适应位置
  477. $.each(align, function (i, val) {
  478. // 超出右或下边界:使用左或者上边对齐
  479. if (temp[i][val] > range[name[val]][1]) {
  480. val = align[i] = reverse[val];
  481. }
  482. // 超出左或右边界:使用右或者下边对齐
  483. if (temp[i][val] < range[name[val]][0]) {
  484. align[i] = reverse[val];
  485. }
  486. });
  487. // 一个参数的情况
  488. if (!align[1]) {
  489. name[align[1]] = name[align[0]] === 'left' ? 'top' : 'left';
  490. temp[1][align[1]] = center[name[align[1]]];
  491. }
  492. className += align.join('-');
  493. that.__followSkin = className;
  494. if ($elem) {
  495. popup.addClass(className);
  496. }
  497. css[name[align[0]]] = parseInt(temp[0][align[0]]);
  498. css[name[align[1]]] = parseInt(temp[1][align[1]]);
  499. popup.css(css);
  500. },
  501. // 获取元素相对于页面的位置(包括iframe内的元素)
  502. // 暂时不支持两层以上的 iframe 套嵌
  503. __offset: function (anchor) {
  504. var isNode = anchor.parentNode;
  505. var offset = isNode ? $(anchor).offset() : {
  506. left: anchor.pageX,
  507. top: anchor.pageY
  508. };
  509. anchor = isNode ? anchor : anchor.target;
  510. var ownerDocument = anchor.ownerDocument;
  511. var defaultView = ownerDocument.defaultView || ownerDocument.parentWindow;
  512. if (defaultView == window) {// IE <= 8 只能使用两个等于号
  513. return offset;
  514. }
  515. // {Element Ifarme}
  516. var frameElement = defaultView.frameElement;
  517. var $ownerDocument = $(ownerDocument);
  518. var docLeft = $ownerDocument.scrollLeft();
  519. var docTop = $ownerDocument.scrollTop();
  520. var frameOffset = $(frameElement).offset();
  521. var frameLeft = frameOffset.left;
  522. var frameTop = frameOffset.top;
  523. return {
  524. left: offset.left + frameLeft - docLeft,
  525. top: offset.top + frameTop - docTop
  526. };
  527. },
  528. // 设置屏锁遮罩
  529. __lock: function () {
  530. var that = this;
  531. var popup = this.__popup;
  532. var backdrop = this.__backdrop;
  533. var backdropCss = {
  534. position: 'fixed',
  535. left: 0,
  536. top: 0,
  537. width: '100%',
  538. height: '100%',
  539. overflow: 'hidden',
  540. userSelect: 'none',
  541. opacity: 0,
  542. background: this.backdropBackground
  543. };
  544. popup.addClass(this.className + '-modal');
  545. // 避免遮罩不能盖住上一次的对话框
  546. // 如果当前对话框是上一个对话框创建,点击的那一瞬间它会增长 zIndex 值
  547. Popup.zIndex = Popup.zIndex + 2;
  548. this.__zIndex();
  549. if (!_isFixed) {
  550. $.extend(backdropCss, {
  551. position: 'absolute',
  552. width: $(window).width() + 'px',
  553. height: $(document).height() + 'px'
  554. });
  555. }
  556. backdrop
  557. .css(backdropCss)
  558. .animate({opacity: this.backdropOpacity}, 150)
  559. .insertAfter(popup)
  560. // 锁住模态对话框的 tab 简单办法
  561. // 甚至可以避免焦点落入对话框外的 iframe 中
  562. .attr({tabindex: '0'})
  563. .bind('focus', function () {
  564. that.focus();
  565. });
  566. },
  567. // 卸载屏锁遮罩
  568. __unlock: function () {
  569. if (this.modal) {
  570. this.__popup.removeClass(this.className + '-modal');
  571. this.__backdrop.remove();
  572. delete this.modal;
  573. }
  574. }
  575. });
  576. /** 当前叠加高度 */
  577. Popup.zIndex = 1e4;
  578. /** 顶层浮层的实例 */
  579. Popup.current = null;
  580. // Popup 定义结束
  581. // artDialog 定义开始
  582. var artDialog = function (options, ok, cancel) {
  583. var originalOptions = options = options || {};
  584. if (typeof options === 'string' || options.nodeType === 1) {
  585. options = {content: options, fixed: !_isMobile};
  586. }
  587. options = $.extend(true, {}, artDialog.defaults, options);
  588. options._ = originalOptions;
  589. var id = options.id = options.id || _expando + _count;
  590. var api = artDialog.get(id);
  591. // 如果存在同名的对话框对象,则直接返回
  592. if (api) {
  593. return api.focus();
  594. }
  595. // 目前主流移动设备对fixed支持不好,禁用此特性
  596. if (!_isFixed) {
  597. options.fixed = false;
  598. }
  599. //@edit: 增加对 innerHTML_NC 的处理
  600. if(options.type === "NC"){
  601. options.innerHTML = options.innerHTML_NC
  602. }
  603. // 快捷关闭支持:点击对话框外快速关闭对话框
  604. if (options.quickClose || options.lock) { //@edit: 以前options.quickClose 为true的时候才进行lock,现在增加 options.lock
  605. options.modal = true;
  606. if (!originalOptions.backdropOpacity) {
  607. options.backdropOpacity = 0;
  608. }
  609. }
  610. // 按钮组
  611. if (!$.isArray(options.button)) {
  612. options.button = [];
  613. }
  614. // 取消按钮
  615. if (cancel !== undefined) {
  616. options.cancel = cancel;
  617. }
  618. if (options.cancel) {
  619. options.button.push({
  620. id: 'cancel',
  621. value: options.cancelValue,
  622. callback: options.cancel
  623. });
  624. }
  625. // 确定按钮
  626. if (ok !== undefined) {
  627. options.ok = ok;
  628. }
  629. if (options.ok) {
  630. options.button.push({
  631. id: 'ok',
  632. value: options.okValue,
  633. callback: options.ok,
  634. autofocus: true
  635. });
  636. }
  637. return artDialog.list[id] = new artDialog.create(options);
  638. };
  639. var popup = function () {};
  640. popup.prototype = Popup.prototype;
  641. var prototype = artDialog.prototype = new popup();
  642. artDialog.version = _version;
  643. artDialog.create = function (options) {
  644. var that = this;
  645. // drag 支持 开始
  646. if(options.drag){
  647. var drag = $.artDrag;
  648. this.oncreate = function (api) {
  649. var options = api.options;
  650. var originalOptions = options._;
  651. // 页面地址
  652. var url = options.url;
  653. // 页面加载完毕的事件
  654. var oniframeload = options.oniframeload;
  655. var $iframe;
  656. if (url) {
  657. this.padding = options.padding = 0;
  658. $iframe = $('<iframe />');
  659. $iframe.attr({
  660. src: url,
  661. name: api.id,
  662. width: '100%',
  663. height: '100%',
  664. allowtransparency: 'yes',
  665. frameborder: 'no',
  666. scrolling: 'no'
  667. })
  668. .bind('load', function () {
  669. var test;
  670. try {
  671. // 跨域测试
  672. test = $iframe[0].contentWindow.frameElement;
  673. } catch (e) {}
  674. if (test) {
  675. if (!options.width) {
  676. api.width($iframe.contents().width());
  677. }
  678. if (!options.height) {
  679. api.height($iframe.contents().height());
  680. }
  681. }
  682. if (oniframeload) {
  683. oniframeload.call(api);
  684. }
  685. });
  686. api.addEventListener('beforeremove', function () {
  687. // 重要!需要重置iframe地址,否则下次出现的对话框在IE6、7无法聚焦input
  688. // IE删除iframe后,iframe仍然会留在内存中出现上述问题,置换src是最容易解决的方法
  689. $iframe.attr('src', 'about:blank').remove();
  690. }, false);
  691. api.content($iframe[0]);
  692. api.iframeNode = $iframe[0];
  693. }
  694. // 对于子页面呼出的对话框特殊处理
  695. // 如果对话框配置来自 iframe
  696. if (!(originalOptions instanceof Object)) {
  697. var un = function () {
  698. api.close().remove();
  699. };
  700. // 找到那个 iframe
  701. for (var i = 0; i < frames.length; i ++) {
  702. try {
  703. if (originalOptions instanceof frames[i].Object) {
  704. // 让 iframe 刷新前时候也关闭对话框,
  705. // 防止要执行的对象被强制收回导致 IE 报错:“不能执行已释放 Script 的代码”
  706. $(frames[i]).one('unload', un);
  707. break;
  708. }
  709. } catch (e) {}
  710. }
  711. }
  712. // 拖拽支持
  713. $(api.node).bind(drag.types.start, '[i=dialog]', function (event) {
  714. // 排除气泡类型的对话框
  715. if (!api.follow) {
  716. //api.focus(); //@edit: 修复需要单击两次“关闭”按钮才能关闭弹窗的问题
  717. if (event.offsetX < api.node.clientWidth - 20) { //@edit: 暂时解决IE下单击纵向滚动条后弹出框会跟着鼠标移动的问题
  718. drag.create(api.node, event);
  719. }
  720. }
  721. });
  722. $(api.node).bind(drag.types.start, '[i=content]', function (event) {
  723. event.stopPropagation(); // do something
  724. // return false;
  725. });
  726. };
  727. }else{
  728. this.oncreate = $.noop;
  729. }
  730. // drag 支持 结束
  731. $.extend(this, new Popup());
  732. var $popup = $(this.node).html(options.innerHTML);
  733. this.options = options;
  734. this._popup = $popup;
  735. $.each(options, function (name, value) {
  736. if (typeof that[name] === 'function') {
  737. that[name](value);
  738. } else {
  739. that[name] = value;
  740. }
  741. });
  742. // 更新 zIndex 全局配置
  743. if (options.zIndex) {
  744. Popup.zIndex = options.zIndex;
  745. }
  746. // 设置 ARIA 信息
  747. $popup.attr({
  748. 'aria-labelledby': this._$('title')
  749. .attr('id', 'title:' + this.id).attr('id'),
  750. 'aria-describedby': this._$('content')
  751. .attr('id', 'content:' + this.id).attr('id')
  752. });
  753. // 关闭按钮
  754. this._$('close')
  755. .css('display', this.cancel === false ? 'none' : '')
  756. .attr('title', this.cancelValue)
  757. .bind('click', function (event) {
  758. that._trigger('cancel');
  759. event.preventDefault();
  760. });
  761. // 添加视觉参数
  762. this._$('dialog').addClass(this.skin);
  763. this._$('body').css('padding', this.padding);
  764. // 按钮组点击
  765. $popup.bind('click', '[data-id]', function (event) {
  766. var $this = $(this);
  767. if (!$this.attr('disabled')) {// IE BUG
  768. if($this.data('id')){ //@edit: 增加这个判断,防止点击弹出框边缘时,自动关闭弹出框
  769. that._trigger($this.data('id'));
  770. }
  771. }
  772. event.preventDefault();
  773. });
  774. // 点击遮罩自动关闭对话框
  775. if (options.quickClose) {
  776. $(this.backdrop).bind(
  777. 'onmousedown' in document ? 'mousedown' : 'click',
  778. function () {
  779. that._trigger('cancel');
  780. });
  781. }
  782. // ESC 快捷键关闭对话框
  783. this._esc = function (event) {
  784. var target = event.target;
  785. var nodeName = target.nodeName;
  786. var rinput = /^input|textarea$/i;
  787. var isTop = Popup.current === that;
  788. var keyCode = event.keyCode;
  789. // 避免输入状态中 ESC 误操作关闭
  790. if (!isTop || rinput.test(nodeName) && target.type !== 'button') {
  791. return;
  792. }
  793. if (keyCode === 27) {
  794. that._trigger('cancel');
  795. }
  796. };
  797. $(document).bind('keydown', this._esc);
  798. this.addEventListener('remove', function () {
  799. $(document).unbind('keydown', this._esc);
  800. delete artDialog.list[this.id];
  801. });
  802. _count ++;
  803. this.oncreate(this);
  804. return this;
  805. };
  806. artDialog.create.prototype = prototype;
  807. $.extend(prototype, {
  808. /**
  809. * 显示对话框
  810. * @name artDialog.prototype.show
  811. * @param {HTMLElement Object, Event Object} 指定位置(可选)
  812. */
  813. /**
  814. * 显示对话框(模态)
  815. * @name artDialog.prototype.showModal
  816. * @param {HTMLElement Object, Event Object} 指定位置(可选)
  817. */
  818. /**
  819. * 关闭对话框
  820. * @name artDialog.prototype.close
  821. * @param {String, Number} 返回值,可被 onclose 事件收取(可选)
  822. */
  823. /**
  824. * 销毁对话框
  825. * @name artDialog.prototype.remove
  826. */
  827. /**
  828. * 重置对话框位置
  829. * @name artDialog.prototype.reset
  830. */
  831. /**
  832. * 让对话框聚焦(同时置顶)
  833. * @name artDialog.prototype.focus
  834. */
  835. /**
  836. * 让对话框失焦(同时置顶)
  837. * @name artDialog.prototype.blur
  838. */
  839. /**
  840. * 添加事件
  841. * @param {String} 事件类型
  842. * @param {Function} 监听函数
  843. * @name artDialog.prototype.addEventListener
  844. */
  845. /**
  846. * 删除事件
  847. * @param {String} 事件类型
  848. * @param {Function} 监听函数
  849. * @name artDialog.prototype.removeEventListener
  850. */
  851. /**
  852. * 对话框显示事件,在 show()、showModal() 执行
  853. * @name artDialog.prototype.onshow
  854. * @event
  855. */
  856. /**
  857. * 关闭事件,在 close() 执行
  858. * @name artDialog.prototype.onclose
  859. * @event
  860. */
  861. /**
  862. * 销毁前事件,在 remove() 前执行
  863. * @name artDialog.prototype.onbeforeremove
  864. * @event
  865. */
  866. /**
  867. * 销毁事件,在 remove() 执行
  868. * @name artDialog.prototype.onremove
  869. * @event
  870. */
  871. /**
  872. * 重置事件,在 reset() 执行
  873. * @name artDialog.prototype.onreset
  874. * @event
  875. */
  876. /**
  877. * 焦点事件,在 foucs() 执行
  878. * @name artDialog.prototype.onfocus
  879. * @event
  880. */
  881. /**
  882. * 失焦事件,在 blur() 执行
  883. * @name artDialog.prototype.onblur
  884. * @event
  885. */
  886. /**
  887. * 设置内容
  888. * @param {String, HTMLElement} 内容
  889. */
  890. content: function (html) {
  891. this._$('content').empty('')
  892. [typeof html === 'object' ? 'append' : 'html'](html);
  893. return this.reset();
  894. },
  895. /**
  896. * 设置标题
  897. * @param {String} 标题内容
  898. */
  899. title: function (text) {
  900. this._$('title').text(text);
  901. this._$('header')[text ? 'show' : 'hide']();
  902. return this;
  903. },
  904. /** 设置宽度 */
  905. width: function (value) {
  906. this._$('content').css('width', value);
  907. return this.reset();
  908. },
  909. /** 设置高度 */
  910. height: function (value) {
  911. this._$('content').css('height', value);
  912. return this.reset();
  913. },
  914. /**
  915. * 设置按钮组
  916. * @param {Array, String}
  917. */
  918. button: function (args) {
  919. args = args || [];
  920. var that = this;
  921. var html = '';
  922. this.callbacks = {};
  923. this._$('footer')[args.length ? 'show' : 'hide']();
  924. if (typeof args === 'string') {
  925. html = args;
  926. } else {
  927. $.each(args, function (i, val) {
  928. val.id = val.id || val.value;
  929. that.callbacks[val.id] = val.callback;
  930. html +=
  931. '<button'
  932. + ' type="button"'
  933. + ' data-id="' + val.id + '"'
  934. + (val.disabled ? ' disabled' : '')
  935. + (val.autofocus ? ' autofocus class="ui-dialog-autofocus"' : '')
  936. + '>'
  937. + val.value
  938. + '</button>';
  939. });
  940. }
  941. this._$('button').html(html);
  942. return this;
  943. },
  944. statusbar: function (html) {
  945. this._$('statusbar')
  946. .html(html)[html ? 'show' : 'hide']();
  947. return this;
  948. },
  949. _$: function (i) {
  950. return this._popup.find('[i=' + i + ']');
  951. },
  952. // 触发按钮回调函数
  953. _trigger: function (id) {
  954. var fn = this.callbacks[id];
  955. return typeof fn !== 'function' || fn.call(this) !== false ?
  956. this.close() : this; //@edit: 原来为 ".close().remove()",现在改为".close()" ,去掉 remove 的操作
  957. }
  958. });
  959. /** 最顶层的对话框API */
  960. artDialog.getCurrent = function () {
  961. return Popup.current;
  962. };
  963. /**
  964. * 根据 ID 获取某对话框 API
  965. * @param {String} 对话框 ID
  966. * @return {Object} 对话框 API (实例)
  967. */
  968. artDialog.get = function (id) {
  969. // 从 iframe 传入 window 对象
  970. if (id && id.frameElement) {
  971. var iframe = id.frameElement;
  972. var list = artDialog.list;
  973. var api;
  974. for (var i in list) {
  975. api = list[i];
  976. if (api.node.getElementsByTagName('iframe')[0] === iframe) {
  977. return api;
  978. }
  979. }
  980. // 直接传入 id 的情况
  981. } else if (id) {
  982. return artDialog.list[id];
  983. }
  984. };
  985. artDialog.list = {};
  986. /**
  987. * 默认配置
  988. */
  989. artDialog.defaults = defaults;
  990. // artDialog 定义结束
  991. $.fn.artDialog = artDialog;
  992. $.extend({
  993. artDialog:artDialog
  994. });
  995. return artDialog;
  996. })(jQuery, this);
  997. var fdj_close = function(returnValue){
  998. parent.h3FDJ.close(returnValue).remove();
  999. };
  1000. var showModalDialog = function(url,obj,position,callback,width,height){
  1001. width = width || 970;
  1002. height = height || ($(window).height()-40);
  1003. var div_iframe = $("#dialog_div_fdj");
  1004. if(div_iframe[0] == null){
  1005. div_iframe = $("<div id='dialog_div_fdj' style='display: none'></div>");
  1006. }
  1007. div_iframe.html("<iframe id='dialog_fdj' class='ncDialog' width='100%' height='100%' frameborder=”no” border=”0″></iframe>");
  1008. // div_iframe.html("<iframe class='ncDialog' src='" + url + "' width='100%' height='100%' frameborder=”no” border=”0″></iframe>");
  1009. $("body").append(div_iframe);
  1010. var dialogContent = $(".ncDialog");
  1011. var config = {
  1012. id: "dialog_fdj",
  1013. type: "NC",
  1014. width:width,
  1015. height:height,
  1016. backdropOpacity:0.5,
  1017. content: dialogContent[0],
  1018. drag:false,
  1019. fixed:true,
  1020. lock:true
  1021. };
  1022. if(callback){
  1023. config.onclose = callback;
  1024. }
  1025. window.h3FDJ = $.artDialog(config);
  1026. window.h3FDJ.show();
  1027. document.getElementById('dialog_fdj').src = url;
  1028. $("#dialog_fdj").load(function(){
  1029. if(dialog_fdj.contentWindow){ //CHROME
  1030. dialog_fdj.contentWindow.dialogArguments = obj;
  1031. }else{ //IE
  1032. dialog_fdj.dialogArguments = obj;
  1033. }
  1034. });
  1035. };
  1036. //@ sourceURL=jquery.artDialog.js