Nessuna descrizione

action-sheet.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import _setImmediate from 'babel-runtime/core-js/set-immediate';
  2. import _extends from 'babel-runtime/helpers/extends';
  3. import _Promise from 'babel-runtime/core-js/promise';
  4. /*
  5. Copyright 2013-2015 ASIAL CORPORATION
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. import util from './util';
  17. // Validate parameters
  18. var checkOptions = function checkOptions(options) {
  19. var err = function err(prop) {
  20. var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Function';
  21. return util.throw('"options.' + prop + '" must be an instance of ' + type);
  22. };
  23. var hasOwnProperty = function hasOwnProperty(prop) {
  24. return Object.hasOwnProperty.call(options, prop);
  25. };
  26. var instanceOf = function instanceOf(prop) {
  27. var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Function;
  28. return options[prop] instanceof type;
  29. };
  30. var b = 'buttons',
  31. cb = 'callback',
  32. c = 'compile',
  33. d = 'destroy';
  34. (!hasOwnProperty(b) || !instanceOf(b, Array)) && err(b, 'Array');
  35. hasOwnProperty(cb) && !instanceOf(cb) && err(cb);
  36. hasOwnProperty(c) && !instanceOf(c) && err(c);
  37. hasOwnProperty(d) && !instanceOf(d) && err(d);
  38. };
  39. // Action Sheet
  40. export default (function () {
  41. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  42. return new _Promise(function (resolve) {
  43. util.checkMissingImport('ActionSheet');
  44. checkOptions(options);
  45. // Main component
  46. var actionSheet = util.createElement('\n <ons-action-sheet\n ' + (options.title ? 'title="' + options.title + '"' : '') + '\n ' + (options.cancelable ? 'cancelable' : '') + '\n ' + (options.modifier ? 'modifier="' + options.modifier + '"' : '') + '\n ' + (options.maskColor ? 'mask-color="' + options.maskColor + '"' : '') + '\n ' + (options.id ? 'id="' + options.id + '"' : '') + '\n ' + (options.class ? 'class="' + options.class + '"' : '') + '\n >\n <div class="action-sheet"></div>\n </ons-action-sheet>\n ');
  47. // Resolve action and clean up
  48. var finish = function finish(event) {
  49. var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;
  50. if (actionSheet) {
  51. options.destroy && options.destroy(actionSheet);
  52. actionSheet.removeEventListener('dialog-cancel', finish, false);
  53. actionSheet.remove();
  54. actionSheet = null;
  55. options.callback && options.callback(index);
  56. resolve(index);
  57. }
  58. };
  59. // Link cancel handler
  60. actionSheet.addEventListener('dialog-cancel', finish, false);
  61. // Create buttons and link action handler
  62. var buttons = document.createDocumentFragment();
  63. options.buttons.forEach(function (item, index) {
  64. var buttonOptions = typeof item === 'string' ? { label: item } : _extends({}, item);
  65. if (options.destructive === index) {
  66. buttonOptions.modifier = (buttonOptions.modifier || '') + ' destructive';
  67. }
  68. var button = util.createElement('\n <ons-action-sheet-button\n ' + (buttonOptions.icon ? 'icon="' + buttonOptions.icon + '"' : '') + '\n ' + (buttonOptions.modifier ? 'modifier="' + buttonOptions.modifier + '"' : '') + '\n >\n ' + buttonOptions.label + '\n </ons-action-sheet-button>\n ');
  69. button.onclick = function (event) {
  70. return actionSheet.hide().then(function () {
  71. return finish(event, index);
  72. });
  73. };
  74. buttons.appendChild(button);
  75. });
  76. // Finish component and attach
  77. util.findChild(actionSheet, '.action-sheet').appendChild(buttons);
  78. document.body.appendChild(actionSheet);
  79. options.compile && options.compile(el.dialog);
  80. // Show
  81. _setImmediate(function () {
  82. return actionSheet.show({
  83. animation: options.animation,
  84. animationOptions: options.animationOptions
  85. });
  86. });
  87. });
  88. });