/
mobile_settings.controller.js
157 lines (143 loc) · 5.77 KB
/
mobile_settings.controller.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
export default ['$scope', 'config', 'Core', 'hs.map.service', '$window',
function ($scope, config, Core, OlMap, $window) {
$scope.Core = Core;
configDebug = config;
$scope.settingsDb = settingsDb;
$scope.originalHostnames = $.extend({}, config.hostname);
$scope.hostnames = config.hostnames;
config.hostname["default"] = config.hostnames[0];
$scope.selectedHostname = config.hostnames[0].title;
/**
* @function addHostname
* @memberOf hs.mobile_settings.controller
* @description TODO
*/
$scope.addHostname = function () {
if ($scope.userHostname) {
config.hostname["user"] = {
"title": "User hostname",
"type": "user",
"editable": true,
"url": $scope.userHostname
};
settingsDb.transaction(function (tx) {
tx.executeSql('REPLACE INTO Hostnames VALUES (?,?,?,?)', [config.hostname.user.title, config.hostname.user.type, config.hostname.user.editable, config.hostname.user.url], function (tx, result) {
$scope.userHostname = "";
console.log(result.insertId);
});
});
}
}
$scope.changeHostname = function () {
console.log(this, $scope.hostname);
angular.forEach($scope.hostnames, function (hostname) {
if ($scope.selectedHostname == hostname.title) {
config.hostname[hostname.type] = hostname;
}
});
}
/**
* @function deleteHostname
* @memberOf hs.mobile_settings.controller
* @description TODO
*/
$scope.deleteHostname = function () {
delete $scope.hostname[this.hostname.type];
$scope.deleteRow(settingsDb, this.hostname.type);
}
/**
* @function preFill
* @memberOf hs.mobile_settings.controller
* @description TODO
*/
$scope.preFill = function () {
$scope.userHostname = !$scope.userHostname ? "http://" : $scope.userHostname;
}
/**
* @function removePreFill
* @memberOf hs.mobile_settings.controller
* @description TODO
*/
$scope.removePreFill = function () {
$scope.userHostname = $scope.userHostname == "http://" ? "" : $scope.userHostname;
}
/**
* @function initSettings
* @memberOf hs.mobile_settings.controller
* @params {Unknown} db
* @description TODO
*/
$scope.initSettings = function (db) {
if (console) {
console.log("Populating hostnames database.");
console.log($scope.hostname);
console.log(config.hostname);
console.log(settingsDb);
// config.hostname = $.extend({}, $scope.originalHostnames);
}
$scope.hostname = config.hostname;
db.transaction(function (tx) {
tx.executeSql('DROP TABLE IF EXISTS Hostnames', [], console.log("Dropping hostnames table."));
tx.executeSql('CREATE TABLE IF NOT EXISTS Hostnames (title unique, type, editable, url)', [], console.log("Creating hostnames table."));
$.each($scope.hostname, function (key, value) {
tx.executeSql('INSERT INTO Hostnames VALUES (?,?,?,?)', [value.title, value.type, value.editable, value.url]);
});
}, function () {
//TODO Error
});
}
/**
* @function loadSettingsFromDb
* @memberOf hs.mobile_settings.controller
* @params {unknown} tx
* @description TODO
*/
$scope.loadSettingsFromDb = function (tx) {
dbHostnames = {};
tx.executeSql('SELECT * FROM Hostnames', [], function (tx, results) {
// console.log(results.rows.length + ' rows found.');
for (var i = 0; i < results.rows.length; i++) {
// console.log(results.rows.item(i));
dbHostnames[results.rows.item(i).type] = {
"title": results.rows.item(i).title,
"type": results.rows.item(i).type,
"editable": JSON.parse(results.rows.item(i).editable),
"url": results.rows.item(i).url
}
}
});
}
/**
* @function deleteRow
* @memberOf hs.mobile_settings.controller
* @params {Unknown} db
* @params {Unknown} type
* @description TODO
*/
$scope.deleteRow = function (db, type) {
db.transaction(function (tx) {
tx.executeSql('DELETE FROM Hostnames WHERE type = ?', [type]);
});
}
settingsDb.transaction($scope.loadSettingsFromDb, function (error) {
console.log(error);
$scope.initSettings(settingsDb);
console.log("Loading initial settings.");
}, function () {
if (Object.keys(dbHostnames)[0]) {
config.hostname = dbHostnames;
$scope.hostname = config.hostname;
console.log("Loading settings from memory.");
}
});
function removeLoadingLogo() {
var el = document.getElementById('hs-loading-logo');
if (el) {
el.parentElement.removeChild(el);
$timeout.cancel(logoRemoverTimeout);
}
}
$scope.$on('scope_loaded', removeLoadingLogo);
$scope.$emit('scope_loaded', "Mobile Settings");
}
]