2015-02-08 14:57:02 +01:00
|
|
|
angular.module('OpenSlidesApp.core', [])
|
|
|
|
|
2015-03-01 07:15:44 +01:00
|
|
|
.config(function($stateProvider, $urlMatcherFactoryProvider) {
|
|
|
|
// Make the trailing slash optional
|
|
|
|
$urlMatcherFactoryProvider.strictMode(false)
|
|
|
|
|
2015-02-08 14:57:02 +01:00
|
|
|
// Use stateProvider.decorator to give default values to our states
|
|
|
|
$stateProvider.decorator('views', function(state, parent) {
|
|
|
|
var result = {},
|
|
|
|
views = parent(state);
|
|
|
|
|
2015-02-14 10:10:08 +01:00
|
|
|
if (state.abstract || state.data && state.data.extern) {
|
2015-02-08 14:57:02 +01:00
|
|
|
return views;
|
|
|
|
}
|
|
|
|
|
|
|
|
angular.forEach(views, function(config, name) {
|
2015-02-14 10:10:08 +01:00
|
|
|
|
2015-02-08 14:57:02 +01:00
|
|
|
// Sets default values for templateUrl
|
|
|
|
var patterns = state.name.split('.'),
|
|
|
|
templateUrl,
|
|
|
|
controller,
|
|
|
|
defaultControllers = {
|
|
|
|
create: 'CreateCtrl',
|
|
|
|
update: 'UpdateCtrl',
|
|
|
|
list: 'ListCtrl',
|
|
|
|
detail: 'DetailCtrl',
|
|
|
|
};
|
|
|
|
|
|
|
|
// templateUrl
|
|
|
|
if (_.last(patterns).match(/(create|update)/)) {
|
|
|
|
// When state_patterns is in the form "app.module.create" or
|
|
|
|
// "app.module.update", use the form template.
|
|
|
|
templateUrl = 'static/templates/' + patterns[0] + '/' + patterns[1] + '-form.html'
|
|
|
|
} else {
|
|
|
|
// Replaces the first point through a slash (the app name)
|
|
|
|
var appName = state.name.replace('.', '/');
|
|
|
|
// Replaces any folowing points though a -
|
|
|
|
templateUrl = 'static/templates/' + appName.replace(/\./g, '-') + '.html';
|
|
|
|
}
|
|
|
|
config.templateUrl = config.templateUrl || templateUrl;
|
|
|
|
|
|
|
|
// controller
|
|
|
|
if (patterns.length >= 3) {
|
|
|
|
controller = _.capitalize(patterns[1]) + defaultControllers[_.last(patterns)];
|
|
|
|
config.controller = config.controller || controller;
|
|
|
|
}
|
|
|
|
result[name] = config;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
|
|
|
|
.decorator('url', function(state, parent) {
|
|
|
|
var defaultUrl;
|
|
|
|
|
|
|
|
if (state.abstract) {
|
|
|
|
defaultUrl = '';
|
|
|
|
} else {
|
|
|
|
var patterns = state.name.split('.'),
|
|
|
|
defaultUrls = {
|
|
|
|
create: '/new',
|
|
|
|
update: '/edit',
|
|
|
|
list: '',
|
2015-03-01 07:15:44 +01:00
|
|
|
// The id is expected to be an integer, if not, the url has to
|
|
|
|
// be defined manually
|
|
|
|
detail: '/{id:int}',
|
2015-02-08 14:57:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
defaultUrl = defaultUrls[_.last(patterns)];
|
|
|
|
}
|
|
|
|
|
|
|
|
state.url = state.url || defaultUrl;
|
|
|
|
return parent(state)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2015-02-12 22:42:54 +01:00
|
|
|
.config(function($stateProvider, $locationProvider) {
|
2015-02-08 14:57:02 +01:00
|
|
|
// Core urls
|
2015-02-12 22:42:54 +01:00
|
|
|
$stateProvider.state('dashboard', {
|
2015-02-14 10:10:08 +01:00
|
|
|
url: '/',
|
|
|
|
templateUrl: 'static/templates/dashboard.html'
|
|
|
|
});
|
2015-02-08 14:57:02 +01:00
|
|
|
|
|
|
|
$locationProvider.html5Mode(true);
|
|
|
|
})
|
|
|
|
|
2015-02-13 23:23:48 +01:00
|
|
|
.config(function(DSProvider) {
|
|
|
|
// Reloads everything after 5 minutes.
|
|
|
|
// TODO: * find a way only to reload things that are still needed
|
|
|
|
DSProvider.defaults.maxAge = 5 * 60 * 1000; // 5 minutes
|
|
|
|
DSProvider.defaults.reapAction = 'none';
|
|
|
|
DSProvider.defaults.afterReap = function(model, items) {
|
|
|
|
if (items.length > 5) {
|
|
|
|
model.findAll({}, {bypassCache: true});
|
|
|
|
} else {
|
|
|
|
_.forEach(items, function (item) {
|
|
|
|
model.refresh(item[model.idAttribute]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
2015-02-14 10:10:08 +01:00
|
|
|
.provider('runtimeStates', function($stateProvider) {
|
|
|
|
this.$get = function($q, $timeout, $state) {
|
|
|
|
return {
|
|
|
|
addState: function(name, state) {
|
|
|
|
$stateProvider.state(name, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
.run(function(runtimeStates, $http) {
|
|
|
|
$http.get('/core/url_patterns/').then(function(data) {
|
|
|
|
for (var pattern in data.data) {
|
|
|
|
runtimeStates.addState(pattern, {
|
|
|
|
'url': data.data[pattern],
|
|
|
|
data: {extern: true},
|
|
|
|
onEnter: function($window) {
|
|
|
|
$window.location.href = this.url;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2015-02-08 14:57:02 +01:00
|
|
|
.run(function(DS, autoupdate) {
|
|
|
|
autoupdate.on_message(function(data) {
|
|
|
|
// TODO: when MODEL.find() is called after this
|
|
|
|
// a new request is fired. This could be a bug in DS
|
2015-03-01 08:42:47 +01:00
|
|
|
|
|
|
|
// TODO: Do not send the status code to the client, but make the decission
|
|
|
|
// on the server side. It is an implementation detail, that tornado
|
|
|
|
// sends request to wsgi, which should not concern the client.
|
2015-02-08 14:57:02 +01:00
|
|
|
if (data.status_code == 200) {
|
2015-03-01 08:42:47 +01:00
|
|
|
DS.inject(data.collection, data.data);
|
|
|
|
} else if (data.status_code == 404) {
|
|
|
|
DS.eject(data.collection, data.id);
|
2015-02-08 14:57:02 +01:00
|
|
|
}
|
|
|
|
// TODO: handle other statuscodes
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
.run(function($rootScope, Config) {
|
|
|
|
// Puts the config object into each scope.
|
|
|
|
// TODO: maybe rootscope.config has to set before findAll() is finished
|
|
|
|
Config.findAll().then(function() {;
|
|
|
|
$rootScope.config = function(key) {
|
|
|
|
return Config.get(key).value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
.factory('autoupdate', function() {
|
|
|
|
//TODO: use config here
|
|
|
|
var url = "http://" + location.host + "/sockjs";
|
|
|
|
|
|
|
|
var Autoupdate = {
|
|
|
|
socket: null,
|
|
|
|
message_receivers: [],
|
|
|
|
connect: function() {
|
|
|
|
var autoupdate = this;
|
|
|
|
this.socket = new SockJS(url);
|
|
|
|
|
|
|
|
this.socket.onmessage = function(event) {
|
|
|
|
_.forEach(autoupdate.message_receivers, function(receiver) {
|
|
|
|
receiver(event.data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.socket.onclose = function() {
|
|
|
|
setTimeout(autoupdate.connect, 5000);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
on_message: function(receiver) {
|
|
|
|
this.message_receivers.push(receiver);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Autoupdate.connect();
|
|
|
|
return Autoupdate;
|
|
|
|
})
|
|
|
|
|
|
|
|
.factory('Config', function(DS) {
|
|
|
|
return DS.defineResource({
|
|
|
|
name: 'config/config',
|
|
|
|
idAttribute: 'key',
|
|
|
|
endpoint: '/rest/config/config/'
|
|
|
|
});
|
2015-02-08 22:37:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
.controller("LanguageCtrl", function ($scope, gettextCatalog) {
|
|
|
|
// controller to switch app language
|
|
|
|
// TODO: detect browser language for default language
|
|
|
|
gettextCatalog.setCurrentLanguage('en');
|
|
|
|
//TODO: for debug only! (helps to find untranslated strings by adding "[MISSING]:")
|
|
|
|
gettextCatalog.debug = true;
|
|
|
|
$scope.switchLanguage = function (lang) {
|
|
|
|
gettextCatalog.setCurrentLanguage(lang);
|
|
|
|
if (lang != 'en') {
|
|
|
|
gettextCatalog.loadRemote("static/i18n/" + lang + ".json");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
.directive('osFocusMe', function ($timeout) {
|
|
|
|
return {
|
|
|
|
link: function (scope, element, attrs, model) {
|
|
|
|
$timeout(function () {
|
|
|
|
element[0].focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// some general JavaScript functions used in all OpenSlides apps
|
|
|
|
$(function () {
|
|
|
|
$('[data-toggle="tooltip"]').tooltip({'placement': 'bottom'})
|
2015-02-08 14:57:02 +01:00
|
|
|
});
|