Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

notification.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. import _setImmediate from 'babel-runtime/core-js/set-immediate';
  2. import _Object$keys from 'babel-runtime/core-js/object/keys';
  3. import _Promise from 'babel-runtime/core-js/promise';
  4. import _extends from 'babel-runtime/helpers/extends';
  5. /*
  6. Copyright 2013-2015 ASIAL CORPORATION
  7. Licensed under the Apache License, Version 2.0 (the "License");
  8. you may not use this file except in compliance with the License.
  9. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. */
  17. import util from './util';
  18. import contentReady from './content-ready';
  19. import ToastQueue from './internal/toast-queue';
  20. var _setAttributes = function _setAttributes(element, options) {
  21. ['id', 'class', 'animation'].forEach(function (a) {
  22. return options.hasOwnProperty(a) && element.setAttribute(a, options[a]);
  23. });
  24. if (options.modifier) {
  25. util.addModifier(element, options.modifier);
  26. }
  27. };
  28. var _normalizeArguments = function _normalizeArguments(message) {
  29. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  30. var defaults = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  31. options = _extends({}, options);
  32. typeof message === 'string' ? options.message = message : options = message;
  33. if (!options || !options.message && !options.messageHTML) {
  34. util.throw('Notifications must contain a message');
  35. }
  36. if (options.hasOwnProperty('buttonLabels') || options.hasOwnProperty('buttonLabel')) {
  37. options.buttonLabels = options.buttonLabels || options.buttonLabel;
  38. if (!Array.isArray(options.buttonLabels)) {
  39. options.buttonLabels = [options.buttonLabels || ''];
  40. }
  41. }
  42. return util.extend({
  43. compile: function compile(param) {
  44. return param;
  45. },
  46. callback: function callback(param) {
  47. return param;
  48. },
  49. animation: 'default',
  50. cancelable: false,
  51. primaryButtonIndex: (options.buttonLabels || defaults.buttonLabels || []).length - 1
  52. }, defaults, options);
  53. };
  54. /**
  55. * @object ons.notification
  56. * @category dialog
  57. * @tutorial vanilla/Reference/notification
  58. * @description
  59. * [en]
  60. * Utility methods to create different kinds of notifications. There are three methods available:
  61. *
  62. * * `ons.notification.alert()`
  63. * * `ons.notification.confirm()`
  64. * * `ons.notification.prompt()`
  65. * * `ons.notification.toast()`
  66. *
  67. * It will automatically display a Material Design dialog on Android devices.
  68. * [/en]
  69. * [ja]いくつかの種類のアラートダイアログを作成するためのユーティリティメソッドを収めたオブジェクトです。[/ja]
  70. * @example
  71. * ons.notification.alert('Hello, world!');
  72. *
  73. * ons.notification.confirm('Are you ready?')
  74. * .then(
  75. * function(answer) {
  76. * if (answer === 1) {
  77. * ons.notification.alert('Let\'s go!');
  78. * }
  79. * }
  80. * );
  81. *
  82. * ons.notification.prompt('How old are ?')
  83. * .then(
  84. * function(age) {
  85. * ons.notification.alert('You are ' + age + ' years old.');
  86. * }
  87. * );
  88. */
  89. var notification = {};
  90. notification._createAlertDialog = function () {
  91. for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
  92. params[_key] = arguments[_key];
  93. }
  94. return new _Promise(function (resolve) {
  95. var options = _normalizeArguments.apply(undefined, params);
  96. util.checkMissingImport('AlertDialog', 'AlertDialogButton');
  97. // Prompt input string
  98. var inputString = '';
  99. if (options.isPrompt) {
  100. inputString = '\n <input\n class="text-input text-input--underbar"\n type="' + (options.inputType || 'text') + '"\n placeholder="' + (options.placeholder || '') + '"\n value="' + (options.defaultValue || '') + '"\n style="width: 100%; margin-top: 10px;"\n />\n ';
  101. }
  102. // Buttons string
  103. var buttons = '';
  104. options.buttonLabels.forEach(function (label, index) {
  105. buttons += '\n <ons-alert-dialog-button\n class="\n ' + (index === options.primaryButtonIndex ? ' alert-dialog-button--primal' : '') + '\n ' + (options.buttonLabels.length <= 2 ? ' alert-dialog-button--rowfooter' : '') + '\n "\n style="position: relative;">\n ' + label + '\n </ons-alert-dialog-button>\n ';
  106. });
  107. // Dialog Element
  108. var el = {};
  109. var _destroyDialog = function _destroyDialog() {
  110. if (el.dialog.onDialogCancel) {
  111. el.dialog.removeEventListener('dialog-cancel', el.dialog.onDialogCancel);
  112. }
  113. _Object$keys(el).forEach(function (key) {
  114. return delete el[key];
  115. });
  116. el = null;
  117. if (options.destroy instanceof Function) {
  118. options.destroy();
  119. }
  120. };
  121. el.dialog = document.createElement('ons-alert-dialog');
  122. el.dialog.innerHTML = '\n <div class="alert-dialog-mask"\n style="\n ' + (options.maskColor ? 'background-color: ' + options.maskColor : '') + '\n "></div>\n <div class="alert-dialog">\n <div class="alert-dialog-container">\n <div class="alert-dialog-title">\n ' + (options.title || '') + '\n </div>\n <div class="alert-dialog-content">\n ' + (options.message || options.messageHTML) + '\n ' + inputString + '\n </div>\n <div class="\n alert-dialog-footer\n ' + (options.buttonLabels.length <= 2 ? ' alert-dialog-footer--rowfooter' : '') + '\n ">\n ' + buttons + '\n </div>\n </div>\n </div>\n ';
  123. contentReady(el.dialog);
  124. // Set attributes
  125. _setAttributes(el.dialog, options);
  126. // Prompt events
  127. if (options.isPrompt && options.submitOnEnter) {
  128. el.input = el.dialog.querySelector('.text-input');
  129. el.input.onkeypress = function (event) {
  130. if (event.keyCode === 13) {
  131. el.dialog.hide().then(function () {
  132. if (el) {
  133. var resolveValue = el.input.value;
  134. _destroyDialog();
  135. options.callback(resolveValue);
  136. resolve(resolveValue);
  137. }
  138. });
  139. }
  140. };
  141. }
  142. // Button events
  143. el.footer = el.dialog.querySelector('.alert-dialog-footer');
  144. util.arrayFrom(el.dialog.querySelectorAll('.alert-dialog-button')).forEach(function (buttonElement, index) {
  145. buttonElement.onclick = function () {
  146. el.dialog.hide().then(function () {
  147. if (el) {
  148. var resolveValue = index;
  149. if (options.isPrompt) {
  150. resolveValue = index === options.primaryButtonIndex ? el.input.value : null;
  151. }
  152. el.dialog.remove();
  153. _destroyDialog();
  154. options.callback(resolveValue);
  155. resolve(resolveValue);
  156. }
  157. });
  158. };
  159. el.footer.appendChild(buttonElement);
  160. });
  161. // Cancel events
  162. if (options.cancelable) {
  163. el.dialog.cancelable = true;
  164. el.dialog.onDialogCancel = function () {
  165. _setImmediate(function () {
  166. el.dialog.remove();
  167. _destroyDialog();
  168. });
  169. var resolveValue = options.isPrompt ? null : -1;
  170. options.callback(resolveValue);
  171. resolve(resolveValue);
  172. };
  173. el.dialog.addEventListener('dialog-cancel', el.dialog.onDialogCancel, false);
  174. }
  175. // Show dialog
  176. document.body.appendChild(el.dialog);
  177. options.compile(el.dialog);
  178. _setImmediate(function () {
  179. el.dialog.show().then(function () {
  180. if (el.input && options.isPrompt && options.autofocus) {
  181. var strLength = el.input.value.length;
  182. el.input.focus();
  183. el.input.setSelectionRange(strLength, strLength);
  184. }
  185. });
  186. });
  187. });
  188. };
  189. /**
  190. * @method alert
  191. * @signature alert(message [, options] | options)
  192. * @return {Promise}
  193. * [en]Will resolve to the index of the button that was pressed or `-1` when canceled.[/en]
  194. * [ja][/ja]
  195. * @param {String} message
  196. * [en]Notification message. This argument is optional but if it's not defined either `options.message` or `options.messageHTML` must be defined instead.[/en]
  197. * [ja][/ja]
  198. * @param {Object} options
  199. * [en]Parameter object.[/en]
  200. * [ja]オプションを指定するオブジェクトです。[/ja]
  201. * @param {String} [options.message]
  202. * [en]Notification message.[/en]
  203. * [ja]アラートダイアログに表示する文字列を指定します。[/ja]
  204. * @param {String} [options.messageHTML]
  205. * [en]Notification message in HTML.[/en]
  206. * [ja]アラートダイアログに表示するHTMLを指定します。[/ja]
  207. * @param {String | Array} [options.buttonLabels]
  208. * [en]Labels for the buttons. Default is `"OK"`.[/en]
  209. * [ja]確認ボタンのラベルを指定します。"OK"がデフォルトです。[/ja]
  210. * @param {Number} [options.primaryButtonIndex]
  211. * [en]Index of primary button. Default is the last one.[/en]
  212. * [ja]プライマリボタンのインデックスを指定します。デフォルトは 0 です。[/ja]
  213. * @param {Boolean} [options.cancelable]
  214. * [en]Whether the dialog is cancelable or not. Default is `false`. If the dialog is cancelable it can be closed by clicking the background or pressing the Android back button.[/en]
  215. * [ja]ダイアログがキャンセル可能かどうかを指定します。[/ja]
  216. * @param {String} [options.animation]
  217. * [en]Animation name. Available animations are `none` and `fade`. Default is `fade`.[/en]
  218. * [ja]アラートダイアログを表示する際のアニメーション名を指定します。"none", "fade"のいずれかを指定できます。[/ja]
  219. * @param {String} [options.id]
  220. * [en]The `<ons-alert-dialog>` element's ID.[/en]
  221. * [ja]ons-alert-dialog要素のID。[/ja]
  222. * @param {String} [options.class]
  223. * [en]The `<ons-alert-dialog>` element's class.[/en]
  224. * [ja]ons-alert-dialog要素のclass。[/ja]
  225. * @param {String} [options.title]
  226. * [en]Dialog title. Default is `"Alert"`.[/en]
  227. * [ja]アラートダイアログの上部に表示するタイトルを指定します。"Alert"がデフォルトです。[/ja]
  228. * @param {String} [options.modifier]
  229. * [en]Modifier for the dialog.[/en]
  230. * [ja]アラートダイアログのmodifier属性の値を指定します。[/ja]
  231. * @param {String} [options.maskColor]
  232. * [en]Color of the background mask. Default is "rgba(0, 0, 0, 0.2)" ("rgba(0, 0, 0, 0.3)" for Material).[/en]
  233. * [ja]背景のマスクの色を指定します。"rgba(0, 0, 0, 0.2)"がデフォルト値です。[/ja]
  234. * @param {Function} [options.callback]
  235. * [en]Function that executes after dialog has been closed.[/en]
  236. * [ja]アラートダイアログが閉じられた時に呼び出される関数オブジェクトを指定します。[/ja]
  237. * @description
  238. * [en]
  239. * Display an alert dialog to show the user a message.
  240. *
  241. * The content of the message can be either simple text or HTML.
  242. *
  243. * It can be called in the following ways:
  244. *
  245. * ```
  246. * ons.notification.alert(message, options);
  247. * ons.notification.alert(options);
  248. * ```
  249. *
  250. * Must specify either `message` or `messageHTML`.
  251. * [/en]
  252. * [ja]
  253. * ユーザーへメッセージを見せるためのアラートダイアログを表示します。
  254. * 表示するメッセージは、テキストかもしくはHTMLを指定できます。
  255. * このメソッドの引数には、options.messageもしくはoptions.messageHTMLのどちらかを必ず指定する必要があります。
  256. * [/ja]
  257. */
  258. notification.alert = function (message, options) {
  259. return notification._createAlertDialog(message, options, {
  260. buttonLabels: ['OK'],
  261. title: 'Alert'
  262. });
  263. };
  264. /**
  265. * @method confirm
  266. * @signature confirm(message [, options] | options)
  267. * @return {Promise}
  268. * [en]Will resolve to the index of the button that was pressed or `-1` when canceled.[/en]
  269. * [ja][/ja]
  270. * @param {String} message
  271. * [en]Notification message. This argument is optional but if it's not defined either `options.message` or `options.messageHTML` must be defined instead.[/en]
  272. * [ja][/ja]
  273. * @param {Object} options
  274. * [en]Parameter object.[/en]
  275. * @param {Array} [options.buttonLabels]
  276. * [en]Labels for the buttons. Default is `["Cancel", "OK"]`.[/en]
  277. * [ja]ボタンのラベルの配列を指定します。["Cancel", "OK"]がデフォルトです。[/ja]
  278. * @param {Number} [options.primaryButtonIndex]
  279. * [en]Index of primary button. Default is the last one.[/en]
  280. * [ja]プライマリボタンのインデックスを指定します。デフォルトは 1 です。[/ja]
  281. * @description
  282. * [en]
  283. * Display a dialog to ask the user for confirmation. Extends `alert()` parameters.
  284. * The default button labels are `"Cancel"` and `"OK"` but they can be customized.
  285. *
  286. * It can be called in the following ways:
  287. *
  288. * ```
  289. * ons.notification.confirm(message, options);
  290. * ons.notification.confirm(options);
  291. * ```
  292. *
  293. * Must specify either `message` or `messageHTML`.
  294. * [/en]
  295. * [ja]
  296. * ユーザに確認を促すダイアログを表示します。
  297. * デオルとのボタンラベルは、"Cancel"と"OK"ですが、これはこのメソッドの引数でカスタマイズできます。
  298. * このメソッドの引数には、options.messageもしくはoptions.messageHTMLのどちらかを必ず指定する必要があります。
  299. * [/ja]
  300. */
  301. notification.confirm = function (message, options) {
  302. return notification._createAlertDialog(message, options, {
  303. buttonLabels: ['Cancel', 'OK'],
  304. title: 'Confirm'
  305. });
  306. };
  307. /**
  308. * @method prompt
  309. * @signature prompt(message [, options] | options)
  310. * @param {String} message
  311. * [en]Notification message. This argument is optional but if it's not defined either `options.message` or `options.messageHTML` must be defined instead.[/en]
  312. * [ja][/ja]
  313. * @return {Promise}
  314. * [en]Will resolve to the input value when the dialog is closed or `null` when canceled.[/en]
  315. * [ja][/ja]
  316. * @param {Object} options
  317. * [en]Parameter object.[/en]
  318. * [ja]オプションを指定するオブジェクトです。[/ja]
  319. * @param {String | Array} [options.buttonLabels]
  320. * [en]Labels for the buttons. Default is `"OK"`.[/en]
  321. * [ja]確認ボタンのラベルを指定します。"OK"がデフォルトです。[/ja]
  322. * @param {Number} [options.primaryButtonIndex]
  323. * [en]Index of primary button. Default is the last one.[/en]
  324. * [ja]プライマリボタンのインデックスを指定します。デフォルトは 0 です。[/ja]
  325. * @param {String} [options.placeholder]
  326. * [en]Placeholder for the text input.[/en]
  327. * [ja]テキスト欄のプレースホルダに表示するテキストを指定します。[/ja]
  328. * @param {String} [options.defaultValue]
  329. * [en]Default value for the text input.[/en]
  330. * [ja]テキスト欄のデフォルトの値を指定します。[/ja]
  331. * @param {String} [options.inputType]
  332. * [en]Type of the input element (`password`, `date`...). Default is `text`.[/en]
  333. * [ja][/ja]
  334. * @param {Boolean} [options.autofocus]
  335. * [en]Autofocus the input element. Default is `true`. In Cordova, `KeyboardDisplayRequiresUserAction` in `config.xml` must be `false` to activate this feature.[/en]
  336. * [ja]input要素に自動的にフォーカスするかどうかを指定します。デフォルトはtrueです。Cordova環境では、この機能を有効にするためには `config.xml` で `KeyboardDisplayRequiresUserAction` を `false` に設定する必要があります。[/ja]
  337. * @param {Boolean} [options.submitOnEnter]
  338. * [en]Submit automatically when enter is pressed. Default is `true`.[/en]
  339. * [ja]Enterが押された際にそのformをsubmitするかどうかを指定します。デフォルトはtrueです。[/ja]
  340. * @description
  341. * [en]
  342. * Display a dialog with a prompt to ask the user a question. Extends `alert()` parameters.
  343. *
  344. * It can be called in the following ways:
  345. *
  346. * ```
  347. * ons.notification.prompt(message, options);
  348. * ons.notification.prompt(options);
  349. * ```
  350. *
  351. * Must specify either `message` or `messageHTML`.
  352. * [/en]
  353. * [ja]
  354. * ユーザーに入力を促すダイアログを表示します。
  355. * このメソッドの引数には、options.messageもしくはoptions.messageHTMLのどちらかを必ず指定する必要があります。
  356. * [/ja]
  357. */
  358. notification.prompt = function (message, options) {
  359. return notification._createAlertDialog(message, options, {
  360. buttonLabels: ['OK'],
  361. title: 'Alert',
  362. isPrompt: true,
  363. autofocus: true,
  364. submitOnEnter: true
  365. });
  366. };
  367. /**
  368. * @method toast
  369. * @signature toast(message [, options] | options)
  370. * @return {Promise}
  371. * [en]Will resolve when the toast is hidden.[/en]
  372. * [ja][/ja]
  373. * @param {String} message
  374. * [en]Toast message. This argument is optional but if it's not defined then `options.message` must be defined instead.[/en]
  375. * [ja][/ja]
  376. * @param {Object} options
  377. * [en]Parameter object.[/en]
  378. * [ja]オプションを指定するオブジェクトです。[/ja]
  379. * @param {String} [options.message]
  380. * [en]Notification message.[/en]
  381. * [ja]トーストに表示する文字列を指定します。[/ja]
  382. * @param {String} [options.buttonLabel]
  383. * [en]Label for the button.[/en]
  384. * [ja]確認ボタンのラベルを指定します。[/ja]
  385. * @param {String} [options.animation]
  386. * [en]Animation name. Available animations are `none`, `fade`, `ascend`, `lift` and `fall`. Default is `ascend` for Android and `lift` for iOS.[/en]
  387. * [ja]トーストを表示する際のアニメーション名を指定します。"none", "fade", "ascend", "lift", "fall"のいずれかを指定できます。[/ja]
  388. * @param {Number} [options.timeout]
  389. * [en]Number of miliseconds where the toast is visible before hiding automatically.[/en]
  390. * [ja][/ja]
  391. * @param {Boolean} [options.force]
  392. * [en]If `true`, the toast skips the notification queue and is shown immediately. Defaults to `false`.[/en]
  393. * [ja][/ja]
  394. * @param {String} [options.id]
  395. * [en]The `<ons-toast>` element's ID.[/en]
  396. * [ja]ons-toast要素のID。[/ja]
  397. * @param {String} [options.class]
  398. * [en]The `<ons-toast>` element's class.[/en]
  399. * [ja]ons-toast要素のclass。[/ja]
  400. * @param {String} [options.modifier]
  401. * [en]Modifier for the element.[/en]
  402. * [ja]トーストのmodifier属性の値を指定します。[/ja]
  403. * @param {Function} [options.callback]
  404. * [en]Function that executes after toast has been hidden.[/en]
  405. * [ja]トーストが閉じられた時に呼び出される関数オブジェクトを指定します。[/ja]
  406. * @description
  407. * [en]
  408. * Display a simple notification toast with an optional button that can be used for simple actions.
  409. *
  410. * It can be called in the following ways:
  411. *
  412. * ```
  413. * ons.notification.toast(message, options);
  414. * ons.notification.toast(options);
  415. * ```
  416. * [/en]
  417. * [ja][/ja]
  418. */
  419. notification.toast = function (message, options) {
  420. var promise = new _Promise(function (resolve) {
  421. util.checkMissingImport('Toast'); // Throws error, must be inside promise
  422. options = _normalizeArguments(message, options, {
  423. timeout: 0,
  424. force: false
  425. });
  426. var toast = util.createElement('\n <ons-toast>\n ' + options.message + '\n ' + (options.buttonLabels ? '<button>' + options.buttonLabels[0] + '</button>' : '') + '\n </ons-toast>\n ');
  427. _setAttributes(toast, options);
  428. var finish = function finish(value) {
  429. if (toast) {
  430. toast.hide().then(function () {
  431. if (toast) {
  432. toast.remove();
  433. toast = null;
  434. options.callback(value);
  435. resolve(value);
  436. }
  437. });
  438. }
  439. };
  440. if (options.buttonLabels) {
  441. util.findChild(toast._toast, 'button').onclick = function () {
  442. return finish(0);
  443. };
  444. }
  445. document.body.appendChild(toast);
  446. options.compile(toast);
  447. var show = function show() {
  448. toast.parentElement && toast.show(options).then(function () {
  449. if (options.timeout) {
  450. setTimeout(function () {
  451. return finish(-1);
  452. }, options.timeout);
  453. }
  454. });
  455. };
  456. _setImmediate(function () {
  457. return options.force ? show() : ToastQueue.add(show, promise);
  458. });
  459. });
  460. return promise;
  461. };
  462. export default notification;