server/bo_client/app/models/document.js
author Chloe Laisne <chloe.laisne@gmail.com>
Thu, 07 Jul 2016 11:06:11 +0200
changeset 233 0ff47a9c5da2
parent 168 17f10b56c079
child 277 bd4bc1db4f40
permissions -rw-r--r--
Keyboard events on chronology
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
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    12
    language: DS.attr('string'),
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
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    20
    mediaArray: DS.attr({defaultValue: function() { return []; }}),
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    21
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
    22
    encodedId: Ember.computed('id', function() {
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
    23
      return encodeURIComponent(this.get('id'));
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
    }),
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
    25
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    26
    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
    27
        var res = [];
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    28
        var mp3 = null;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    29
        _.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
    30
            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
    31
                mp3 = m;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    32
            } 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
    33
                res.push(m);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    34
            }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    35
        });
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    36
        if(mp3) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    37
            res.unshift(mp3);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    38
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    39
        return res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    40
    }),
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    41
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    42
    addContributor: function(contrib_def) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    43
        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
    44
        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
    45
            contributors.pushObject(contrib_def);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    46
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    47
        // must set dirty
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    48
        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
    49
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    50
    removeContributor: function(contrib_def) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    51
        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
    52
        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
    53
        if(index >= 0) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    54
            contributors.removeAt(index);
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
        //must set dirty
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    57
        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
    58
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    59
    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
    60
        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
    61
        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
    62
            return;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    63
        }
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    64
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    65
        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
    66
        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
    67
            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
    68
        });
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    69
        if(existingContribs.length > 0) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    70
            // 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
    71
            contributors.removeAt(index);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    72
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    73
        else {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    74
            contributors[index] = contrib_def;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    75
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    76
        //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
    77
        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
    78
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    79
    removeSubject: function(subject_def) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    80
        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
    81
        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
    82
        if(index >= 0) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    83
            subjects.removeAt(index);
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
        //must set dirty
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    86
        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
    87
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    88
    addSubject: function(subject_def) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    89
        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
    90
        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
    91
            subjects.pushObject(subject_def);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    92
        }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    93
        // must set dirty
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    94
        this.set('subjects', _.clone(subjects));
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    95
    }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    96
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
});