-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathliveedit.ios.js
418 lines (363 loc) · 11.3 KB
/
liveedit.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**********************************************************************************
* (c) 2015, 2016 Master Technology
* Licensed under the MIT license or contact me for a Support or Commercial License
*
* I do contract work in most languages, so let me solve your problems!
*
* Any questions please feel free to email me or put a issue up on the github repo
* Version 0.0.2 - IOS Dummy Wrapper Nathan@master-technology.com
*********************************************************************************/
"use strict";
// ON THE IOS platform this is a DUMMY wrapper currently. I supply it so deployment on iOS won't crash.
/* jshint node: true, browser: true, unused: true, undef: true */
/* global __clearRequireCachedItem */
// --------------------------------------------
var fs = require("file-system");
var fsa = require("file-system/file-system-access").FileSystemAccess;
var application = require('application');
var frameCommon = require('ui/frame/frame-common');
var styleScope = require("ui/styling/style-scope");
// --------------------------------------------
var FSA = new fsa();
// --------------------------------------------
var UpdaterSingleton = null;
var Updater = function() {
if (UpdaterSingleton) {
return UpdaterSingleton;
}
UpdaterSingleton = this;
this._modelLink = {};
this._ignoredPages = [];
this._restartPages = [];
this._suspendedNavigation = null;
this._applicationResumedStatus = 0;
this._updaterEnabled = false;
//this._hookFramework();
};
/**
* Returns debugMode status, or allows you to set it.
* @param value - true/false or no value.
* @returns {*} - debug mode status.
*/
Updater.prototype.debugMode = function(value) {
if (arguments.length) {
this._isDebugMode = !!value;
} else if (this._isDebugMode === null) {
return this.checkForDebugMode();
}
return this._isDebugMode;
};
/**
* Allow you to enable/disable the instant update support
* @param value - true or false
* @returns {boolean} the current enabled status
*/
Updater.prototype.enabled = function(value) {
if (arguments.length) {
this._updaterEnabled = !!value;
}
return this._updaterEnabled;
};
/**
* Retrieves the App name from the AndroidManifest
* @returns {string} - Name of App
*/
Updater.prototype.getAppName = function() {
return null;
};
/**
* Retrieves the app Version from the AndroidManifest
* @returns {string} - App Version
*/
Updater.prototype.getAppVersion = function() {
return null;
};
/**
* Restart the application
*/
Updater.prototype.restart = function() {
// Does Nothing
};
/**
* Returns true if the application is running on a emulator
* @returns {boolean}
*/
Updater.prototype.checkForEmulator = function() {
return false;
};
/**
* Returns a list of application signatures
* @returns {Array} - signature array
*/
Updater.prototype.getAppSignatures = function() {
return [];
};
/**
* Returns true if the application was signed with a DebugKey meaning the app is in debug mode
* @returns {boolean} - false if it is in release mode
*/
Updater.prototype.checkForDebugMode = function() {
return false;
};
/**
* Returns true if this application is signed by a release key
* @returns {boolean} - false if the app is in debug mode
*/
Updater.prototype.checkForReleaseMode = function() {
return !this.checkForDebugMode();
};
/**
* Reload the current page
* @param p - optional page name to reload
*/
Updater.prototype.reloadPage = function(p) {
reloadPage(p);
};
/**
* Is the application suspended
* @returns {boolean} - true/false
*/
Updater.prototype.isSuspended = function() {
return this._applicationResumedStatus === 0;
};
/**
* This allows you to link model(s) to a specific page
* @param page - the page that uses this model
* @param model - the model that is linked to the page
*/
Updater.prototype.addModelPageLink = function(page, model) {
if (!model.endsWith('.js')) {
model = model + ".js";
}
if (page.endsWith('.js')) {
page = page.substring(0, page.length-3);
}
if (typeof this._modelLink[model] === "undefined") {
this._modelLink[model] = page;
return;
} else if (!Array.isArray(this._modelLink[model])) {
if (this._modelLink[model] === page) {
return;
}
// Convert to an array if it is already assigned a page
this._modelLink[model] = [this._modelLink[model]];
}
var found = false;
for (var i=0;i<this._modelLink[model].length;i++) {
if (this._modelLink[model][i] === page) {
found = true; break;
}
}
if (!found) {
this._modelLink[model].push(page);
}
};
/**
* Ignores files from being updated
* @param page
*/
Updater.prototype.ignoreFile = function(page) {
this._ignoredPages.push(page);
};
/**
* These files cause the whole application to restart
* @param page
*/
Updater.prototype.restartFile = function(page) {
this._restartPages.push(page);
};
/**
* Returns the current Application Running path
* @returns {string}
*/
Updater.prototype.currentAppPath = function() {
return this._curAppPath;
};
/**
* This allows you to link a type of file to a page; to force it to reload if the support file changes...
* @param page
* @param fileName
*/
Updater.prototype.addSupportReloads = function(page, fileName) {
if (fileName == null || fileName.length === 0) { return; }
if (fileName[0] === '*' && fileName[1] === '.') {
fileName = fileName.substr(1);
}
if (page.endsWith('.js')) {
page = page.substring(0, page.length-3);
}
// We only allow a file or and extension to be assigned to ONE page
this._supportFiles[fileName] = page;
};
/**********************************/
/****** Internal Functions ******/
/**********************************/
/**
* Used to hook into the framework functions
* @private
*/
Updater.prototype._hookFramework = function() {
// Have to hook into the framework so that we can return our values if need be.
if (!global.__clearRequireCachedItem) {
this._updaterEnabled = false;
}
//noinspection JSValidateTypes
//application.loadCss = loadCss;
// We need to hook the Resume/Suspend Application events because attempting to navigate while suspended will crash
var oldResume = application.onResume;
application.onResume = function () {
if (oldResume) {
oldResume();
}
UpdaterSingleton._applicationResumed();
};
var oldSuspend = application.onSuspend;
application.onSuspend = function () {
if (oldSuspend) {
oldSuspend();
}
UpdaterSingleton._applicationSuspended();
};
};
/**
* Used to track any pages for navigation while the app is suspended
* @param value - page name
* @returns {string} - page name
* @private
*/
Updater.prototype._suspendedNavigate = function(value) {
if (arguments.length) {
this._suspendedNavigation = value;
}
return this._suspendedNavigation;
};
/**
* Used to track if the app is suspended
* @private
*/
Updater.prototype._applicationSuspended = function() {
this._applicationResumedStatus--;
};
/**
* Used to track when the app is resumed
* @private
*/
Updater.prototype._applicationResumed = function() {
this._applicationResumedStatus++;
if (this._suspendedNavigation) {
reloadPage(this._suspendedNavigation);
this._suspendedNavigation = null;
}
};
/**
* Checks if the modified file is related to the currently loaded page
* @param v
* @private
*/
Updater.prototype._checkCurrentPage = function(v) {
var f = frameCommon.topmost();
var CE, CEjs, CExml, CEcss;
if (f.currentEntry && f.currentEntry.entry) {
CE = f.currentEntry.entry.moduleName;
}
if (!CE) {
return;
}
if (CE.toLowerCase().endsWith('.js')) {
CE = CE.substr(0, CE.lastIndexOf('.'));
}
CEjs = CE + '.js';
CExml = CE + '.xml';
CEcss = CE + '.css';
//console.log("******************* Checking ", v, "against:", CEjs, CExml, CEcss);
if (v === CEjs || v === CExml) {
reloadPage(CE);
} else if (v === application.cssFile) {
loadCss();
} else if (v === CEcss) {
loadPageCss(CEcss);
} else {
if (v.endsWith('.js')) {
__clearRequireCachedItem(this._curAppPath + v);
for (var key in this._modelLink) {
if (this._modelLink.hasOwnProperty(key)) {
if (key === v) {
if (Array.isArray(this._modelLink[key])) {
for (var j = 0; j < this._modelLink[key].length; j++) {
if (this._modelLink[key][j] === CE) {
reloadPage(CE);
return;
}
}
} else if (this._modelLink[key] === CE) {
reloadPage(CE);
}
return;
}
}
}
}
}
};
// ---------------------------------------------------------------
// Create our UpdaterSingleton and assign it to the export
// ---------------------------------------------------------------
UpdaterSingleton = new Updater();
module.exports = UpdaterSingleton;
/**
* This is the loadCss helper function to replace the one on Application
*/
function loadCss() {
var cssFileName = fs.path.join(fs.knownFolders.currentApp().path, application.cssFile);
var applicationCss;
if (FSA.fileExists(cssFileName)) {
FSA.readText(cssFileName, function (r) { applicationCss = r; });
//noinspection JSUnusedAssignment
application.cssSelectorsCache = styleScope.StyleScope.createSelectorsFromCss(applicationCss, cssFileName);
// Add New CSS to Current Page
var f = frameCommon.topmost();
if (f && f.currentPage) {
f.currentPage._resetCssValues();
f.currentPage._styleScope = new styleScope.StyleScope();
//noinspection JSUnusedAssignment
f.currentPage._addCssInternal(applicationCss, cssFileName);
f.currentPage._refreshCss();
}
}
}
/**
* Override a single page's css
* @param cssFile
*/
function loadPageCss(cssFile) {
var cssFileName;
// Eliminate the ./ on the file if present so that we can add the full path
if (cssFile.startsWith("./")) {
cssFile = cssFile.substring(2);
}
if (cssFile.startsWith(UpdaterSingleton.currentAppPath())) {
cssFileName = cssFile;
} else {
cssFileName = fs.path.join(UpdaterSingleton.currentAppPath(), cssFile);
}
var applicationCss;
if (FSA.fileExists(cssFileName)) {
FSA.readText(cssFileName, function (r) { applicationCss = r; });
// Add New CSS to Current Page
var f = frameCommon.topmost();
if (f && f.currentPage) {
f.currentPage._resetCssValues();
f.currentPage._styleScope = new styleScope.StyleScope();
//noinspection JSUnusedAssignment
f.currentPage._addCssInternal(applicationCss, cssFileName);
f.currentPage._refreshCss();
}
}
}
/**
* This is a helper function to reload the current page
* @param page
*/
function reloadPage(page) {
return page;
}