农燊高科官方网站
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

dialog.js 11 KiB

4 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*!
  2. * artDialog
  3. * Date: 2014-11-09
  4. * https://github.com/aui/artDialog
  5. * (c) 2009-2014 TangBin, http://www.planeArt.cn
  6. *
  7. * This is licensed under the GNU LGPL, version 2.1 or later.
  8. * For details, see: http://www.gnu.org/licenses/lgpl-2.1.html
  9. */
  10. define(['jquery','popup','dialogConf'],function ($,Popup,defaults) {
  11. //var $ = require('jquery');
  12. //var Popup = require('popup');
  13. //var defaults = require('dialogConf');
  14. var css = defaults.cssUri;
  15. // css loader: RequireJS & SeaJS
  16. if (css) {
  17. var fn = require[require.toUrl ? 'toUrl' : 'resolve'];
  18. if (fn) {
  19. css = fn(css);
  20. css = '<link rel="stylesheet" href="' + css + '" />';
  21. if ($('base')[0]) {
  22. $('base').before(css);
  23. } else {
  24. $('head').append(css);
  25. }
  26. }
  27. }
  28. var _count = 0;
  29. var _expando = new Date() - 0; // Date.now()
  30. var _isIE6 = !('minWidth' in $('html')[0].style);
  31. var _isMobile = 'createTouch' in document && !('onmousemove' in document)
  32. || /(iPhone|iPad|iPod)/i.test(navigator.userAgent);
  33. var _isFixed = !_isIE6 && !_isMobile;
  34. var artDialog = function (options, ok, cancel) {
  35. var originalOptions = options = options || {};
  36. if (typeof options === 'string' || options.nodeType === 1) {
  37. options = {content: options, fixed: !_isMobile};
  38. }
  39. options = $.extend(true, {}, artDialog.defaults, options);
  40. options.original = originalOptions;
  41. var id = options.id = options.id || _expando + _count;
  42. var api = artDialog.get(id);
  43. // 如果存在同名的对话框对象,则直接返回
  44. if (api) {
  45. return api.focus();
  46. }
  47. // 目前主流移动设备对fixed支持不好,禁用此特性
  48. if (!_isFixed) {
  49. options.fixed = false;
  50. }
  51. // 快捷关闭支持:点击对话框外快速关闭对话框
  52. if (options.quickClose) {
  53. options.modal = true;
  54. options.backdropOpacity = 0;
  55. }
  56. // 按钮组
  57. if (!$.isArray(options.button)) {
  58. options.button = [];
  59. }
  60. // 取消按钮
  61. if (cancel !== undefined) {
  62. options.cancel = cancel;
  63. }
  64. if (options.cancel) {
  65. options.button.push({
  66. id: 'cancel',
  67. value: options.cancelValue,
  68. callback: options.cancel,
  69. display: options.cancelDisplay
  70. });
  71. }
  72. // 确定按钮
  73. if (ok !== undefined) {
  74. options.ok = ok;
  75. }
  76. if (options.ok) {
  77. options.button.push({
  78. id: 'ok',
  79. value: options.okValue,
  80. callback: options.ok,
  81. autofocus: true
  82. });
  83. }
  84. return artDialog.list[id] = new artDialog.create(options);
  85. };
  86. var popup = function () {};
  87. popup.prototype = Popup.prototype;
  88. var prototype = artDialog.prototype = new popup();
  89. artDialog.create = function (options) {
  90. var that = this;
  91. $.extend(this, new Popup());
  92. var originalOptions = options.original;
  93. var $popup = $(this.node).html(options.innerHTML);
  94. var $backdrop = $(this.backdrop);
  95. this.options = options;
  96. this._popup = $popup;
  97. $.each(options, function (name, value) {
  98. if (typeof that[name] === 'function') {
  99. that[name](value);
  100. } else {
  101. that[name] = value;
  102. }
  103. });
  104. // 更新 zIndex 全局配置
  105. if (options.zIndex) {
  106. Popup.zIndex = options.zIndex;
  107. }
  108. // 设置 ARIA 信息
  109. $popup.attr({
  110. 'aria-labelledby': this._$('title')
  111. .attr('id', 'title:' + this.id).attr('id'),
  112. 'aria-describedby': this._$('content')
  113. .attr('id', 'content:' + this.id).attr('id')
  114. });
  115. // 关闭按钮
  116. this._$('close')
  117. .css('display', this.cancel === false ? 'none' : '')
  118. .attr('title', this.cancelValue)
  119. .on('click', function (event) {
  120. that._trigger('cancel');
  121. event.preventDefault();
  122. });
  123. // 添加视觉参数
  124. this._$('dialog').addClass(this.skin);
  125. this._$('body').css('padding', this.padding);
  126. // 点击任意空白处关闭对话框
  127. if (options.quickClose) {
  128. $backdrop
  129. .on(
  130. 'onmousedown' in document ? 'mousedown' : 'click',
  131. function () {
  132. that._trigger('cancel');
  133. return false;// 阻止抢夺焦点
  134. });
  135. }
  136. // 遮罩设置
  137. this.addEventListener('show', function () {
  138. $backdrop.css({
  139. opacity: 0,
  140. background: options.backdropBackground
  141. }).animate(
  142. {opacity: options.backdropOpacity}
  143. , 150);
  144. });
  145. // ESC 快捷键关闭对话框
  146. this._esc = function (event) {
  147. var target = event.target;
  148. var nodeName = target.nodeName;
  149. var rinput = /^input|textarea$/i;
  150. var isTop = Popup.current === that;
  151. var keyCode = event.keyCode;
  152. // 避免输入状态中 ESC 误操作关闭
  153. if (!isTop || rinput.test(nodeName) && target.type !== 'button') {
  154. return;
  155. }
  156. if (keyCode === 27) {
  157. that._trigger('cancel');
  158. }
  159. };
  160. $(document).on('keydown', this._esc);
  161. this.addEventListener('remove', function () {
  162. $(document).off('keydown', this._esc);
  163. delete artDialog.list[this.id];
  164. });
  165. _count ++;
  166. artDialog.oncreate(this);
  167. return this;
  168. };
  169. artDialog.create.prototype = prototype;
  170. $.extend(prototype, {
  171. /**
  172. * 显示对话框
  173. * @name artDialog.prototype.show
  174. * @param {HTMLElement Object, Event Object} 指定位置(可选)
  175. */
  176. /**
  177. * 显示对话框(模态)
  178. * @name artDialog.prototype.showModal
  179. * @param {HTMLElement Object, Event Object} 指定位置(可选)
  180. */
  181. /**
  182. * 关闭对话框
  183. * @name artDialog.prototype.close
  184. * @param {String, Number} 返回值,可被 onclose 事件收取(可选)
  185. */
  186. /**
  187. * 销毁对话框
  188. * @name artDialog.prototype.remove
  189. */
  190. /**
  191. * 重置对话框位置
  192. * @name artDialog.prototype.reset
  193. */
  194. /**
  195. * 让对话框聚焦(同时置顶)
  196. * @name artDialog.prototype.focus
  197. */
  198. /**
  199. * 让对话框失焦(同时置顶)
  200. * @name artDialog.prototype.blur
  201. */
  202. /**
  203. * 添加事件
  204. * @param {String} 事件类型
  205. * @param {Function} 监听函数
  206. * @name artDialog.prototype.addEventListener
  207. */
  208. /**
  209. * 删除事件
  210. * @param {String} 事件类型
  211. * @param {Function} 监听函数
  212. * @name artDialog.prototype.removeEventListener
  213. */
  214. /**
  215. * 对话框显示事件,在 show()、showModal() 执行
  216. * @name artDialog.prototype.onshow
  217. * @event
  218. */
  219. /**
  220. * 关闭事件,在 close() 执行
  221. * @name artDialog.prototype.onclose
  222. * @event
  223. */
  224. /**
  225. * 销毁前事件,在 remove() 前执行
  226. * @name artDialog.prototype.onbeforeremove
  227. * @event
  228. */
  229. /**
  230. * 销毁事件,在 remove() 执行
  231. * @name artDialog.prototype.onremove
  232. * @event
  233. */
  234. /**
  235. * 重置事件,在 reset() 执行
  236. * @name artDialog.prototype.onreset
  237. * @event
  238. */
  239. /**
  240. * 焦点事件,在 foucs() 执行
  241. * @name artDialog.prototype.onfocus
  242. * @event
  243. */
  244. /**
  245. * 失焦事件,在 blur() 执行
  246. * @name artDialog.prototype.onblur
  247. * @event
  248. */
  249. /**
  250. * 设置内容
  251. * @param {String, HTMLElement} 内容
  252. */
  253. content: function (html) {
  254. var $content = this._$('content');
  255. // HTMLElement
  256. if (typeof html === 'object') {
  257. html = $(html);
  258. $content.empty('').append(html.show());
  259. this.addEventListener('beforeremove', function () {
  260. $('body').append(html.hide());
  261. });
  262. // String
  263. } else {
  264. $content.html(html);
  265. }
  266. return this.reset();
  267. },
  268. /**
  269. * 设置标题
  270. * @param {String} 标题内容
  271. */
  272. title: function (text) {
  273. this._$('title').text(text);
  274. this._$('header')[text ? 'show' : 'hide']();
  275. return this;
  276. },
  277. /** 设置宽度 */
  278. width: function (value) {
  279. this._$('content').css('width', value);
  280. return this.reset();
  281. },
  282. /** 设置高度 */
  283. height: function (value) {
  284. this._$('content').css('height', value);
  285. return this.reset();
  286. },
  287. /**
  288. * 设置按钮组
  289. * @param {Array, String}
  290. * Options: value, callback, autofocus, disabled
  291. */
  292. button: function (args) {
  293. args = args || [];
  294. var that = this;
  295. var html = '';
  296. var number = 0;
  297. this.callbacks = {};
  298. if (typeof args === 'string') {
  299. html = args;
  300. number ++;
  301. } else {
  302. $.each(args, function (i, val) {
  303. var id = val.id = val.id || val.value;
  304. var style = '';
  305. that.callbacks[id] = val.callback;
  306. if (val.display === false) {
  307. style = ' style="display:none"';
  308. } else {
  309. number ++;
  310. }
  311. html +=
  312. '<button'
  313. + ' type="button"'
  314. + ' i-id="' + id + '"'
  315. + style
  316. + (val.disabled ? ' disabled' : '')
  317. + (val.autofocus ? ' autofocus class="ui-dialog-autofocus"' : '')
  318. + '>'
  319. + val.value
  320. + '</button>';
  321. that._$('button')
  322. .on('click', '[i-id=' + id +']', function (event) {
  323. var $this = $(this);
  324. if (!$this.attr('disabled')) {// IE BUG
  325. that._trigger(id);
  326. }
  327. event.preventDefault();
  328. });
  329. });
  330. }
  331. this._$('button').html(html);
  332. this._$('footer')[number ? 'show' : 'hide']();
  333. return this;
  334. },
  335. statusbar: function (html) {
  336. this._$('statusbar')
  337. .html(html)[html ? 'show' : 'hide']();
  338. return this;
  339. },
  340. _$: function (i) {
  341. return this._popup.find('[i=' + i + ']');
  342. },
  343. // 触发按钮回调函数
  344. _trigger: function (id) {
  345. var fn = this.callbacks[id];
  346. return typeof fn !== 'function' || fn.call(this) !== false ?
  347. this.close().remove() : this;
  348. }
  349. });
  350. artDialog.oncreate = $.noop;
  351. /** 获取最顶层的对话框API */
  352. artDialog.getCurrent = function () {
  353. return Popup.current;
  354. };
  355. /**
  356. * 根据 ID 获取某对话框 API
  357. * @param {String} 对话框 ID
  358. * @return {Object} 对话框 API (实例)
  359. */
  360. artDialog.get = function (id) {
  361. return id === undefined
  362. ? artDialog.list
  363. : artDialog.list[id];
  364. };
  365. artDialog.list = {};
  366. /**
  367. * 默认配置
  368. */
  369. artDialog.defaults = defaults;
  370. return artDialog;
  371. });