author | ymh <ymh.work@gmail.com> |
Fri, 08 Sep 2017 12:04:06 +0200 | |
changeset 541 | e756a8c72c3d |
parent 528 | aa4fc985bf64 |
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({ |
|
528 | 7 |
// id: attr('string'), |
8 |
uri: DS.attr('string'), |
|
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
137
diff
changeset
|
9 |
|
528 | 10 |
title: DS.attr('string'), |
20 | 11 |
|
528 | 12 |
languages: DS.attr({ defaultValue: function() { return []; } }), |
20 | 13 |
|
528 | 14 |
publishers: DS.attr({ defaultValue: function() { return []; } }), |
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
137
diff
changeset
|
15 |
|
528 | 16 |
contributors: DS.attr({ defaultValue: function() { return []; } }), |
28 | 17 |
|
528 | 18 |
subjects: DS.attr({ defaultValue: function() { return []; } }), |
19 |
||
20 |
geoInfo: DS.attr({ defaultValue: function() { return []; } }), |
|
21 |
||
22 |
mediaArray: DS.attr({ defaultValue: function() { return []; } }), |
|
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
|
23 |
|
528 | 24 |
encodedId: Ember.computed('id', function() { |
25 |
return encodeURIComponent(this.get('id')); |
|
26 |
}), |
|
20 | 27 |
|
528 | 28 |
mediaList: Ember.computed('mediaArray', function() { |
29 |
const res = []; |
|
30 |
let mp3 = null; |
|
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
277
diff
changeset
|
31 |
|
528 | 32 |
_.forEach(this.get('mediaArray'), function(m) { |
33 |
if (m.format === 'audio/mpeg') { |
|
34 |
mp3 = m; |
|
35 |
} else if (m.format.startsWith('audio/')) { |
|
36 |
res.push(m); |
|
37 |
} |
|
38 |
}); |
|
39 |
if (mp3) { |
|
40 |
res.unshift(mp3); |
|
41 |
} |
|
28 | 42 |
|
528 | 43 |
return res; |
44 |
}), |
|
45 |
||
46 |
reflocs: Ember.computed('geoInfo', function() { |
|
47 |
return this.get('geoInfo')['ref-locs']; |
|
48 |
}), |
|
49 |
||
50 |
addContributor: function(contribDef) { |
|
51 |
const contributors = this.get('contributors'); |
|
52 |
||
53 |
if (_.findIndex(contributors, function(c) { return _.isEqual(c, contribDef); }) < 0) { |
|
54 |
contributors.pushObject(contribDef); |
|
55 |
||
28 | 56 |
} |
57 |
||
528 | 58 |
// must set dirty |
59 |
this.set('contributors', _.clone(this.get('contributors'))); |
|
60 |
}, |
|
61 |
removeContributor: function(contribDef) { |
|
62 |
const contributors = this.get('contributors'); |
|
63 |
const index = _.findIndex(contributors, function(c) { return _.isEqual(c, contribDef); }); |
|
64 |
||
65 |
if (index >= 0) { |
|
66 |
||
67 |
contributors.removeAt(index); |
|
68 |
} |
|
69 |
// must set dirty |
|
70 |
this.set('contributors', _.clone(this.get('contributors'))); |
|
71 |
}, |
|
72 |
saveContributor: function(contribDef, index) { |
|
73 |
const contributors = this.get('contributors'); |
|
74 |
||
75 |
if (index < 0 || index >= contributors.length) { |
|
76 |
return; |
|
77 |
} |
|
78 |
||
79 |
const contribDefMap = { name: contribDef.name || '', url: contribDef.url || '', role: contribDef.role || '' }; |
|
80 |
const existingContribs = _.filter(contributors, function(c, i) { |
|
81 |
||
82 |
return (i !== index && _.isEqual({ name: c.name || '', url: c.url || '', role: c.role || '' }, contribDefMap)); |
|
83 |
}); |
|
84 |
||
85 |
if (existingContribs.length > 0) { |
|
86 |
// contributor exists, remove contributor @ index |
|
87 |
contributors.removeAt(index); |
|
88 |
} else { |
|
89 |
contributors[index] = contribDef; |
|
90 |
} |
|
91 |
||
92 |
// must set dirty only if needed |
|
93 |
this.set('contributors', _.clone(this.get('contributors'))); |
|
94 |
}, |
|
95 |
removeSubject: function(subjectDef) { |
|
96 |
const subjects = this.get('subjects'); |
|
97 |
const index = _.findIndex(subjects, function(s) { return _.isEqual(s, subjectDef); }); |
|
98 |
||
99 |
if (index >= 0) { |
|
100 |
subjects.removeAt(index); |
|
101 |
} |
|
102 |
// must set dirty |
|
103 |
this.set('subjects', _.clone(subjects)); |
|
104 |
}, |
|
105 |
addSubject: function(subjectDef) { |
|
106 |
const subjects = this.get('subjects'); |
|
107 |
||
108 |
if (_.findIndex(subjects, function(s) { return _.isEqual(s, subjectDef) || _.isEqual(s, utils.switchArkBnfLink(subjectDef)); }) < 0) { |
|
109 |
subjects.pushObject(subjectDef); |
|
110 |
} |
|
111 |
// must set dirty |
|
112 |
this.set('subjects', _.clone(subjects)); |
|
113 |
} |
|
114 |
||
4 | 115 |
}); |