forked from SIMRacingApps/SIMRacingAppsWebContent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.js
300 lines (261 loc) · 13.8 KB
/
default.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
'use strict';
require(SIMRacingAppsRequireConfig,
['angular','angular-sanitize'],
function( angular,angularSanitize ) {
angular.element(document).ready(function() {
var module = angular.module('SIMRacingApps',['ngSanitize']);
//call modules configuration function
module.config(
['$locationProvider',
function($locationProvider) {
$locationProvider.html5Mode(true); //this is for the $location service to work right.
}]);
module.controller("SIMRacingApps-Controller",
['$scope', '$http', '$window', '$location', '$document','$locale',
function($scope,$http,$window,$location,$document,$locale) {
$scope.headers = {};
$scope.listings = {};
$scope.version = {};
$scope.websiteVersion = {};
$scope.websiteBETAVersion = {};
$scope.versionString = "";
$scope.updateString = "";
$scope.translations = {};
$scope.lang = "";
$scope.country = "";
$scope.locale = "";
var args = $location.search();
$scope.args = "";
for (var arg in args) {
$scope.args += '&'+arg+'='+args[arg];
if (arg == "lang")
$scope.lang = args[arg];
if (arg == "country")
$scope.country = args[arg];
if (arg == "locale")
$scope.locale = args[arg];
}
//if user did not pass in a lang, then use the default one.
if ($scope.lang.length == 0 && $scope.country.length == 0) {
//if the browser sets the navigator.language, then use it instead of $locale.id
console.log("$locale = "+JSON.stringify($locale));
var l = ($locale.id).split(/[-_@;]/);
if (l.length > 1) {
$scope.lang = l[0];
$scope.country = l[1];
}
else
if (l.length > 0) {
$scope.lang = l[0];
}
}
if ($scope.locale.length == 0) {
$scope.locale = $scope.country.length > 0
? $scope.lang + "-" + $scope.country
: $scope.lang;
}
// sraDispatcher.loadTranslations("/SIMRacingApps","text",function(path) {
// $scope.translations = sraDispatcher.getTranslation(path,"auto");
// });
$scope.url = function(url) {
var rUrl;
if (url.match(/^http/)) {
rUrl = url;
}
else {
rUrl = document.location.origin + url;
}
if (rUrl.length >= 2 && rUrl.substring(rUrl.length-1) == "&")
rUrl = rUrl.substring(0,rUrl.length-1);
if (rUrl.length >= 2 && rUrl.substring(rUrl.length-1) == "?")
rUrl = rUrl.substring(0,rUrl.length-1);
if (rUrl.indexOf('?') < 0 && $scope.args)
rUrl = rUrl + '?' + $scope.args.substring(1);
else
rUrl = rUrl + $scope.args;
return rUrl;
};
$scope.launch = function(url,width,height) {
var useWidth = width || 800;
var useHeight = height || 480;
console.log('launch('+$scope.url(url)+')');
var eventWin = $window.open($scope.url(url), "_blank", 'width=' + useWidth + ', height=' + useHeight + ', resizable=1, scrollbars=1, status=0, toolbar=0, location=0, menubar=0');
//var eventWin = $window.open(url + $scope.args, url + $scope.args, 'width=' + useWidth + ', height=' + useHeight + ', resizable=1, scrollbars=1, status=0, toolbar=0, location=0, menubar=0');
eventWin.focus();
};
$scope.checkVersion = function() {
if ($scope.version.major && $scope.websiteVersion.major && $scope.websiteBETAVersion.major) {
//$scope.version.major = 1;
//$scope.version.minor = 1;
//$scope.version.build = "BETA-2016.04.01";
//$scope.websiteVersion.major = 1;
//$scope.websiteVersion.minor = 0;
//$scope.websiteVersion.build = "2016.05.01";
//$scope.websiteBETAVersion.major = 1;
//$scope.websiteBETAVersion.minor = 1;
//$scope.websiteBETAVersion.build = "RC-2016.05.15";
$scope.version.major *= 1;
$scope.version.minor *= 1;
$scope.websiteVersion.major *= 1;
$scope.websiteVersion.minor *= 1;
$scope.websiteBETAVersion.major *= 1;
$scope.websiteBETAVersion.minor *= 1;
$scope.translations = $scope.version.translations;
$scope.versionString = $scope.translations.VERSION+": "+$scope.version.major+"."+$scope.version.minor+"-"+$scope.version.build + ' [' + $scope.locale + ']';
var websiteBETABuild = "";
var a = $scope.websiteBETAVersion.build.split(/[-.]/);
for (var i=0;i < a.length;i++) {
if (a[i].match(/[0-9]/))
websiteBETABuild += "" + a[i];
}
websiteBETABuild *= 1;
var websiteBuild = "";
var a = $scope.websiteVersion.build.split(/[-.]/);
for (var i=0;i < a.length;i++) {
if (a[i].match(/[0-9]/))
websiteBuild += "" + a[i];
}
websiteBuild *= 1;
var build = "";
a = $scope.version.build.split(/[-.]/);
for (var i=0;i < a.length;i++) {
if (a[i].match(/[0-9]/))
build += "" + a[i];
}
build *= 1;
//if the main version on the website is newer, post it as the update
if ($scope.version.major < $scope.websiteVersion.major
|| ($scope.version.major == $scope.websiteVersion.major && $scope.version.minor < $scope.websiteVersion.minor)
|| ($scope.version.major == $scope.websiteVersion.major && $scope.version.minor == $scope.websiteVersion.minor && build < websiteBuild)
) {
$scope.updateString += "(UPDATE Available: "+$scope.websiteVersion.major+"."+$scope.websiteVersion.minor+"-"+$scope.websiteVersion.build+")";
}
else //if this is a BETA/RC version, then post the new BETA/RC as the update
if (($scope.version.build.match(/^BETA-/) || $scope.version.build.match(/^RC-/))
&& ($scope.version.major < $scope.websiteBETAVersion.major
|| ($scope.version.major == $scope.websiteBETAVersion.major && $scope.version.minor < $scope.websiteBETAVersion.minor)
|| ($scope.version.major == $scope.websiteBETAVersion.major && $scope.version.minor == $scope.websiteBETAVersion.minor && build < websiteBETABuild))
) {
$scope.updateString += "(UPDATE Available: "+$scope.websiteBETAVersion.major+"."+$scope.websiteBETAVersion.minor+"-"+$scope.websiteBETAVersion.build+")";
}
}
};
var request = {
method: 'GET',
url: '/SIMRacingApps/listings' + ($scope.locale.length > 0 ? '?lang='+$scope.locale : ''),
cache: false,
timeout: 5000
};
$http(request)
.success(function(data, status, headers, config) {
$scope.version = data.version;
$scope.headers = data.headers;
$document[0].title = 'SIMRacingApps '+ $scope.version.major+'.'+$scope.version.minor+'_'+$scope.version.build + ' [' + $scope.locale + ']';
ga('set','av',$scope.version.major+'.'+$scope.version.minor+'_'+$scope.version.build);
ga('send', 'pageview', '/SIMRacingApps');
$scope.checkVersion();
for (var headeridx in data.headers) {
var header = data.headers[headeridx];
$scope.listings[header] = [];
for (var itemidx in data[header]) {
var item = data[header][itemidx];
var count = 0;
var newitem = {};
if (item.url.match(/^http/)) {
newitem.url = item.url;
}
else {
newitem.url = '/SIMRacingApps/'+item.url;
}
if (item.doc.match(/^http/)) {
newitem.doc = item.doc;
}
else {
newitem.doc = '/SIMRacingApps/'+item.doc;
}
newitem.width = item.width;
newitem.height = item.height;
newitem.description = item.description;
newitem.icon = item.icon;
newitem.name = item.name;
if (item.args) {
if (newitem.url.indexOf('?') < 0)
newitem.url += '?' + item.args;
else
newitem.url += item.args;
}
console.log(header+"."+newitem.name+" = "+JSON.stringify(newitem))
$scope.listings[header].push(newitem);
}
}
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("error - "+status);
});
$http({
method: 'GET',
url: 'http://SIMRacingApps.com/version.json',
cache: false,
timeout: 5000
})
.success(function(data, status, headers, config) {
console.log("SIMRacingApps.com/version.json: " + JSON.stringify(data));
$scope.websiteVersion = data;
$scope.checkVersion();
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("version.json: error - "+status);
});
$http({
method: 'GET',
url: 'http://SIMRacingApps.com/BETA-version.json',
cache: false,
timeout: 5000
})
.success(function(data, status, headers, config) {
console.log("SIMRacingApps.com/BETA-version.json: " + JSON.stringify(data));
$scope.websiteBETAVersion = data;
$scope.checkVersion();
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("version.json: error - "+status);
});
}]);
var i18n = 'i18n/angular-locale_';
//this method confirmed with IE
if (window.navigator.userLanguage)
i18n += window.navigator.userLanguage.toLowerCase().split(/[-_]/).join('-');
else
if (window.navigator.language)
//this method confirmed with FireFox, Chrome, Safari iOS8
i18n += window.navigator.language.toLowerCase().split(/[-_]/).join('-');
else
i18n += 'en';
var search = window.location.search;
if (search) {
//now parse the URL search string for arguments we need before bootstrapping angular
search = search.split(/[\?&]/);
for (var i=0; i < search.length; i++) {
var s = search[i].split("=");
if (s.length > 1) {
if (s[0].toLowerCase() == "lang") {
i18n = 'i18n/angular-locale_'+s[1].toLowerCase().split(/[-_]/).join('-');
}
}
}
}
require([i18n],
function(vi18n) {
angular.bootstrap(document.body,['SIMRacingApps']);
}, function(err) {
console.log(err.toString());
angular.element(document.body).prepend( "<div class='SIMRacingAppsError'>"+err.toString()+"</div>" );
});
});
});