-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathKTL_Start.js
More file actions
143 lines (123 loc) · 6.31 KB
/
KTL_Start.js
File metadata and controls
143 lines (123 loc) · 6.31 KB
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
//KTL and App Starter. Also enables switching between Prod and Dev modes.
//window.ktlStart = window.performance.now();
/* ktlVersion:
* - 'x,y,z' will use that specific Prod version.
* - if empty, will use the latest Prod version from KTL_LATEST_JS_VERSION.
* - if 'dev', will use /Prod/KTL-dev.js version, which is the latest "experimental" code
* - if 'beta', will use /Prod/KTL-beta.js version, which is the candidate for next release
*/
let callback;
function loadKtl($, _callback, _KnackApp, ktlVersion = '', fullCode = 'min', noCacheBust = false) {
const KTL_LATEST_JS_VERSION = '0.42.10';
const KTL_LATEST_CSS_VERSION = '0.14.1';
let cssVersion = KTL_LATEST_CSS_VERSION;
let prodFolder = 'Prod/';
let ktlSvr = 'https://ctrnd.s3.amazonaws.com/'; //CDN is Cortex R&D Inc server.
window.$ = $;
window.jQuery = $; //For BlockUI
window.KnackApp = _KnackApp;
callback = _callback;
const lsShortName = Knack.app.attributes.name.substr(0, 6).replace(/ /g, '') + '_' + app_id.substr(-4, 4) + '_';
//Used to bypass KTL completely, typically to troubleshoot and isolate an issue. Used with the KTL Developer Tools popup.
const bypassKtl = (sessionStorage.getItem(lsShortName + 'bypassKtl') !== null);
if (bypassKtl) {
callback();
return;
}
let ktlCode = localStorage.getItem(lsShortName + 'ktlCode');
if (ktlCode === null || ktlCode === 'prod') {
ktlVersion = (ktlVersion ? ktlVersion : KTL_LATEST_JS_VERSION);
} else if (['dev', 'beta'].includes(ktlCode) || /^\d.*\./.test(ktlCode)) {
ktlVersion = ktlCode; //Use 'dev', 'beta', or specific version.
}
if (ktlCode === 'local') {
ktlVersion = '';
cssVersion = '';
prodFolder = '';
fullCode = 'forcefull';
ktlSvr = 'http://localhost:3000/';
function checkFileExists(url) {
return new Promise((resolve, reject) => {
fetch(url, { method: 'HEAD' })
.then(response => resolve(response.ok))
.catch(() => resolve(false));
});
}
let fileName = localStorage.getItem(lsShortName + 'fileName');
if (fileName !== 'NO_APP_FILE') {
!fileName && (fileName = Knack.app.attributes.name);
let appJsFile = ktlSvr + 'KnackApps/' + fileName + '/' + fileName + '.js';
appJsFile = encodeURI(appJsFile);
//Replace CSS from Builder by local file, if it exists.
let appCSSFile = ktlSvr + 'KnackApps/' + fileName + '/' + fileName + '.css';
appCSSFile = encodeURI(appCSSFile);
checkFileExists(appCSSFile).then(exists => {
if (exists) {
// Replace CSS from Builder by local file
const cssText = document.querySelector('#kn-custom-css');
if (cssText) {
cssText.textContent = '';
} else {
const cssFile = document.querySelector('link[href*="main.css"]');
if (cssFile)
cssFile.disabled = true;
}
LazyLoad.css([`${appCSSFile}`]);
}
});
//Replace JAVASCRIPT from Builder by local file.
delete window.KnackApp;
if (typeof window.ktlReady === 'function')
delete window.ktlReady;
LazyLoad.js([`${appJsFile}`], () => {
if (typeof window.ktlReady !== 'function') {
let srcFileName = prompt(`Can't find source file with ktlReady:\n\n${appJsFile}\n\nWhat is file name (without .js)?\n\nLeave empty for none.`, Knack.app.attributes.name);
if (srcFileName === null) {
localStorage.removeItem(lsShortName + 'dev');
alert('Reverting to Prod mode.');
location.reload(true);
} else if (srcFileName !== '') {
localStorage.setItem(lsShortName + 'fileName', srcFileName);
location.reload(true);
} else
localStorage.setItem(lsShortName + 'fileName', 'NO_APP_FILE');
}
})
}
}
LazyLoad.js(['https://cdnjs.cloudflare.com/ajax/libs/jquery.blockUI/2.70/jquery.blockUI.min.js']);
LazyLoad.js(['https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js']); //Docs: https://github.com/SortableJS/Sortable#readme
if (ktlVersion === 'dev' || ktlVersion === 'beta') {
fullCode = 'forcefull';
cssVersion = ktlVersion;
}
function loadFilesAndRunApp() {
//Append this to end of filename to force loading new code without requiring Ctrl+F5.
let bypassCacheSuffix = (!noCacheBust && ['dev', 'beta'].includes(ktlVersion)) ? `?v=${new Date().getTime()}` : '';
let cssFile = ktlSvr + 'Lib/KTL/' + prodFolder + (cssVersion ? 'KTL-' + cssVersion : 'KTL') + '.css' + bypassCacheSuffix;
let ktlFile = ktlSvr + 'Lib/KTL/' + prodFolder + (ktlVersion ? 'KTL-' + ktlVersion : 'KTL') + (fullCode === 'forcefull' ? '' : '.min') + '.js' + bypassCacheSuffix;
LazyLoad.css([`${cssFile}`], () => {
LazyLoad.js([`${ktlFile}`], () => {
if (typeof Ktl === 'function') {
LazyLoad.js([ktlSvr + 'Lib/KTL/KTL_Defaults' + ((ktlVersion === 'dev' || ktlVersion === 'beta') ? '-' + ktlVersion : '') + '.js'], () => {
if (typeof window?.KnackApp === 'function') {
window.KnackApp($, { ktlVersion: ktlVersion, lsShortName: lsShortName });
} else
alert('Error - KnackApp not found.');
callback();
})
} else {
if (ktlCode === 'local') {
alert('KTL not found');
} else {
//Use case when numbered version doesn't exist anymore due to AWS archives monthly cleanup.
//Reload KTL one more time, but with latest prod version.
ktlVersion = KTL_LATEST_JS_VERSION;
loadFilesAndRunApp();
}
}
})
});
}
loadFilesAndRunApp();
}