author | ymh <ymh.work@gmail.com> |
Sun, 06 Mar 2016 00:29:01 +0100 | |
changeset 141 | c0e8626a271c |
parent 137 | 1baa7c6bd370 |
child 158 | 366509ae2f37 |
permissions | -rw-r--r-- |
4 | 1 |
import DS from 'ember-data'; |
20 | 2 |
import Ember from 'ember'; |
3 |
import _ from 'lodash/lodash'; |
|
137 | 4 |
import * as utils from 'corpus-common-addon/utils/utils'; |
4 | 5 |
|
6 |
export default DS.Model.extend({ |
|
32 | 7 |
//id: attr('string'), |
4 | 8 |
uri: DS.attr('string'), |
20 | 9 |
|
4 | 10 |
title: DS.attr('string'), |
20 | 11 |
|
28 | 12 |
language: DS.attr('string'), |
13 |
||
32 | 14 |
publishers: DS.attr({defaultValue: function() { return []; }}), |
20 | 15 |
|
32 | 16 |
contributors: DS.attr({defaultValue: function() { return []; }}), |
20 | 17 |
|
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
|
18 |
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
|
19 |
|
32 | 20 |
mediaArray: DS.attr({defaultValue: function() { return []; }}), |
20 | 21 |
|
22 |
mediaList: Ember.computed('mediaArray', function() { |
|
23 |
var res = []; |
|
24 |
var mp3 = null; |
|
25 |
_.forEach(this.get('mediaArray'), function(m) { |
|
26 |
if(m.format === 'audio/mpeg') { |
|
27 |
mp3 = m; |
|
28 |
} else if (m.format.startsWith('audio/')) { |
|
29 |
res.push(m); |
|
30 |
} |
|
31 |
}); |
|
32 |
if(mp3) { |
|
33 |
res.unshift(mp3); |
|
34 |
} |
|
35 |
return res; |
|
36 |
}), |
|
28 | 37 |
|
38 |
addContributor: function(contrib_def) { |
|
39 |
var contributors = this.get('contributors'); |
|
40 |
if(_.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);}) < 0) { |
|
41 |
contributors.pushObject(contrib_def); |
|
42 |
} |
|
43 |
// must set dirty |
|
44 |
this.set('contributors', _.clone(this.get('contributors'))); |
|
45 |
}, |
|
46 |
removeContributor: function(contrib_def) { |
|
47 |
var contributors = this.get('contributors'); |
|
48 |
var index = _.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);}); |
|
49 |
if(index >= 0) { |
|
50 |
contributors.removeAt(index); |
|
51 |
} |
|
52 |
//must set dirty |
|
53 |
this.set('contributors', _.clone(this.get('contributors'))); |
|
54 |
}, |
|
55 |
saveContributor: function(contrib_def, index) { |
|
56 |
var contributors = this.get('contributors'); |
|
57 |
if(index < 0 || index >= contributors.length) { |
|
58 |
return; |
|
59 |
} |
|
60 |
||
61 |
var contrib_def_map = {name: contrib_def.name||"", url: contrib_def.url||"", role: contrib_def.role||""}; |
|
62 |
var existingContribs = _.filter(contributors, function(c, i) { |
|
63 |
return (i!==index && _.isEqual({name: c.name||"", url: c.url||"", role: c.role||""}, contrib_def_map)); |
|
64 |
}); |
|
65 |
if(existingContribs.length > 0) { |
|
66 |
// contributor exists, remove contributor @ index |
|
67 |
contributors.removeAt(index); |
|
68 |
} |
|
69 |
else { |
|
70 |
contributors[index] = contrib_def; |
|
71 |
} |
|
72 |
//must set dirty only if needed |
|
73 |
this.set('contributors', _.clone(this.get('contributors'))); |
|
137 | 74 |
}, |
75 |
removeSubject: function(subject_def) { |
|
76 |
var subjects = this.get('subjects'); |
|
77 |
var index = _.findIndex(subjects, function(s) { return _.isEqual(s, subject_def);}); |
|
78 |
if(index >= 0) { |
|
79 |
subjects.removeAt(index); |
|
80 |
} |
|
81 |
//must set dirty |
|
82 |
this.set('subjects', _.clone(subjects)); |
|
83 |
}, |
|
84 |
addSubject: function(subject_def) { |
|
85 |
var subjects = this.get('subjects'); |
|
86 |
if(_.findIndex(subjects, function(s) { return _.isEqual(s, subject_def) || _.isEqual(s, utils.switchArkBnfLink(subject_def));}) < 0) { |
|
87 |
subjects.pushObject(subject_def); |
|
88 |
} |
|
89 |
// must set dirty |
|
90 |
this.set('subjects', _.clone(subjects)); |
|
91 |
}, |
|
28 | 92 |
|
4 | 93 |
}); |