UNPKG

sheercms

Version:

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

76 lines (68 loc) 2.9 kB
app.directive('dropdownField', function ($parse, $modal, HelperService, RoleService, GroupService) { return { restrict: 'E', scope: { value: "=" }, template: '<select class="dropdown-field form-control" ng-model="value" ng-options="a.value as a.text group by a.group for a in options"></select><span class="field-error">{{error}}</span>', link: function(scope, element, attrs) { scope.source = attrs["source"]; scope.options = []; scope.error = ""; function init() { //load the options from the source parseSource(function(err, type, listItems) { if (err) scope.error = err; scope.options = listItems; }); } /* callback: (err, type, listItems) */ function parseSource(callback) { var p = scope.source; if (p === "roles") { RoleService.getRoles(true).then(function(result) { if (result && result.success === true && result.roles) { var list = []; for(var i = 0; i < result.roles.length; i++) { var r = result.roles[i].name; list.push({value: r, text: r}); } callback(null, "roles", list); } else callback("Error loading roles into dropdown."); }); } else if (p === "groups") { GroupService.list("content,media", null).then(function(result) { if (result && result.success === true) { var list = []; for(var i = 0; i < result.groups.length; i++) { var g = result.groups[i]; list.push({value: g._id, text: g.name, group: g.branch}); } callback(null, "groups", list); } else { callback("Error loading groups into dropdown."); } }); } else { var arr = HelperService.splitTrim(p, ','); var list = []; if (arr) { for(var i = 0; i < arr.length; i++) { list.push({value: arr[i], text: arr[i]}); } } callback(null, "list", list); } } init(); } }; });