4
|
1 |
import DS from 'ember-data'; |
20
|
2 |
import Ember from 'ember'; |
|
3 |
import _ from 'lodash/lodash'; |
4
|
4 |
|
|
5 |
export default DS.Model.extend({ |
|
6 |
//id: DS.attr('string'), |
|
7 |
uri: DS.attr('string'), |
20
|
8 |
|
4
|
9 |
title: DS.attr('string'), |
20
|
10 |
|
28
|
11 |
language: DS.attr('string'), |
|
12 |
|
4
|
13 |
publishers: DS.attr({defaultValue: []}), |
20
|
14 |
|
|
15 |
contributors: DS.attr({defaultValue: []}), |
|
16 |
|
|
17 |
mediaArray: DS.attr({defaultValue: []}), |
|
18 |
|
|
19 |
mediaList: Ember.computed('mediaArray', function() { |
|
20 |
var res = []; |
|
21 |
var mp3 = null; |
|
22 |
_.forEach(this.get('mediaArray'), function(m) { |
|
23 |
if(m.format === 'audio/mpeg') { |
|
24 |
mp3 = m; |
|
25 |
} else if (m.format.startsWith('audio/')) { |
|
26 |
res.push(m); |
|
27 |
} |
|
28 |
}); |
|
29 |
if(mp3) { |
|
30 |
res.unshift(mp3); |
|
31 |
} |
|
32 |
return res; |
|
33 |
}), |
28
|
34 |
|
|
35 |
addContributor: function(contrib_def) { |
|
36 |
var contributors = this.get('contributors'); |
|
37 |
if(_.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);}) < 0) { |
|
38 |
contributors.pushObject(contrib_def); |
|
39 |
} |
|
40 |
// must set dirty |
|
41 |
this.set('contributors', _.clone(this.get('contributors'))); |
|
42 |
}, |
|
43 |
removeContributor: function(contrib_def) { |
|
44 |
var contributors = this.get('contributors'); |
|
45 |
var index = _.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);}); |
|
46 |
if(index >= 0) { |
|
47 |
contributors.removeAt(index); |
|
48 |
} |
|
49 |
//must set dirty |
|
50 |
this.set('contributors', _.clone(this.get('contributors'))); |
|
51 |
}, |
|
52 |
saveContributor: function(contrib_def, index) { |
|
53 |
var contributors = this.get('contributors'); |
|
54 |
if(index < 0 || index >= contributors.length) { |
|
55 |
return; |
|
56 |
} |
|
57 |
|
|
58 |
var contrib_def_map = {name: contrib_def.name||"", url: contrib_def.url||"", role: contrib_def.role||""}; |
|
59 |
var existingContribs = _.filter(contributors, function(c, i) { |
|
60 |
return (i!==index && _.isEqual({name: c.name||"", url: c.url||"", role: c.role||""}, contrib_def_map)); |
|
61 |
}); |
|
62 |
if(existingContribs.length > 0) { |
|
63 |
// contributor exists, remove contributor @ index |
|
64 |
contributors.removeAt(index); |
|
65 |
} |
|
66 |
else { |
|
67 |
contributors[index] = contrib_def; |
|
68 |
} |
|
69 |
//must set dirty only if needed |
|
70 |
this.set('contributors', _.clone(this.get('contributors'))); |
|
71 |
} |
|
72 |
|
4
|
73 |
}); |