annotate src/DML/MainVisBundle/Resources/assets/marionette/modules/DataModule/DataModule.32-DynamicDefinitionProviderForCollections.js @ 1:f38015048f48 tip

Added GPL
author Daniel Wolff
date Sat, 13 Feb 2016 20:43:38 +0100
parents 493bcb69166c
children
rev   line source
Daniel@0 1 "use strict";
Daniel@0 2
Daniel@0 3 App.module("DataModule", function(DataModule, App, Backbone, Marionette, $, _, Logger) {
Daniel@0 4 // Prevent auto start
Daniel@0 5 //CollectionDefinitionProvider.startWithParent = false;
Daniel@0 6
Daniel@0 7 // Define private variables
Daniel@0 8 var logger = null;
Daniel@0 9
Daniel@0 10 /**
Daniel@0 11 * Module initializer
Daniel@0 12 *
Daniel@0 13 */
Daniel@0 14 DataModule.addInitializer(function(){
Daniel@0 15
Daniel@0 16 logger = Logger.get("App.DataModule");
Daniel@0 17
Daniel@0 18 DataModule.DynamicDefinitionForCollection = DataModule.DynamicDefinition.extend({});
Daniel@0 19 DataModule.DynamicDefinitionProviderForCollections = DataModule.DynamicDefinitionProvider.extend({
Daniel@0 20
Daniel@0 21 options: {
Daniel@0 22 DynamicDefinition: DataModule.DynamicDefinitionForCollection,
Daniel@0 23 apiMethod: "getCollectionId",
Daniel@0 24 customAttributesWhenNotApplicable: {id: undefined},
Daniel@0 25 customAttributesWhileBeingUpdated: {id: null, sampleSize: null, fullSize: null},
Daniel@0 26 customAttributesWhenFaulty: {id: false, sampleSize: false, fullSize: false},
Daniel@0 27
Daniel@0 28 customAttributesWhenEmpty: {id: "", sampleSize: 0, fullSize: 0}
Daniel@0 29 },
Daniel@0 30
Daniel@0 31 definitionIsFaulty: function(definition) {
Daniel@0 32 return definition.id === false;
Daniel@0 33 },
Daniel@0 34
Daniel@0 35 definitionIsBeingUpdated: function(definition) {
Daniel@0 36 return definition.id === null;
Daniel@0 37 },
Daniel@0 38
Daniel@0 39 configParametersToRequestParameters: function(config) {
Daniel@0 40 var rawConfigParameters = config.attributes.parameters.attributes;
Daniel@0 41 var requestParameters = {};
Daniel@0 42 var library = _.str.trim(rawConfigParameters.library).replace(/ /g, "").replace(/,/g, ";");
Daniel@0 43 if (library) {
Daniel@0 44 requestParameters.library = library;
Daniel@0 45 } else {
Daniel@0 46 return null;
Daniel@0 47 }
Daniel@0 48 requestParameters.sv = 0;
Daniel@0 49 var year = _.str.trim(rawConfigParameters.year).replace(/ /g, "");
Daniel@0 50 if (year) {
Daniel@0 51 requestParameters.year = year;
Daniel@0 52 }
Daniel@0 53
Daniel@0 54 //FIXME (hard-coded sample size)
Daniel@0 55 requestParameters.sample = 1000;
Daniel@0 56
Daniel@0 57 _.each(["genre", "composer", "performer", "title", "place", "collection"], function(field) {
Daniel@0 58 var f = _.str.trim(rawConfigParameters[field]);
Daniel@0 59 if (f) {
Daniel@0 60 requestParameters[field] = f;
Daniel@0 61 };
Daniel@0 62 });
Daniel@0 63
Daniel@0 64 return requestParameters;
Daniel@0 65 },
Daniel@0 66
Daniel@0 67 apiResponseToAttributes: function(data, query) {
Daniel@0 68 var attributes = {};
Daniel@0 69
Daniel@0 70 if (data && data.cid) {
Daniel@0 71 attributes.id = data.cid;
Daniel@0 72 attributes.sampleSize = data.size;
Daniel@0 73 attributes.fullSize = data.full_size;
Daniel@0 74 } else if (data.errors && data.errors[0] && _.isString(data.errors[0].desc) && data.errors[0].desc.indexOf("empty_dataset") > 0) {
Daniel@0 75 attributes = _.clone(this.options.customAttributesWhenEmpty);
Daniel@0 76 } else {
Daniel@0 77 attributes = _.clone(this.options.customAttributesWhenFaulty);
Daniel@0 78 attributes.errors = data.errors;
Daniel@0 79 logger.warn("An error occured when attempting to obtain collection definition. Query / response:", query, data);
Daniel@0 80 };
Daniel@0 81 attributes.query = query;
Daniel@0 82 return attributes;
Daniel@0 83 }
Daniel@0 84 });
Daniel@0 85 });
Daniel@0 86 }, Logger);