2014-11-26 12:39:59 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2014-11-26 12:39:59 +00:00
|
|
|
|
|
|
|
/*jslint browser: true, continue: true, plusplus: true */
|
|
|
|
/*global $: false, angular: false, console: false, validLangs: false */
|
|
|
|
|
|
|
|
var syncthing = angular.module('syncthing', [
|
2015-04-25 21:53:44 +00:00
|
|
|
'angularUtils.directives.dirPagination',
|
2014-11-26 12:39:59 +00:00
|
|
|
'pascalprecht.translate',
|
2015-08-02 06:27:05 +00:00
|
|
|
|
|
|
|
'syncthing.core',
|
|
|
|
'syncthing.device',
|
|
|
|
'syncthing.folder',
|
|
|
|
'syncthing.settings',
|
|
|
|
'syncthing.transfer',
|
|
|
|
'syncthing.usagereport'
|
2014-11-26 12:39:59 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
var urlbase = 'rest';
|
|
|
|
|
2014-11-28 18:39:33 +00:00
|
|
|
syncthing.config(function ($httpProvider, $translateProvider, LocaleServiceProvider) {
|
2015-07-21 21:41:10 +00:00
|
|
|
$httpProvider.interceptors.push(function xHeadersResponseInterceptor() {
|
2015-07-21 21:47:35 +00:00
|
|
|
var guiVersion = null;
|
|
|
|
var deviceId = null;
|
2015-08-02 07:05:19 +00:00
|
|
|
|
2014-11-26 12:39:59 +00:00
|
|
|
return {
|
2015-07-21 21:41:10 +00:00
|
|
|
response: function onResponse(response) {
|
2015-07-21 20:35:51 +00:00
|
|
|
var headers = response.headers();
|
2015-07-21 21:41:10 +00:00
|
|
|
var responseVersion;
|
|
|
|
var deviceIdShort;
|
2015-08-02 07:05:19 +00:00
|
|
|
|
2015-07-21 20:35:51 +00:00
|
|
|
// angular template cache sends no headers
|
2015-07-21 21:41:10 +00:00
|
|
|
if(Object.keys(headers).length === 0) {
|
|
|
|
return response;
|
|
|
|
}
|
2015-08-02 07:05:19 +00:00
|
|
|
|
2015-07-21 21:41:10 +00:00
|
|
|
responseVersion = headers['x-syncthing-version'];
|
2015-08-02 07:05:19 +00:00
|
|
|
|
2015-07-21 21:41:10 +00:00
|
|
|
if (!guiVersion) {
|
|
|
|
guiVersion = responseVersion;
|
|
|
|
} else if (guiVersion != responseVersion) {
|
|
|
|
document.location.reload(true);
|
|
|
|
}
|
2015-08-02 07:05:19 +00:00
|
|
|
|
2015-07-21 21:41:10 +00:00
|
|
|
if (!deviceId) {
|
|
|
|
deviceId = headers['x-syncthing-id'];
|
|
|
|
if (deviceId) {
|
|
|
|
deviceIdShort = deviceId.substring(0, 5);
|
|
|
|
$httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token-' + deviceIdShort;
|
|
|
|
$httpProvider.defaults.xsrfCookieName = 'CSRF-Token-' + deviceIdShort;
|
2015-06-22 15:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-02 07:05:19 +00:00
|
|
|
|
2014-11-26 12:39:59 +00:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2014-11-28 18:39:33 +00:00
|
|
|
// language and localisation
|
|
|
|
|
2014-11-26 12:39:59 +00:00
|
|
|
$translateProvider.useStaticFilesLoader({
|
|
|
|
prefix: 'assets/lang/lang-',
|
|
|
|
suffix: '.json'
|
|
|
|
});
|
|
|
|
|
2014-11-28 18:39:33 +00:00
|
|
|
LocaleServiceProvider.setAvailableLocales(validLangs);
|
|
|
|
LocaleServiceProvider.setDefaultLocale('en');
|
|
|
|
|
2014-11-26 12:39:59 +00:00
|
|
|
});
|
|
|
|
|
2015-04-28 15:34:55 +00:00
|
|
|
// @TODO: extract global level functions into separate service(s)
|
2014-11-26 12:39:59 +00:00
|
|
|
|
|
|
|
function deviceCompare(a, b) {
|
2015-04-18 10:23:21 +00:00
|
|
|
if (typeof a.name !== 'undefined' && typeof b.name !== 'undefined') {
|
|
|
|
if (a.name < b.name)
|
2014-11-26 12:39:59 +00:00
|
|
|
return -1;
|
2015-04-20 13:18:19 +00:00
|
|
|
return a.name > b.name;
|
2014-11-26 12:39:59 +00:00
|
|
|
}
|
2015-04-18 10:23:21 +00:00
|
|
|
if (a.deviceID < b.deviceID) {
|
2014-11-26 12:39:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-04-18 10:23:21 +00:00
|
|
|
return a.deviceID > b.deviceID;
|
2014-11-26 12:39:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function folderCompare(a, b) {
|
2015-04-18 10:23:21 +00:00
|
|
|
if (a.id < b.id) {
|
2014-11-26 12:39:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-04-18 10:23:21 +00:00
|
|
|
return a.id > b.id;
|
2014-11-26 12:39:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function folderMap(l) {
|
|
|
|
var m = {};
|
|
|
|
l.forEach(function (r) {
|
2015-03-10 22:45:43 +00:00
|
|
|
m[r.id] = r;
|
2014-11-26 12:39:59 +00:00
|
|
|
});
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
function folderList(m) {
|
|
|
|
var l = [];
|
|
|
|
for (var id in m) {
|
|
|
|
l.push(m[id]);
|
|
|
|
}
|
|
|
|
l.sort(folderCompare);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
function decimals(val, num) {
|
|
|
|
var digits, decs;
|
|
|
|
|
|
|
|
if (val === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
|
|
|
|
decs = Math.max(0, num - digits);
|
|
|
|
return decs;
|
|
|
|
}
|
|
|
|
|
2014-12-29 12:48:26 +00:00
|
|
|
function randomString(len) {
|
|
|
|
var i, result = '', chars = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-';
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
result += chars[Math.round(Math.random() * (chars.length - 1))];
|
2014-11-26 12:39:59 +00:00
|
|
|
}
|
2014-12-29 12:48:26 +00:00
|
|
|
return result;
|
2014-11-26 12:39:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isEmptyObject(obj) {
|
|
|
|
var name;
|
|
|
|
for (name in obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function debounce(func, wait) {
|
|
|
|
var timeout, args, context, timestamp, result, again;
|
|
|
|
|
|
|
|
var later = function () {
|
|
|
|
var last = Date.now() - timestamp;
|
|
|
|
if (last < wait) {
|
|
|
|
timeout = setTimeout(later, wait - last);
|
|
|
|
} else {
|
|
|
|
timeout = null;
|
|
|
|
if (again) {
|
|
|
|
again = false;
|
|
|
|
result = func.apply(context, args);
|
|
|
|
context = args = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return function () {
|
|
|
|
context = this;
|
|
|
|
args = arguments;
|
|
|
|
timestamp = Date.now();
|
|
|
|
var callNow = !timeout;
|
|
|
|
if (!timeout) {
|
|
|
|
timeout = setTimeout(later, wait);
|
|
|
|
result = func.apply(context, args);
|
|
|
|
context = args = null;
|
|
|
|
} else {
|
|
|
|
again = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|