1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | module.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 |
|
36 |
|
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 |
|
57 |
|
58 |
|
59 | var activationHandler = function (e) {
|
60 |
|
61 | platform.activationContext = utils.clone(e.detail);
|
62 | platform.activationContext.raw = e.detail;
|
63 | platform.activationContext.args = e.detail.arguments;
|
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 |
|
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 |
|
110 | scriptElem.src = '/www/WinJS/js/base.js';
|
111 | } else if (navigator.appVersion.indexOf("Windows Phone 8.1;") !== -1) {
|
112 |
|
113 | scriptElem.src = "//Microsoft.Phone.WinJS.2.1/js/base.js";
|
114 | } else if (navigator.appVersion.indexOf("MSAppHost/2.0;") !== -1) {
|
115 |
|
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 |
|
127 | function injectBackButtonHandler() {
|
128 |
|
129 | var app = WinJS.Application;
|
130 |
|
131 |
|
132 | var backButtonChannel = cordova.addDocumentEventHandler('backbutton');
|
133 |
|
134 |
|
135 |
|
136 | var defaultBackButtonHandler = app.onbackclick || function () { return false; };
|
137 |
|
138 | var backRequestedHandler = function backRequestedHandler(evt) {
|
139 |
|
140 |
|
141 | if (backButtonChannel.numHandlers >= 1) {
|
142 | try {
|
143 | cordova.fireDocumentEvent('backbutton', evt, true);
|
144 | evt.handled = true;
|
145 | return true;
|
146 | }
|
147 | catch (e) {
|
148 | return false;
|
149 | }
|
150 | }
|
151 |
|
152 | else {
|
153 | return defaultBackButtonHandler.apply(app, arguments);
|
154 | }
|
155 | };
|
156 |
|
157 |
|
158 | if (navigator.appVersion.indexOf('MSAppHost/3.0') !== -1) {
|
159 | var navigationManager = Windows.UI.Core.SystemNavigationManager.getForCurrentView();
|
160 |
|
161 | backButtonChannel.onHasSubscribersChange = function () {
|
162 |
|
163 |
|
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 {
|
171 |
|
172 | app.onbackclick = backRequestedHandler;
|
173 | }
|
174 | }
|