From d7579b052bd8f75d1594da1bdc85b15fb9685dfc Mon Sep 17 00:00:00 2001 From: Juanfran Date: Fri, 13 Mar 2015 08:42:08 +0100 Subject: [PATCH] tgCheckPermission directive tg-class-permission="{'xx': 'modify_us'}" the directive add 'xx' if the user has the modify_us permisssion --- app/coffee/modules/common.coffee | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/app/coffee/modules/common.coffee b/app/coffee/modules/common.coffee index bb11a405..15514453 100644 --- a/app/coffee/modules/common.coffee +++ b/app/coffee/modules/common.coffee @@ -60,6 +60,42 @@ CheckPermissionDirective = -> module.directive("tgCheckPermission", CheckPermissionDirective) +############################################################################# +## Add class based on permissions +############################################################################# + +ClassPermissionDirective = -> + name = "tgClassPermission" + + link = ($scope, $el, $attrs) -> + checkPermissions = (project, className, permission) -> + negation = permission[0] == "!" + + permission = permission.slice(1) if negation + + if negation && project.my_permissions.indexOf(permission) == -1 + $el.addClass(className) + else if project.my_permissions.indexOf(permission) != -1 + $el.addClass(className) + else + $el.removeClass(className) + + tgClassPermissionWatchAction = (project) -> + if project + unbindWatcher() + + classes = $scope.$eval($attrs[name]) + + for className, permission of classes + checkPermissions(project, className, permission) + + + unbindWatcher = $scope.$watch "project", tgClassPermissionWatchAction + + return {link:link} + +module.directive("tgClassPermission", ClassPermissionDirective) + ############################################################################# ## Animation frame service, apply css changes in the next render frame #############################################################################