123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- import _extends from 'babel-runtime/helpers/extends';
- import _typeof from 'babel-runtime/helpers/typeof';
- import _Promise from 'babel-runtime/core-js/promise';
- /*
- Copyright 2013-2015 ASIAL CORPORATION
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
- */
-
- import util from './util';
- import elements from './elements';
- import animit from './animit';
- import GestureDetector from './gesture-detector';
- import platform from './platform';
- import notification from './notification';
- import actionSheet from './action-sheet';
- import internal from './internal';
- import orientation from './orientation';
- import modifier from './modifier';
- import softwareKeyboard from './software-keyboard';
- import pageAttributeExpression from './page-attribute-expression';
- import autoStyle from './autostyle';
- import DoorLock from './doorlock';
- import { defaultPageLoader, PageLoader } from './page-loader';
-
- /**
- * @object ons
- * @category util
- * @description
- * [ja]Onsen UIで利用できるグローバルなオブジェクトです。[/ja]
- * [en]A global object that's used in Onsen UI. [/en]
- */
- var ons = {
- animit: animit,
- defaultPageLoader: defaultPageLoader,
- elements: elements,
- GestureDetector: GestureDetector,
- modifier: modifier,
- notification: notification,
- orientation: orientation,
- pageAttributeExpression: pageAttributeExpression,
- PageLoader: PageLoader,
- platform: platform,
- softwareKeyboard: softwareKeyboard,
- _autoStyle: autoStyle,
- _internal: internal,
- _readyLock: new DoorLock(),
- _util: util
- };
-
- ons.platform.select((window.location.search.match(/platform=([\w-]+)/) || [])[1]);
-
- waitDeviceReady();
-
- var readyError = function readyError(after) {
- return util.throw('This method must be called ' + (after ? 'after' : 'before') + ' ons.isReady() is true');
- };
-
- /**
- * @method isReady
- * @signature isReady()
- * @return {Boolean}
- * [en]Will be true if Onsen UI is initialized.[/en]
- * [ja]初期化されているかどうかを返します。[/ja]
- * @description
- * [en]Returns true if Onsen UI is initialized.[/en]
- * [ja]Onsen UIがすでに初期化されているかどうかを返すメソッドです。[/ja]
- */
- ons.isReady = function () {
- return !ons._readyLock.isLocked();
- };
-
- /**
- * @method isWebView
- * @signature isWebView()
- * @return {Boolean}
- * [en]Will be true if the app is running in Cordova.[/en]
- * [ja]Cordovaで実行されている場合にtrueになります。[/ja]
- * @description
- * [en]Returns true if running inside Cordova.[/en]
- * [ja]Cordovaで実行されているかどうかを返すメソッドです。[/ja]
- */
- ons.isWebView = ons.platform.isWebView;
-
- /**
- * @method ready
- * @signature ready(callback)
- * @description
- * [ja]アプリの初期化に利用するメソッドです。渡された関数は、Onsen UIの初期化が終了している時点で必ず呼ばれます。[/ja]
- * [en]Method used to wait for app initialization. Waits for `DOMContentLoaded` and `deviceready`, when necessary, before executing the callback.[/en]
- * @param {Function} callback
- * [en]Function that executes after Onsen UI has been initialized.[/en]
- * [ja]Onsen UIが初期化が完了した後に呼び出される関数オブジェクトを指定します。[/ja]
- */
- ons.ready = function (callback) {
- if (ons.isReady()) {
- callback();
- } else {
- ons._readyLock.waitUnlock(callback);
- }
- };
-
- /**
- * @method setDefaultDeviceBackButtonListener
- * @signature setDefaultDeviceBackButtonListener(listener)
- * @param {Function} listener
- * [en]Function that executes when device back button is pressed. Must be called on `ons.ready`.[/en]
- * [ja]デバイスのバックボタンが押された時に実行される関数オブジェクトを指定します。[/ja]
- * @description
- * [en]Set default handler for device back button.[/en]
- * [ja]デバイスのバックボタンのためのデフォルトのハンドラを設定します。[/ja]
- */
- ons.setDefaultDeviceBackButtonListener = function (listener) {
- if (!ons.isReady()) {
- readyError(true);
- }
- ons._defaultDeviceBackButtonHandler.setListener(listener);
- };
-
- /**
- * @method disableDeviceBackButtonHandler
- * @signature disableDeviceBackButtonHandler()
- * @description
- * [en]Disable device back button event handler. Must be called on `ons.ready`.[/en]
- * [ja]デバイスのバックボタンのイベントを受け付けないようにします。[/ja]
- */
- ons.disableDeviceBackButtonHandler = function () {
- if (!ons.isReady()) {
- readyError(true);
- }
- internal.dbbDispatcher.disable();
- };
-
- /**
- * @method enableDeviceBackButtonHandler
- * @signature enableDeviceBackButtonHandler()
- * @description
- * [en]Enable device back button event handler. Must be called on `ons.ready`.[/en]
- * [ja]デバイスのバックボタンのイベントを受け付けるようにします。[/ja]
- */
- ons.enableDeviceBackButtonHandler = function () {
- if (!ons.isReady()) {
- readyError(true);
- }
- internal.dbbDispatcher.enable();
- };
-
- ons.fireDeviceBackButtonEvent = function () {
- internal.dbbDispatcher.fireDeviceBackButtonEvent();
- };
-
- /**
- * @method enableAutoStatusBarFill
- * @signature enableAutoStatusBarFill()
- * @description
- * [en]Enable status bar fill feature on iOS7 and above (except for iPhone X). Must be called before `ons.ready`.[/en]
- * [ja]iOS7以上(iPhone Xは除く)で、ステータスバー部分の高さを自動的に埋める処理を有効にします。[/ja]
- */
- ons.enableAutoStatusBarFill = function () {
- if (ons.isReady()) {
- readyError(false);
- }
- internal.config.autoStatusBarFill = true;
- };
-
- /**
- * @method disableAutoStatusBarFill
- * @signature disableAutoStatusBarFill()
- * @description
- * [en]Disable status bar fill feature on iOS7 and above (except for iPhone X). Must be called before `ons.ready`.[/en]
- * [ja]iOS7以上(iPhone Xは除く)で、ステータスバー部分の高さを自動的に埋める処理を無効にします。[/ja]
- */
- ons.disableAutoStatusBarFill = function () {
- if (ons.isReady()) {
- readyError(false);
- }
- internal.config.autoStatusBarFill = false;
- };
-
- /**
- * @method mockStatusBar
- * @signature mockStatusBar()
- * @description
- * [en]Creates a static element similar to iOS status bar. Only useful for browser testing. Must be called before `ons.ready`.[/en]
- * [ja][/ja]
- */
- ons.mockStatusBar = function () {
- if (ons.isReady()) {
- readyError(false);
- }
-
- var mock = function mock() {
- if (!document.body.children[0] || !document.body.children[0].classList.contains('ons-status-bar-mock')) {
- var android = platform.isAndroid(),
- i = function i(_i) {
- return '<i class="' + _i.split('-')[0] + ' ' + _i + '"></i>';
- };
- var left = android ? i('zmdi-twitter') + ' ' + i('zmdi-google-play') : 'No SIM ' + i('fa-wifi'),
- center = android ? '' : '12:28 PM',
- right = android ? i('zmdi-network') + ' ' + i('zmdi-wifi') + ' ' + i('zmdi-battery') + ' 12:28 PM' : '80% ' + i('fa-battery-three-quarters');
-
- document.body.insertBefore(util.createElement('<div class="ons-status-bar-mock ' + (android ? 'android' : 'ios') + '">' + ('<div>' + left + '</div><div>' + center + '</div><div>' + right + '</div>') + '</div>'), document.body.firstChild);
- }
- };
-
- document.body ? mock() : internal.waitDOMContentLoaded(mock);
- };
-
- /**
- * @method disableAnimations
- * @signature disableAnimations()
- * @description
- * [en]Disable all animations. Could be handy for testing and older devices.[/en]
- * [ja]アニメーションを全て無効にします。テストの際に便利です。[/ja]
- */
- ons.disableAnimations = function () {
- internal.config.animationsDisabled = true;
- };
-
- /**
- * @method enableAnimations
- * @signature enableAnimations()
- * @description
- * [en]Enable animations (default).[/en]
- * [ja]アニメーションを有効にします。[/ja]
- */
- ons.enableAnimations = function () {
- internal.config.animationsDisabled = false;
- };
-
- ons._disableWarnings = function () {
- internal.config.warningsDisabled = true;
- };
-
- ons._enableWarnings = function () {
- internal.config.warningsDisabled = false;
- };
-
- /**
- * @method disableAutoStyling
- * @signature disableAutoStyling()
- * @description
- * [en]Disable automatic styling.[/en]
- * [ja][/ja]
- */
- ons.disableAutoStyling = autoStyle.disable;
-
- /**
- * @method enableAutoStyling
- * @signature enableAutoStyling()
- * @description
- * [en]Enable automatic styling based on OS (default).[/en]
- * [ja][/ja]
- */
- ons.enableAutoStyling = autoStyle.enable;
-
- /**
- * @method disableIconAutoPrefix
- * @signature disableIconAutoPrefix()
- * @description
- * [en]Disable adding `fa-` prefix automatically to `ons-icon` classes. Useful when including custom icon packs.[/en]
- * [ja][/ja]
- */
- ons.disableIconAutoPrefix = function () {
- util.checkMissingImport('Icon');
- elements.Icon.setAutoPrefix(false);
- };
-
- /**
- * @method forceUIWebViewScrollFix
- * @signature forceUIWebViewScrollFix()
- * @param {Boolean} force Enable or disable the fix.
- * @description
- * [en]Applies a fix for iOS UIWebView which prevents scroll events jumping to pages under the top layer. This may visually affect normal scrolling of UIWebView if you open a dialog/menu before the scroll momentum finished. Disabled by default.[/en]
- * [ja][/ja]
- */
- ons.forceUIWebViewScrollFix = function () {
- var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
-
- internal.config.forceUIWebViewScrollFix = force;
- };
-
- /**
- * @method forcePlatformStyling
- * @signature forcePlatformStyling(platform)
- * @description
- * [en]Refresh styling for the given platform. Only useful for demos. Use `ons.platform.select(...)` instead for development and production.[/en]
- * [ja][/ja]
- * @param {string} platform New platform to style the elements.
- */
- ons.forcePlatformStyling = function (newPlatform) {
- ons.enableAutoStyling();
- ons.platform.select(newPlatform || 'ios');
-
- ons._util.arrayFrom(document.querySelectorAll('*')).forEach(function (element) {
- if (element.tagName.toLowerCase() === 'ons-if') {
- element._platformUpdate();
- } else if (element.tagName.match(/^ons-/i)) {
- autoStyle.prepare(element, true);
- if (element.tagName.toLowerCase() === 'ons-tabbar') {
- element._updatePosition();
- }
- }
- });
- };
-
- /**
- * @method preload
- * @signature preload(templatePaths)
- * @param {String|Array} templatePaths
- * [en]Set of HTML file paths containing 'ons-page' elements.[/en]
- * [ja][/ja]
- * @return {Promise}
- * [en]Promise that resolves when all the templates are cached.[/en]
- * [ja][/ja]
- * @description
- * [en]Separated files need to be requested on demand and this can slightly delay pushing new pages. This method requests and caches templates for later use.[/en]
- * [ja][/ja]
- */
- ons.preload = function () {
- var templates = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
-
- return _Promise.all((templates instanceof Array ? templates : [templates]).map(function (template) {
- if (typeof template !== 'string') {
- util.throw('Expected string arguments but got ' + (typeof template === 'undefined' ? 'undefined' : _typeof(template)));
- }
- return internal.getTemplateHTMLAsync(template);
- }));
- };
-
- /**
- * @method createElement
- * @signature createElement(template, options)
- * @param {String} template
- * [en]Either an HTML file path, a `<template>` id or an HTML string such as `'<div id="foo">hoge</div>'`.[/en]
- * [ja][/ja]
- * @param {Object} [options]
- * [en]Parameter object.[/en]
- * [ja]オプションを指定するオブジェクト。[/ja]
- * @param {Boolean|HTMLElement} [options.append]
- * [en]Whether or not the element should be automatically appended to the DOM. Defaults to `false`. If `true` value is given, `document.body` will be used as the target.[/en]
- * [ja][/ja]
- * @param {HTMLElement} [options.insertBefore]
- * [en]Reference node that becomes the next sibling of the new node (`options.append` element).[/en]
- * [ja][/ja]
- * @return {HTMLElement|Promise}
- * [en]If the provided template was an inline HTML string, it returns the new element. Otherwise, it returns a promise that resolves to the new element.[/en]
- * [ja][/ja]
- * @description
- * [en]Create a new element from a template. Both inline HTML and external files are supported although the return value differs.[/en]
- * [ja][/ja]
- */
- ons.createElement = function (template) {
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
-
- template = template.trim();
-
- var create = function create(html) {
- var element = ons._util.createElement(html);
- element.remove();
-
- if (options.append) {
- var target = options.append instanceof HTMLElement ? options.append : document.body;
- target.insertBefore(element, options.insertBefore || null);
- options.link instanceof Function && options.link(element);
- }
-
- return element;
- };
-
- return template.charAt(0) === '<' ? create(template) : internal.getPageHTMLAsync(template).then(create);
- };
-
- /**
- * @method createPopover
- * @signature createPopover(page, [options])
- * @param {String} page
- * [en]Page name. Can be either an HTML file or a <template> containing a <ons-dialog> component.[/en]
- * [ja]pageのURLか、もしくは`<template>`で宣言したテンプレートのid属性の値を指定できます。[/ja]
- * @param {Object} [options]
- * [en]Parameter object.[/en]
- * [ja]オプションを指定するオブジェクト。[/ja]
- * @param {Object} [options.parentScope]
- * [en]Parent scope of the dialog. Used to bind models and access scope methods from the dialog.[/en]
- * [ja]ダイアログ内で利用する親スコープを指定します。ダイアログからモデルやスコープのメソッドにアクセスするのに使います。このパラメータはAngularJSバインディングでのみ利用できます。[/ja]
- * @return {Promise}
- * [en]Promise object that resolves to the popover component object.[/en]
- * [ja]ポップオーバーのコンポーネントオブジェクトを解決するPromiseオブジェクトを返します。[/ja]
- * @description
- * [en]Create a popover instance from a template.[/en]
- * [ja]テンプレートからポップオーバーのインスタンスを生成します。[/ja]
- */
- /**
- * @method createDialog
- * @signature createDialog(page, [options])
- * @param {String} page
- * [en]Page name. Can be either an HTML file or an `<template>` containing a <ons-dialog> component.[/en]
- * [ja]pageのURLか、もしくは`<template>`で宣言したテンプレートのid属性の値を指定できます。[/ja]
- * @param {Object} [options]
- * [en]Parameter object.[/en]
- * [ja]オプションを指定するオブジェクト。[/ja]
- * @return {Promise}
- * [en]Promise object that resolves to the dialog component object.[/en]
- * [ja]ダイアログのコンポーネントオブジェクトを解決するPromiseオブジェクトを返します。[/ja]
- * @description
- * [en]Create a dialog instance from a template.[/en]
- * [ja]テンプレートからダイアログのインスタンスを生成します。[/ja]
- */
- /**
- * @method createAlertDialog
- * @signature createAlertDialog(page, [options])
- * @param {String} page
- * [en]Page name. Can be either an HTML file or an `<template>` containing a <ons-alert-dialog> component.[/en]
- * [ja]pageのURLか、もしくは`<template>`で宣言したテンプレートのid属性の値を指定できます。[/ja]
- * @param {Object} [options]
- * [en]Parameter object.[/en]
- * [ja]オプションを指定するオブジェクト。[/ja]
- * @return {Promise}
- * [en]Promise object that resolves to the alert dialog component object.[/en]
- * [ja]ダイアログのコンポーネントオブジェクトを解決するPromiseオブジェクトを返します。[/ja]
- * @description
- * [en]Create a alert dialog instance from a template.[/en]
- * [ja]テンプレートからアラートダイアログのインスタンスを生成します。[/ja]
- */
- ons.createPopover = ons.createDialog = ons.createAlertDialog = function (template) {
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- return ons.createElement(template, _extends({ append: true }, options));
- };
-
- /**
- * @method openActionSheet
- * @signature openActionSheet(options)
- * @description
- * [en]Shows an instant Action Sheet and lets the user choose an action.[/en]
- * [ja][/ja]
- * @param {Object} [options]
- * [en]Parameter object.[/en]
- * [ja]オプションを指定するオブジェクト。[/ja]
- * @param {Array} [options.buttons]
- * [en]Represent each button of the action sheet following the specified order. Every item can be either a string label or an object containing `label`, `icon` and `modifier` properties.[/en]
- * [ja][/ja]
- * @param {String} [options.title]
- * [en]Optional title for the action sheet.[/en]
- * [ja][/ja]
- * @param {Number} [options.destructive]
- * [en]Optional index of the "destructive" button (only for iOS). It can be specified in the button array as well.[/en]
- * [ja][/ja]
- * @param {Boolean} [options.cancelable]
- * [en]Whether the action sheet can be canceled by tapping on the background mask or not.[/en]
- * [ja][/ja]
- * @param {String} [options.modifier]
- * [en]Modifier attribute of the action sheet. E.g. `'destructive'`.[/en]
- * [ja][/ja]
- * @param {String} [options.maskColor]
- * [en]Optionally change the background mask color.[/en]
- * [ja][/ja]
- * @param {String} [options.id]
- * [en]The element's id attribute.[/en]
- * [ja][/ja]
- * @param {String} [options.class]
- * [en]The element's class attribute.[/en]
- * [ja][/ja]
- * @return {Promise}
- * [en]Will resolve when the action sheet is closed. The resolve value is either the index of the tapped button or -1 when canceled.[/en]
- * [ja][/ja]
- */
- ons.openActionSheet = actionSheet;
-
- /**
- * @method resolveLoadingPlaceholder
- * @signature resolveLoadingPlaceholder(page)
- * @param {String} page
- * [en]Page name. Can be either an HTML file or a `<template>` id.[/en]
- * [ja]pageのURLか、もしくは`<template>`で宣言したテンプレートのid属性の値を指定できます。[/ja]
- * @description
- * [en]If no page is defined for the `ons-loading-placeholder` attribute it will wait for this method being called before loading the page.[/en]
- * [ja]ons-loading-placeholderの属性値としてページが指定されていない場合は、ページロード前に呼ばれるons.resolveLoadingPlaceholder処理が行われるまで表示されません。[/ja]
- */
- ons.resolveLoadingPlaceholder = function (page, link) {
- var elements = ons._util.arrayFrom(window.document.querySelectorAll('[ons-loading-placeholder]'));
- if (elements.length === 0) {
- util.throw('No ons-loading-placeholder exists');
- }
-
- elements.filter(function (element) {
- return !element.getAttribute('page');
- }).forEach(function (element) {
- element.setAttribute('ons-loading-placeholder', page);
- ons._resolveLoadingPlaceholder(element, page, link);
- });
- };
-
- ons._setupLoadingPlaceHolders = function () {
- ons.ready(function () {
- var elements = ons._util.arrayFrom(window.document.querySelectorAll('[ons-loading-placeholder]'));
-
- elements.forEach(function (element) {
- var page = element.getAttribute('ons-loading-placeholder');
- if (typeof page === 'string') {
- ons._resolveLoadingPlaceholder(element, page);
- }
- });
- });
- };
-
- ons._resolveLoadingPlaceholder = function (parent, page) {
- var link = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function (el, done) {
- return done();
- };
-
- page && ons.createElement(page).then(function (element) {
- element.style.display = 'none';
- parent.appendChild(element);
- link(element, function () {
- while (parent.firstChild && parent.firstChild !== element) {
- parent.removeChild(parent.firstChild);
- }
- element.style.display = '';
- });
- }).catch(function (error) {
- return _Promise.reject('Unabled to resolve placeholder: ' + error);
- });
- };
-
- function waitDeviceReady() {
- var unlockDeviceReady = ons._readyLock.lock();
- window.addEventListener('DOMContentLoaded', function () {
- if (ons.isWebView()) {
- window.document.addEventListener('deviceready', unlockDeviceReady, false);
- } else {
- unlockDeviceReady();
- }
- }, false);
- }
-
- /**
- * @method getScriptPage
- * @signature getScriptPage()
- * @description
- * [en]Access the last created page from the current `script` scope. Only works inside `<script></script>` tags that are direct children of `ons-page` element. Use this to add lifecycle hooks to a page.[/en]
- * [ja][/ja]
- * @return {HTMLElement}
- * [en]Returns the corresponding page element.[/en]
- * [ja][/ja]
- */
- var getCS = 'currentScript' in document ? function () {
- return document.currentScript;
- } : function () {
- return document.scripts[document.scripts.length - 1];
- };
- ons.getScriptPage = function () {
- return getCS() && /ons-page/i.test(getCS().parentElement.tagName) && getCS().parentElement || null;
- };
-
- export default ons;
|