server/bo_client/app/models/document.js
author ymh <ymh.work@gmail.com>
Mon, 14 Nov 2016 16:27:55 +0100
changeset 419 a37e8629a95c
parent 327 13564bb13ccc
child 528 aa4fc985bf64
permissions -rw-r--r--
Added tag 0.0.11 for changeset 6eb84397be15
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import DS from 'ember-data';
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
     2
import Ember from 'ember';
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
     3
import _ from 'lodash/lodash';
137
1baa7c6bd370 add subject edition
ymh <ymh.work@gmail.com>
parents: 130
diff changeset
     4
import * as utils from 'corpus-common-addon/utils/utils';
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
export default DS.Model.extend({
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     7
    //id: attr('string'),
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     8
    uri: DS.attr('string'),
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
     9
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    10
    title: DS.attr('string'),
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    11
327
13564bb13ccc In the serialized document the lan files id 'languages' and not 'language'
ymh <ymh.work@gmail.com>
parents: 326
diff changeset
    12
    languages: DS.attr({defaultValue: function() { return []; }}),
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    13
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    14
    publishers: DS.attr({defaultValue: function() { return []; }}),
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    15
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    16
    contributors: DS.attr({defaultValue: function() { return []; }}),
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    17
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    18
    subjects: DS.attr({defaultValue: function() { return []; }}),
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    19
304
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    20
    geoInfo: DS.attr({defaultValue: function() { return []; }}),
277
bd4bc1db4f40 add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    21
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    22
    mediaArray: DS.attr({defaultValue: function() { return []; }}),
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    23
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    24
    encodedId: Ember.computed('id', function() {
277
bd4bc1db4f40 add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
    25
        return encodeURIComponent(this.get('id'));
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    26
    }),
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
    27
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    28
    mediaList: Ember.computed('mediaArray', function() {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    29
        var res = [];
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    30
        var mp3 = null;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    31
        _.forEach(this.get('mediaArray'), function(m) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    32
            if(m.format === 'audio/mpeg') {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    33
                mp3 = m;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    34
            } else if (m.format.startsWith('audio/')) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    35
                res.push(m);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    36
            }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    37
        });
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    38
        if(mp3) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    39
            res.unshift(mp3);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    40
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    41
        return res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    42
    }),
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    43
304
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    44
    reflocs: Ember.computed('geoInfo', function() {
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    45
        return this.get('geoInfo')['ref-locs'];
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    46
    }),
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    47
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    48
    addContributor: function(contrib_def) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    49
        var contributors = this.get('contributors');
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    50
        if(_.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);}) < 0) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    51
            contributors.pushObject(contrib_def);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    52
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    53
        // must set dirty
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    54
        this.set('contributors', _.clone(this.get('contributors')));
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    55
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    56
    removeContributor: function(contrib_def) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    57
        var contributors = this.get('contributors');
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    58
        var index = _.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);});
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    59
        if(index >= 0) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    60
            contributors.removeAt(index);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    61
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    62
        //must set dirty
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    63
        this.set('contributors', _.clone(this.get('contributors')));
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    64
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    65
    saveContributor: function(contrib_def, index) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    66
        var contributors = this.get('contributors');
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    67
        if(index < 0 || index >= contributors.length) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    68
            return;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    69
        }
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    70
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    71
        var contrib_def_map = {name: contrib_def.name||'', url: contrib_def.url||'', role: contrib_def.role||''};
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    72
        var existingContribs = _.filter(contributors, function(c, i) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    73
            return (i!==index && _.isEqual({name: c.name||'', url: c.url||'', role: c.role||''}, contrib_def_map));
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    74
        });
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    75
        if(existingContribs.length > 0) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    76
            // contributor exists, remove contributor @ index
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    77
            contributors.removeAt(index);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    78
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    79
        else {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    80
            contributors[index] = contrib_def;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    81
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    82
        //must set dirty only if needed
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    83
        this.set('contributors', _.clone(this.get('contributors')));
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    84
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    85
    removeSubject: function(subject_def) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    86
        var subjects = this.get('subjects');
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    87
        var index = _.findIndex(subjects, function(s) { return _.isEqual(s, subject_def);});
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    88
        if(index >= 0) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    89
            subjects.removeAt(index);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    90
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    91
        //must set dirty
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    92
        this.set('subjects', _.clone(subjects));
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    93
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    94
    addSubject: function(subject_def) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    95
        var subjects = this.get('subjects');
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    96
        if(_.findIndex(subjects, function(s) { return _.isEqual(s, subject_def) || _.isEqual(s, utils.switchArkBnfLink(subject_def));}) < 0) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    97
            subjects.pushObject(subject_def);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    98
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    99
        // must set dirty
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
   100
        this.set('subjects', _.clone(subjects));
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   101
    }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   102
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
});