view annotator_files/lib/plugin/annotateitpermissions.js @ 3:6356e78ccf5c

new version contains Annotator JS files to be used with FilesystemSite.
author casties
date Thu, 05 Apr 2012 19:37:27 +0200
parents
children 6979313586cf
line wrap: on
line source

var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  __hasProp = Object.prototype.hasOwnProperty,
  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; },
  __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

Annotator.Plugin.AnnotateItPermissions = (function(_super) {

  __extends(AnnotateItPermissions, _super);

  AnnotateItPermissions.prototype.options = {
    showViewPermissionsCheckbox: true,
    showEditPermissionsCheckbox: true,
    groups: {
      world: 'group:__world__',
      authenticated: 'group:__authenticated__',
      consumer: 'group:__consumer__'
    },
    userString: function(user) {
      return user;
    },
    userAuthorize: function(action, annotation, user, consumer) {
      var action_field, permissions, _ref, _ref2, _ref3, _ref4;
      permissions = annotation.permissions || {};
      action_field = permissions[action] || [];
      if (!(user && consumer)) {
        return _ref = this.groups.world, __indexOf.call(action_field, _ref) >= 0;
      } else {
        if (_ref2 = this.groups.world, __indexOf.call(action_field, _ref2) >= 0) {
          return true;
        } else if (user === annotation.user && consumer === annotation.consumer) {
          return true;
        } else if (_ref3 = this.groups.authenticated, __indexOf.call(action_field, _ref3) >= 0) {
          return true;
        } else if (consumer === annotation.consumer && (_ref4 = this.groups.consumer, __indexOf.call(action_field, _ref4) >= 0)) {
          return true;
        } else if (consumer === annotation.consumer && __indexOf.call(action_field, user) >= 0) {
          return true;
        } else {
          return false;
        }
      }
    },
    user: '',
    consumer: 'annotateit',
    permissions: {
      'read': ['group:__world__'],
      'update': [],
      'delete': [],
      'admin': []
    }
  };

  function AnnotateItPermissions(element, options) {
    this._setAuthFromToken = __bind(this._setAuthFromToken, this);
    this.updateAnnotationPermissions = __bind(this.updateAnnotationPermissions, this);
    this.updatePermissionsField = __bind(this.updatePermissionsField, this);
    this.addFieldsToAnnotation = __bind(this.addFieldsToAnnotation, this);    AnnotateItPermissions.__super__.constructor.apply(this, arguments);
    if (this.options.consumer) {
      this.setConsumer(this.options.consumer);
      delete this.options.consumer;
    }
  }

  AnnotateItPermissions.prototype.setConsumer = function(consumer) {
    return this.consumer = consumer;
  };

  AnnotateItPermissions.prototype.authorize = function(action, annotation, user, consumer) {
    if (user === void 0) user = this.user;
    if (consumer === void 0) consumer = this.consumer;
    if (this.options.userAuthorize) {
      return this.options.userAuthorize.call(this.options, action, annotation, user, consumer);
    } else {
      return true;
    }
  };

  AnnotateItPermissions.prototype.addFieldsToAnnotation = function(annotation) {
    AnnotateItPermissions.__super__.addFieldsToAnnotation.apply(this, arguments);
    if (annotation && this.consumer) return annotation.consumer = this.consumer;
  };

  AnnotateItPermissions.prototype.updatePermissionsField = function(action, field, annotation) {
    var input;
    field = $(field).show();
    input = field.find('input').removeAttr('disabled');
    if (!this.authorize('admin', annotation)) field.hide();
    if (this.authorize(action, annotation || {}, '__nonexistentuser__')) {
      return input.attr('checked', 'checked');
    } else {
      return input.removeAttr('checked');
    }
  };

  AnnotateItPermissions.prototype.updateAnnotationPermissions = function(type, field, annotation) {
    var dataKey;
    if (!annotation.permissions) annotation.permissions = this.options.permissions;
    dataKey = type + '-permissions';
    if ($(field).find('input').is(':checked')) {
      return annotation.permissions[type] = [type === 'read' ? this.options.groups.world : this.options.groups.consumer];
    } else {
      return annotation.permissions[type] = [];
    }
  };

  AnnotateItPermissions.prototype._setAuthFromToken = function(token) {
    AnnotateItPermissions.__super__._setAuthFromToken.apply(this, arguments);
    return this.setConsumer(token.consumerKey);
  };

  return AnnotateItPermissions;

})(Annotator.Plugin.Permissions);