Sha256: 47c374b6332948b74f9b6e39c50118e6eff779dda8872652efa87de2b9e13cb7

Contents?: true

Size: 1.95 KB

Versions: 17

Compression:

Stored size: 1.95 KB

Contents

pageflow.FileTypes = pageflow.Object.extend({
  initialize: function() {
    this.clientSideConfigs = [];
  },

  register: function(name, config) {
    if (this._setup) {
      throw 'File types already set up. Register file types before initializers run.';
    }

    this.clientSideConfigs[name] = config;
  },

  setup: function(serverSideConfigs) {
    var clientSideConfigs = this.clientSideConfigs;
    this._setup = true;

    this.fileTypes = _.map(serverSideConfigs, function(serverSideConfig) {
      var clientSideConfig = clientSideConfigs[serverSideConfig.collectionName];

      if (!clientSideConfig) {
        throw 'Missing client side config for file type "' + serverSideConfig.collectionName + '"';
      }

      return new pageflow.FileType(_.extend({}, serverSideConfig, clientSideConfig));
    });
  },

  findByUpload: function(upload) {
    var result = this.find(function(fileType) {
      return fileType.matchUpload(upload);
    });

    if (!result) {
      throw(new pageflow.FileTypes.UnmatchedUploadError(upload));
    }

    return result;
  },

  findByCollectionName: function(collectionName) {
    var result = this.find(function(fileType) {
      return fileType.collectionName === collectionName;
    });

    if (!result) {
      throw('Could not find file type by collection name "' + collectionName +'"');
    }

    return result;
  }
});

_.each(['each', 'map', 'reduce', 'first', 'find'], function(method) {
  pageflow.FileTypes.prototype[method] = function() {
    if (!this._setup) {
      throw  'File types are not yet set up.';
    }

    var args = Array.prototype.slice.call(arguments);
    args.unshift(this.fileTypes);
    return _[method].apply(_, args);
  };
});

pageflow.FileTypes.UnmatchedUploadError = pageflow.Object.extend({
  name: 'UnmatchedUploadError',

  initialize: function(upload) {
    this.upload = upload;
    this.message = 'No matching file type found for upload "' + upload.name + '" of type "' + upload.type +'".';
  }
});

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
pageflow-0.11.4 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.11.3 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.11.2 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.11.1 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.11.0 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.10.0 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.9.2 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.9.1 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.9.0 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.8.2 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.8.1 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.8.0 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.7.2 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.7.1 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.7.0 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.6.0 app/assets/javascripts/pageflow/editor/api/file_types.js
pageflow-0.5.0 app/assets/javascripts/pageflow/editor/api/file_types.js