permissions-select.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const app = require('../app')
  2. const { Set } = require('immutable')
  3. app.component('appPermissionsSelect', {
  4. template: html`
  5. <md-list>
  6. <md-subheader>Permissions</md-subheader>
  7. <md-list-item ng-repeat="permission in ::$ctrl.permissions track by permission.key" md-long-text>
  8. <md-checkbox ng-model="message.selected" ng-checked="$ctrl.set.has(permission.key)" ng-click="$ctrl.toggle(permission.key)"></md-checkbox>
  9. <div class="md-list-item-text">
  10. <h3>{{permission.key}}</h3>
  11. <p>{{permission.description}}</p>
  12. </div>
  13. </md-list-item>
  14. </md-list>
  15. `,
  16. bindings: {
  17. ngModel: '='
  18. },
  19. controller: function(api, $scope) {
  20. api.get('/api/auth/permissions').then(x => {
  21. this.permissions = x
  22. })
  23. const modelWatcher = $scope.$watch('$ctrl.ngModel', (model) => {
  24. if (model) {
  25. this.set = new Set(model.split(','))
  26. } else {
  27. this.set = new Set()
  28. }
  29. })
  30. $scope.$on('$destroy', () => modelWatcher())
  31. this.toggle = permission => {
  32. if (this.set.has(permission)) {
  33. this.set = this.set.remove(permission)
  34. } else {
  35. this.set = this.set.add(permission)
  36. }
  37. this.ngModel = this.set.join(',')
  38. }
  39. }
  40. })