server/bo_client/app/models/document.js
author ymh <ymh.work@gmail.com>
Sun, 28 Feb 2016 09:30:23 +0100
changeset 134 c06d08c8a1b8
parent 130 fac22d8c2df8
child 137 1baa7c6bd370
permissions -rw-r--r--
add bnf resolver in common addon + applications
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';
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
export default DS.Model.extend({
32
9765cf7cf817 upgrade to ember-cli 2.2(beta)
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
     6
  //id: attr('string'),
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
  uri: DS.attr('string'),
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
     8
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
  title: DS.attr('string'),
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    10
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    11
  language: DS.attr('string'),
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    12
32
9765cf7cf817 upgrade to ember-cli 2.2(beta)
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    13
  publishers: DS.attr({defaultValue: function() { return []; }}),
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    14
32
9765cf7cf817 upgrade to ember-cli 2.2(beta)
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    15
  contributors: DS.attr({defaultValue: function() { return []; }}),
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    16
130
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    17
  subjects: DS.attr({defaultValue: function() { return []; }}),
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    18
32
9765cf7cf817 upgrade to ember-cli 2.2(beta)
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    19
  mediaArray: DS.attr({defaultValue: function() { return []; }}),
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    21
  mediaList: Ember.computed('mediaArray', function() {
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    22
    var res = [];
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    23
    var mp3 = null;
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    24
    _.forEach(this.get('mediaArray'), function(m) {
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    25
      if(m.format === 'audio/mpeg') {
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    26
        mp3 = m;
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    27
      } else if (m.format.startsWith('audio/')) {
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    28
        res.push(m);
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    29
      }
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    30
    });
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    31
    if(mp3) {
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    32
      res.unshift(mp3);
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    33
    }
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    34
    return res;
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    35
  }),
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    36
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    37
  addContributor: function(contrib_def) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    38
    var contributors = this.get('contributors');
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    39
    if(_.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);}) < 0) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    40
      contributors.pushObject(contrib_def);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    41
    }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    42
    // must set dirty
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    43
    this.set('contributors', _.clone(this.get('contributors')));
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    44
  },
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    45
  removeContributor: function(contrib_def) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    46
    var contributors = this.get('contributors');
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    47
    var index = _.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);});
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    48
    if(index >= 0) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    49
      contributors.removeAt(index);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    50
    }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    51
    //must set dirty
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    52
    this.set('contributors', _.clone(this.get('contributors')));
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    53
  },
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    54
  saveContributor: function(contrib_def, index) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    55
    var contributors = this.get('contributors');
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    56
    if(index < 0 || index >= contributors.length) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    57
      return;
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    58
    }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    59
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    60
    var contrib_def_map = {name: contrib_def.name||"", url: contrib_def.url||"", role: contrib_def.role||""};
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    61
    var existingContribs = _.filter(contributors, function(c, i) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    62
      return (i!==index && _.isEqual({name: c.name||"", url: c.url||"", role: c.role||""}, contrib_def_map));
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    63
    });
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    64
    if(existingContribs.length > 0) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    65
      // contributor exists, remove contributor @ index
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    66
      contributors.removeAt(index);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    67
    }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    68
    else {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    69
      contributors[index] = contrib_def;
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    70
    }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    71
    //must set dirty only if needed
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    72
    this.set('contributors', _.clone(this.get('contributors')));
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    73
  }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
    74
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
});