UNPKG

7.72 kBJavaScriptView Raw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22module.exports = {
23 id: 'windows',
24 bootstrap:function() {
25 var cordova = require('cordova'),
26 exec = require('cordova/exec'),
27 channel = cordova.require('cordova/channel'),
28 platform = require('cordova/platform'),
29 modulemapper = require('cordova/modulemapper'),
30 configHelper = require('cordova/confighelper'),
31 utils = require('cordova/utils');
32
33 modulemapper.clobbers('cordova/exec/proxy', 'cordova.commandProxy');
34
35 // we will make sure we get this channel
36 // TODO: remove this once other platforms catch up.
37 if(!channel.onActivated) {
38 channel.onActivated = cordova.addDocumentEventHandler('activated');
39 }
40 channel.onNativeReady.fire();
41
42 var onWinJSReady = function () {
43 var app = WinJS.Application,
44 splashscreen = require('cordova/splashscreen');
45
46 modulemapper.clobbers('cordova/splashscreen', 'navigator.splashscreen');
47
48 var checkpointHandler = function checkpointHandler() {
49 cordova.fireDocumentEvent('pause',null,true);
50 };
51
52 var resumingHandler = function resumingHandler() {
53 cordova.fireDocumentEvent('resume',null,true);
54 };
55
56 // activation args are available via the activated event
57 // OR cordova.require('cordova/platform').activationContext
58 // activationContext:{type: actType, args: args};
59 var activationHandler = function (e) {
60 // Making all the details available as activationContext
61 platform.activationContext = utils.clone(e.detail); /* CB-10653 to avoid losing detail properties for some activation kinds */
62 platform.activationContext.raw = e.detail; /* CB-11522 to preserve types */
63 platform.activationContext.args = e.detail.arguments; /* for backwards compatibility */
64
65 function makePromise(fn) {
66 return new WinJS.Promise(function init(completeDispatch, errorDispatch) {
67 fn(function successCb(results) {
68 completeDispatch(results);
69 }, function errorCb(error) {
70 errorDispatch(error);
71 });
72 });
73 }
74
75 if (e.detail.previousExecutionState === Windows.ApplicationModel.Activation.ApplicationExecutionState.running
76 || e.detail.previousExecutionState === Windows.ApplicationModel.Activation.ApplicationExecutionState.suspended) {
77 cordova.fireDocumentEvent('activated', platform.activationContext, true);
78 return;
79 }
80
81 e.setPromise(makePromise(configHelper.readConfig).then(function (config) {
82 splashscreen.firstShow(config, e);
83 }).then(function () {
84 // Avoids splashimage flicker on Windows Phone 8.1/10
85 return WinJS.Promise.timeout();
86 }).then(function () {
87 cordova.fireDocumentEvent('activated', platform.activationContext, true);
88 }));
89 };
90
91 app.addEventListener("checkpoint", checkpointHandler);
92 app.addEventListener("activated", activationHandler, false);
93 Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", resumingHandler, false);
94
95 injectBackButtonHandler();
96
97 app.start();
98 };
99
100 function appendScript(scriptElem, loadedCb) {
101 scriptElem.addEventListener("load", loadedCb);
102 document.head.appendChild(scriptElem);
103 }
104
105 if (!window.WinJS) {
106 var scriptElem = document.createElement("script");
107
108 if (navigator.appVersion.indexOf('MSAppHost/3.0') !== -1) {
109 // Windows 10 UWP
110 scriptElem.src = '/www/WinJS/js/base.js';
111 } else if (navigator.appVersion.indexOf("Windows Phone 8.1;") !== -1) {
112 // windows phone 8.1 + Mobile IE 11
113 scriptElem.src = "//Microsoft.Phone.WinJS.2.1/js/base.js";
114 } else if (navigator.appVersion.indexOf("MSAppHost/2.0;") !== -1) {
115 // windows 8.1 + IE 11
116 scriptElem.src = "//Microsoft.WinJS.2.0/js/base.js";
117 }
118 scriptElem.addEventListener("load", onWinJSReady);
119 document.head.appendChild(scriptElem);
120 }
121 else {
122 onWinJSReady();
123 }
124 }
125};
126
127function injectBackButtonHandler() {
128
129 var app = WinJS.Application;
130
131 // create document event handler for backbutton
132 var backButtonChannel = cordova.addDocumentEventHandler('backbutton');
133
134 // preserve reference to original backclick implementation
135 // `false` as a result will trigger system default behaviour
136 var defaultBackButtonHandler = app.onbackclick || function () { return false; };
137
138 var backRequestedHandler = function backRequestedHandler(evt) {
139 // check if listeners are registered, if yes use custom backbutton event
140 // NOTE: On Windows Phone 8.1 backbutton handlers have to throw an exception in order to exit the app
141 if (backButtonChannel.numHandlers >= 1) {
142 try {
143 cordova.fireDocumentEvent('backbutton', evt, true);
144 evt.handled = true; // Windows Mobile requires handled to be set as well;
145 return true;
146 }
147 catch (e) {
148 return false;
149 }
150 }
151 // if not listeners are active, use default implementation (backwards compatibility)
152 else {
153 return defaultBackButtonHandler.apply(app, arguments);
154 }
155 };
156
157 // Only load this code if we're running on Win10 in a non-emulated app frame, otherwise crash \o/
158 if (navigator.appVersion.indexOf('MSAppHost/3.0') !== -1) { // Windows 10 UWP (PC/Tablet/Phone)
159 var navigationManager = Windows.UI.Core.SystemNavigationManager.getForCurrentView();
160 // Inject a listener for the backbutton on the document.
161 backButtonChannel.onHasSubscribersChange = function () {
162 // If we just attached the first handler or detached the last handler,
163 // let native know we need to override the back button.
164 navigationManager.appViewBackButtonVisibility = (this.numHandlers > 0) ?
165 Windows.UI.Core.AppViewBackButtonVisibility.visible :
166 Windows.UI.Core.AppViewBackButtonVisibility.collapsed;
167 };
168
169 navigationManager.addEventListener("backrequested", backRequestedHandler, false);
170 } else { // Windows 8.1 Phone
171 // inject new back button handler
172 app.onbackclick = backRequestedHandler;
173 }
174}