123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- import _setImmediate from 'babel-runtime/core-js/set-immediate';
- import _Object$keys from 'babel-runtime/core-js/object/keys';
- import _Promise from 'babel-runtime/core-js/promise';
- import _extends from 'babel-runtime/helpers/extends';
-
-
- import util from './util';
- import contentReady from './content-ready';
- import ToastQueue from './internal/toast-queue';
-
- var _setAttributes = function _setAttributes(element, options) {
- ['id', 'class', 'animation'].forEach(function (a) {
- return options.hasOwnProperty(a) && element.setAttribute(a, options[a]);
- });
-
- if (options.modifier) {
- util.addModifier(element, options.modifier);
- }
- };
-
- var _normalizeArguments = function _normalizeArguments(message) {
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- var defaults = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
-
- options = _extends({}, options);
- typeof message === 'string' ? options.message = message : options = message;
- if (!options || !options.message && !options.messageHTML) {
- util.throw('Notifications must contain a message');
- }
-
- if (options.hasOwnProperty('buttonLabels') || options.hasOwnProperty('buttonLabel')) {
- options.buttonLabels = options.buttonLabels || options.buttonLabel;
- if (!Array.isArray(options.buttonLabels)) {
- options.buttonLabels = [options.buttonLabels || ''];
- }
- }
-
- return util.extend({
- compile: function compile(param) {
- return param;
- },
- callback: function callback(param) {
- return param;
- },
- animation: 'default',
- cancelable: false,
- primaryButtonIndex: (options.buttonLabels || defaults.buttonLabels || []).length - 1
- }, defaults, options);
- };
-
-
- var notification = {};
-
- notification._createAlertDialog = function () {
- for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
- params[_key] = arguments[_key];
- }
-
- return new _Promise(function (resolve) {
- var options = _normalizeArguments.apply(undefined, params);
- util.checkMissingImport('AlertDialog', 'AlertDialogButton');
-
-
- var inputString = '';
- if (options.isPrompt) {
- 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 ';
- }
-
-
- var buttons = '';
- options.buttonLabels.forEach(function (label, index) {
- 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 ';
- });
-
-
- var el = {};
- var _destroyDialog = function _destroyDialog() {
- if (el.dialog.onDialogCancel) {
- el.dialog.removeEventListener('dialog-cancel', el.dialog.onDialogCancel);
- }
-
- _Object$keys(el).forEach(function (key) {
- return delete el[key];
- });
- el = null;
-
- if (options.destroy instanceof Function) {
- options.destroy();
- }
- };
-
- el.dialog = document.createElement('ons-alert-dialog');
- 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 ';
- contentReady(el.dialog);
-
-
- _setAttributes(el.dialog, options);
-
-
- if (options.isPrompt && options.submitOnEnter) {
- el.input = el.dialog.querySelector('.text-input');
- el.input.onkeypress = function (event) {
- if (event.keyCode === 13) {
- el.dialog.hide().then(function () {
- if (el) {
- var resolveValue = el.input.value;
- _destroyDialog();
- options.callback(resolveValue);
- resolve(resolveValue);
- }
- });
- }
- };
- }
-
-
- el.footer = el.dialog.querySelector('.alert-dialog-footer');
- util.arrayFrom(el.dialog.querySelectorAll('.alert-dialog-button')).forEach(function (buttonElement, index) {
- buttonElement.onclick = function () {
- el.dialog.hide().then(function () {
- if (el) {
- var resolveValue = index;
- if (options.isPrompt) {
- resolveValue = index === options.primaryButtonIndex ? el.input.value : null;
- }
- el.dialog.remove();
- _destroyDialog();
- options.callback(resolveValue);
- resolve(resolveValue);
- }
- });
- };
-
- el.footer.appendChild(buttonElement);
- });
-
-
- if (options.cancelable) {
- el.dialog.cancelable = true;
- el.dialog.onDialogCancel = function () {
- _setImmediate(function () {
- el.dialog.remove();
- _destroyDialog();
- });
- var resolveValue = options.isPrompt ? null : -1;
- options.callback(resolveValue);
- resolve(resolveValue);
- };
- el.dialog.addEventListener('dialog-cancel', el.dialog.onDialogCancel, false);
- }
-
-
- document.body.appendChild(el.dialog);
- options.compile(el.dialog);
- _setImmediate(function () {
- el.dialog.show().then(function () {
- if (el.input && options.isPrompt && options.autofocus) {
- var strLength = el.input.value.length;
- el.input.focus();
- el.input.setSelectionRange(strLength, strLength);
- }
- });
- });
- });
- };
-
-
- notification.alert = function (message, options) {
- return notification._createAlertDialog(message, options, {
- buttonLabels: ['OK'],
- title: 'Alert'
- });
- };
-
-
- notification.confirm = function (message, options) {
- return notification._createAlertDialog(message, options, {
- buttonLabels: ['Cancel', 'OK'],
- title: 'Confirm'
- });
- };
-
-
- notification.prompt = function (message, options) {
- return notification._createAlertDialog(message, options, {
- buttonLabels: ['OK'],
- title: 'Alert',
- isPrompt: true,
- autofocus: true,
- submitOnEnter: true
- });
- };
-
-
- notification.toast = function (message, options) {
- var promise = new _Promise(function (resolve) {
- util.checkMissingImport('Toast');
-
- options = _normalizeArguments(message, options, {
- timeout: 0,
- force: false
- });
-
- var toast = util.createElement('\n <ons-toast>\n ' + options.message + '\n ' + (options.buttonLabels ? '<button>' + options.buttonLabels[0] + '</button>' : '') + '\n </ons-toast>\n ');
-
- _setAttributes(toast, options);
-
- var finish = function finish(value) {
- if (toast) {
- toast.hide().then(function () {
- if (toast) {
- toast.remove();
- toast = null;
- options.callback(value);
- resolve(value);
- }
- });
- }
- };
-
- if (options.buttonLabels) {
- util.findChild(toast._toast, 'button').onclick = function () {
- return finish(0);
- };
- }
-
- document.body.appendChild(toast);
- options.compile(toast);
-
- var show = function show() {
- toast.parentElement && toast.show(options).then(function () {
- if (options.timeout) {
- setTimeout(function () {
- return finish(-1);
- }, options.timeout);
- }
- });
- };
-
- _setImmediate(function () {
- return options.force ? show() : ToastQueue.add(show, promise);
- });
- });
-
- return promise;
- };
-
- export default notification;
|