angular-loading-bar
1 year ago
angular-moment-picker
1 year ago
angularjs
1 year ago
angularjs-i18next
1 year ago
angularjs-material
1 year ago
bootstrap
1 year ago
fontawesome
1 year ago
requirejs
1 year ago
ui-bootstrap
1 year ago
main.js
1 year ago
moment-with-locales.js
1 year ago
moment.min.js
1 year ago
main.js
269 lines
| 1 | var JetBackup = function(options) { this.init(options); }; |
| 2 | JetBackup.prototype = { |
| 3 | _lang: '', |
| 4 | _mfa: {}, |
| 5 | _lang_default: 'en_US', |
| 6 | _path: { |
| 7 | api: './admin-ajax.php?action=jetbackup_api', |
| 8 | download: './admin.php?page=jetbackup', |
| 9 | media: '', |
| 10 | location: '', |
| 11 | lang_cdn_release: '_e0', // _e0 is the latest |
| 12 | lang_cdn_project: '_a7213103036961125313e9d0a2cf', |
| 13 | lang_cdn: 'https://delivery.localazy.com' |
| 14 | }, |
| 15 | _account: {}, |
| 16 | _language_ns: [], |
| 17 | _language_cdn: false, |
| 18 | _info: {}, |
| 19 | _lib_files: [ |
| 20 | "/libraries/moment-with-locales.js", |
| 21 | //"/libraries/bootstrap/js/bootstrap.bundle.min.js", |
| 22 | "/libraries/angularjs/1.8.2/angular.min.js", |
| 23 | "/libraries/angularjs/1.8.2/angular-aria.min.js", |
| 24 | "/libraries/angularjs/1.8.2/angular-route.min.js", |
| 25 | "/libraries/angularjs/1.8.2/angular-animate.min.js", |
| 26 | "/libraries/angularjs/1.8.2/angular-sanitize.min.js", |
| 27 | "/libraries/angularjs-material/1.1.6/angular-material.min.js", |
| 28 | "/libraries/angular-moment-picker/angular-moment-picker.min.js", |
| 29 | "/libraries/angularjs-i18next/i18next.js", |
| 30 | "/libraries/angularjs-i18next/i18nextHttpBackend.js", |
| 31 | "/libraries/angularjs-i18next/i18nextSprintfPostProcessor.js", |
| 32 | "/libraries/angularjs-i18next/ng-i18next.js", |
| 33 | "/libraries/ui-bootstrap/ui-bootstrap-tpls-2.5.0.min.js", |
| 34 | "/libraries/angular-loading-bar/loading-bar.min.js", |
| 35 | "/libraries/requirejs/requirejs.min.js", |
| 36 | ], |
| 37 | _loadLang: function(callback) { |
| 38 | if(callback === undefined) callback = function() {}; |
| 39 | if(this._language_cdn) this._loadLangCDN(callback); |
| 40 | else this._loadLangLocal(callback); |
| 41 | }, |
| 42 | _loadLangCDN: function (callback) { |
| 43 | if(callback === undefined) callback = function() {}; |
| 44 | |
| 45 | var self = this; |
| 46 | |
| 47 | var oReq = new XMLHttpRequest(); |
| 48 | oReq.addEventListener("load", function () { |
| 49 | |
| 50 | if(this.status !== 200) { |
| 51 | self._loadLangLocal(callback); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | var files = JSON.parse(this.responseText).files; |
| 56 | var cdn_files_map = {}; |
| 57 | for(var key in files) { |
| 58 | var filename = files[key].file; |
| 59 | // remove the .json suffix |
| 60 | filename = filename.substring(-5, filename.length-5); |
| 61 | cdn_files_map[filename] = {key: key, languages:[]}; |
| 62 | var locales = files[key].locales; |
| 63 | for(var locale in locales) { |
| 64 | var language = locales[locale].language; |
| 65 | if(locales[locale].region) language += '_' + locales[locale].region; |
| 66 | language = language.replace('-', '_').toLowerCase(); |
| 67 | cdn_files_map[filename].languages[language] = locales[locale].uri; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | self._loadLangLocal(callback, cdn_files_map); |
| 72 | }); |
| 73 | oReq.open("GET", self._path.lang_cdn + '/' + self._path.lang_cdn_project + '/' + self._path.lang_cdn_release + '.v2.json'); |
| 74 | oReq.send(); |
| 75 | }, |
| 76 | _loadLangLocal: function(callback, cdn_files_map) { |
| 77 | |
| 78 | if(callback === undefined) callback = function() {}; |
| 79 | if(cdn_files_map === undefined) cdn_files_map = {}; |
| 80 | |
| 81 | if (!window.i18next) return; |
| 82 | |
| 83 | var self = this; |
| 84 | |
| 85 | window.i18next |
| 86 | .use(window.i18nextHttpBackend) |
| 87 | .use(window.i18nextSprintfPostProcessor); |
| 88 | |
| 89 | var currentLang = this._lang ? this._lang : 'en_US'; |
| 90 | var fallbackLng = []; |
| 91 | |
| 92 | if(currentLang !== 'en_US') fallbackLng.push(currentLang); |
| 93 | fallbackLng.push('en_US'); |
| 94 | |
| 95 | window.i18next.init({ |
| 96 | debug: false, |
| 97 | lng: currentLang, |
| 98 | fallbackLng: fallbackLng, |
| 99 | backend: { |
| 100 | loadPath: function (lang, ns) { |
| 101 | |
| 102 | lang = lang[0]; |
| 103 | ns = ns[0]; |
| 104 | |
| 105 | var version = (self._info.development ? self._randomHash(12) : self._info.version); |
| 106 | |
| 107 | // if the language isn't supported or the file not found in our CDN, try to use the local language files |
| 108 | if(cdn_files_map[ns] === undefined || cdn_files_map[ns].languages[lang.toLowerCase()] === undefined) |
| 109 | return self._path.media + '/lang/' + lang + '/{{ns}}.json?v=' + version; |
| 110 | |
| 111 | return self._path.lang_cdn + cdn_files_map[ns].languages[lang.toLowerCase()] + '?v=' + version; |
| 112 | } |
| 113 | }, |
| 114 | ns: self._language_ns, |
| 115 | defaultNS: 'common', |
| 116 | fallbackNS: 'common', |
| 117 | keySeparator: "|.|", |
| 118 | nsSeparator: "|:|", |
| 119 | pluralSeparator: "|_|", |
| 120 | contextSeparator: "|_|", |
| 121 | useCookie: false, |
| 122 | useLocalStorage: false, |
| 123 | load: 'currentOnly', |
| 124 | postProcess: [ 'sprintf' ] |
| 125 | }, function (err, t) { |
| 126 | callback(); |
| 127 | }); |
| 128 | }, |
| 129 | _api: function(func, callback) { |
| 130 | var oReq = new XMLHttpRequest(); |
| 131 | oReq.addEventListener("load", function () { |
| 132 | |
| 133 | if(this.status != 200 || /^</.test(this.responseText)) { |
| 134 | var message = ""; |
| 135 | if(this.status != 200) message = "API call failed with response code " + this.status; |
| 136 | else message = "API call returned invalid response. Error: " + this.responseText; |
| 137 | callback({ success: 0, message: message, data: {} }); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | callback(JSON.parse(this.responseText)); |
| 142 | }); |
| 143 | oReq.open("POST", this._path.api + "&nonce=" + window.PAGE.nonce + "&actionType=" + func); |
| 144 | oReq.send(); |
| 145 | }, |
| 146 | _loadMain: function(){ |
| 147 | var self = this; |
| 148 | require.config({ |
| 149 | baseUrl: self._path.media, |
| 150 | waitSeconds: 0, |
| 151 | urlArgs: 'v=' + self._info.version |
| 152 | }); |
| 153 | |
| 154 | self._loadLang(function() { self._loadJS("/main" + (self._info.development ? '' : '.min') + ".js"); }); |
| 155 | }, |
| 156 | _loadLibCount: 0, |
| 157 | _loadLibRetries: 0, |
| 158 | _rand: null, |
| 159 | _randomHash :function(length) { |
| 160 | |
| 161 | if(this._rand) return this._rand; |
| 162 | |
| 163 | this._rand = ''; |
| 164 | var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 165 | var charactersLength = characters.length; |
| 166 | for ( var i = 0; i < length; i++ ) { |
| 167 | this._rand += characters.charAt(Math.floor(Math.random() * charactersLength)); |
| 168 | } |
| 169 | return this._rand; |
| 170 | }, |
| 171 | _loadLib: function() { |
| 172 | var self = this; |
| 173 | |
| 174 | self._api('panelPreload', function (response) { |
| 175 | self._loadLibRetries++; |
| 176 | if(!response.success) { |
| 177 | var message = "Failed loading panel preload information. Error: " + response.message; |
| 178 | if(self._loadLibRetries > 15) self._addLoaderError(message); |
| 179 | setTimeout(function () { self._loadLib(); }, 2000); |
| 180 | throw Error(message); |
| 181 | } |
| 182 | |
| 183 | var error = document.getElementById('JetBackupLoadingError'); |
| 184 | if(error !== null) error.innerHTML = ''; |
| 185 | |
| 186 | self._info = response.data.info; |
| 187 | self._info.version = response.system.version; |
| 188 | self._account = response.data.account; |
| 189 | self._configuration = response.data.configuration; |
| 190 | self._integration = response.data.integration; |
| 191 | self._mfa = response.data.mfa; |
| 192 | |
| 193 | self._language_ns = response.data.language_ns; |
| 194 | self._language_cdn = response.data.language_cdn; |
| 195 | |
| 196 | window.PAGE.info = self._info; |
| 197 | window.PAGE.system = response.system; |
| 198 | window.PAGE.account = self._account; |
| 199 | window.PAGE.configuration = self._configuration; |
| 200 | window.PAGE.integration = self._integration; |
| 201 | window.PAGE.mfa = self._mfa; |
| 202 | |
| 203 | window.PAGE.license = response.data.license; |
| 204 | window.PAGE.nonce = response.system.nonce; |
| 205 | |
| 206 | for (var j=0; self._lib_files.length>j; j++) { |
| 207 | self._loadJS(self._lib_files[j], function () { |
| 208 | if (++self._loadLibCount >= self._lib_files.length) self._loadMain(); |
| 209 | }); |
| 210 | } |
| 211 | }); |
| 212 | |
| 213 | }, |
| 214 | _loadJS: function(file, onload) { |
| 215 | if(onload === undefined || typeof onload != 'function') onload = function () {}; |
| 216 | var script = document.createElement('script'); |
| 217 | script.src = this._path.media + file + '?v=' + (this._info.development ? this._randomHash(12) : this._info.version); |
| 218 | script.type = 'text/javascript'; |
| 219 | script.async = false; |
| 220 | script.addEventListener('load', onload); |
| 221 | document.head.appendChild(script); |
| 222 | }, |
| 223 | _addLoaderError: function(message) { |
| 224 | var error = document.getElementById('JetBackupLoadingError'); |
| 225 | if(error === null) return false; |
| 226 | if(!message) return true; |
| 227 | error.innerHTML = '<div style="margin-top: 30px;">' + message + '</div>'; |
| 228 | return true; |
| 229 | |
| 230 | }, |
| 231 | _buildLoader: function() { |
| 232 | |
| 233 | var html = ''; |
| 234 | html += "<div style='text-align: center; padding: 30px; 0; color: #ffffff; margin-top: 150px;'>"; |
| 235 | html += "<img src='" + this._path.media + "/images/logo-loader.png' class='pounding-logo' alt='JetBackup' />"; |
| 236 | html += "<div id='JetBackupLoadingError'></div>"; |
| 237 | html += "</div>"; |
| 238 | |
| 239 | var main = document.getElementById('JetBackup'); |
| 240 | if(main === null) return; |
| 241 | main.innerHTML = html; |
| 242 | |
| 243 | }, |
| 244 | init: function (options) { |
| 245 | |
| 246 | this._lang = options.language; |
| 247 | |
| 248 | if(!window.PAGE) window.PAGE = {}; |
| 249 | window.PAGE.lang = this._lang; |
| 250 | window.PAGE.convertedToMomentLocales = window.PAGE.lang.split('_')[0] |
| 251 | window.PAGE.nonce = options.nonce; |
| 252 | window.PAGE.dateFormat = options.dateFormat; |
| 253 | window.PAGE.timeFormat = options.timeFormat; |
| 254 | |
| 255 | this._path.location = window.location.pathname; |
| 256 | this._path.media = options.plugin_path; |
| 257 | |
| 258 | // Adjust API path only if "/network/" is found |
| 259 | if (window.location.pathname.includes('/network/')) { |
| 260 | const basePath = window.location.pathname.replace('/network/', '/'); |
| 261 | this._path.api = window.location.origin + basePath.split('/wp-admin/')[0] + '/wp-admin/admin-ajax.php?action=jetbackup_api'; |
| 262 | } |
| 263 | |
| 264 | window.PAGE.path = this._path; |
| 265 | |
| 266 | this._buildLoader(); |
| 267 | this._loadLib(); |
| 268 | } |
| 269 | }; |