UNPKG

sheercms

Version:

Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.

185 lines (158 loc) 5.31 kB
app.service('TemplateService', function($q, $http, $modal, DSCacheFactory) { var self = this; var templateCache = DSCacheFactory('templateCache', { maxAge: 1800000, // Items added to this cache expire after 30 minutes. cacheFlushInterval: 3600000, // This cache will clear itself every hour. deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire. storageMode: 'localStorage' // This cache will sync itself with `localStorage`. }); self.list = function(useCache) { var d = $q.defer(); var data = null; var key = "/cms/templates/list"; if (useCache === true) data = templateCache.get(key); if (data) { //console.log("Found template list in cache."); d.resolve(data); } else { //console.log("Getting template list from server."); $http.get("/cms/templates/list").success(function (data) { if (data.success && data.templates) { templateCache.put(key, data); d.resolve(data); } else { d.resolve(data); } }).error(function (data, status) { d.reject(data); }); } return d.promise; }; self.save = function(template) { var d = $q.defer(); var json = JSON.stringify(template); $http.post("/cms/templates/save", {data:json}).success(function(data) { clearCache(); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.getTemplate = function(id) { var d = $q.defer(); $http({ url:"/cms/templates/get", method: 'get', params: {templateId: id }}).success(function(data) { d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.delete = function(id) { var d = $q.defer(); $http.post("/cms/templates/delete", {templateId: id }).success(function(data) { clearCache(); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; /* onSuccess: (value) onCancel: () */ self.openTemplateSelector = function(title, text, multiple, onSuccess, onCancel) { var modalInstance = $modal.open({ templateUrl: 'TemplateSelectorDialog', controller: TemplateSelectorController, windowClass: 'large-dialog', resolve: { title: function() { return title; }, text: function() { return text; }, multiple: function() { return multiple; } } }); modalInstance.result.then(function (value) { if (onSuccess) { onSuccess(value); } }, function () { if (onCancel) { onCancel(); } }); }; function clearCache() { templateCache.removeAll(); //console.log("Template cache cleared."); } }); var TemplateSelectorController = function ($scope, $modalInstance, TemplateService, title, text, multiple) { $scope.data = { title: title, text: text, multiple: multiple, templates: [], message: "", messageType: "" }; $scope.ok = function () { var arr = []; for(var i = 0; i < $scope.data.templates.length; i++) { var t = $scope.data.templates[i]; if (t.selected === true) arr.push(t); } $modalInstance.close(arr); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; $scope.select = function(template) { if (template.selected === true) { template.selected = false; } else { if (multiple !== true) { //deselect all other templates for(var i = 0; i < $scope.data.templates.length; i++) $scope.data.templates[i].selected = false; } template.selected = true; } }; function loadTemplates() { //load the templates from the server TemplateService.list(true).then(function(data) { $scope.data.message = data.message; $scope.data.messageType = data.success == true ? "alert-success" : "alert-danger"; if (data.success) { if (data.templates && data.templates.length > 0) $scope.data.templates = data.templates; else { $scope.data.message = "There are no templates in the system."; $scope.data.messageType = "alert-warning"; $scope.data.templates = []; } } else $scope.data.templates = []; }); }; function init() { loadTemplates(); } init(); };