-
Notifications
You must be signed in to change notification settings - Fork 6
/
view.js
200 lines (174 loc) · 5.3 KB
/
view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Module dependencies.
*/
var countries = require('countries');
var licenses = require('licenses');
var language = require('language');
var Panel = require('panel');
var Ractive = require('ractive');
var RelatedLinks = require('related-links');
var request = require('request');
var render = require('render');
var t = require('t');
var apps = require('applications').all;
var approved = require('applications').approved;
var template = require('./template');
var scroll = require('scroll');
var tags = require('tags');
var technologies = require('technologies');
var AdminAppForm = Ractive.extend({
isolated: true,
template: render(template),
components: {
relatedlinks: RelatedLinks
},
hastag: function (id) {
console.log(id);
return true;
},
validate: function validate() {
var app = this.get('app');
var errors = this.get('errors');
var ok = true;
function validateAttribute(attr) {
if (!app[attr]) this.set('errors.' + attr, 'error'), ok = false;
}
validateAttribute = validateAttribute.bind(this);
validateAttribute('name');
validateAttribute('organization');
validateAttribute('country');
validateAttribute('website');
validateAttribute('contact');
if (!app.description['en']) {
this.set('errors.description.en', 'error')
ok = false;
} else if (app.description['en'].length > 300) {
this.set('errors.description.en', 'error')
ok = false;
}
if (!app.description['es']) {
this.set('errors.description.es', 'error')
ok = false;
} else if (app.description['es'].length > 300) {
this.set('errors.description.es', 'error')
ok = false;
}
if (!app.description['fr']) {
this.set('errors.description.fr', 'error')
ok = false;
} else if (app.description['fr'].length > 300) {
this.set('errors.description.fr', 'error')
ok = false;
}
if (!app.description['pt']) {
this.set('errors.description.pt', 'error')
ok = false;
} else if (app.description['pt'].length > 300) {
this.set('errors.description.pt', 'error')
ok = false;
}
return ok;
},
observeAppError: function (attr) {
this.observe('app.' + attr, function (newValue) {
if (newValue) this.set('errors.' + attr, '');
})
},
onrender: function () {
var app = this.get('app');
var self = this;
this.set('lang', language());
this.set('tags', tags.items);
this.set('countries', countries.items);
this.set('licenses', licenses.items);
this.set('technologies', technologies.items);
this.set('links', app.links.map(function (link, i) {
return { url: link.url, description: link.description, index: i, editing: false };
}));
this.set('adding', false);
this.observeAppError('name');
this.observeAppError('organization');
this.observeAppError('country');
this.observeAppError('website');
this.observeAppError('description.en');
this.observeAppError('description.es');
this.observeAppError('description.fr');
this.observeAppError('description.pt');
this.observeAppError('contact');
this.on('saveEditedLink', function (ev) {
ev.context.editing = false;
this.set('adding', false);
this.update();
});
this.on('editLink', function (ev) {
this.set('links.*.editing', false);
ev.context.editing = true;
this.set('adding', true);
this.update();
});
this.on('removeLink', function (ev) {
function indexOf (arr, fn) {
for (var i = 0; i < arr.length; i++) {
if (fn(arr[i])) {
return i;
}
}
return -1;
}
var currentIndex = ev.context.index;
var idx = indexOf(this.get('links'), function (v) {
return v.index === currentIndex;
});
this.splice('links', idx, 1).then(function () {
self.update();
});
});
function nextIndex () {
function max (arr) {
var m = -1;
for (var i = 0; i < arr.length; i++) {
if (arr[i].index > m) {
m = arr[i].index;
}
}
return m;
}
return max(self.get('links')) + 1;
}
this.on('addLink', function (ev) {
this.push('links', { url: 'http://', description: '', editing: true, index: nextIndex() });
this.set('adding', true);
});
this.on('save', function (ev) {
app.links = this.data.links.map(function(link) {
return { url: link.url, description: link.description };
});
if (!this.validate()) return false;
var url = '/api/applications/' + (app.id || 'create');
request
.post(url)
.send(app)
.end(function (err, res) {
scroll(document.body, 0, 500);
if (err) {
return new Panel({
classes: 'panel-danger',
heading: t('panel.heading.error'),
body: t('panel.heading.body'),
el: self.find('.panels')
});
}
var panel = new Panel({
classes: 'panel-success',
heading: t('panel.heading.success'),
body: t('admin.apps.save.success'),
el: self.find('.panels')
});
apps.fetch();
approved.fetch();
});
return false;
});
}
});
module.exports = AdminAppForm;