-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioSelector.js
332 lines (267 loc) · 10.6 KB
/
AudioSelector.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
dojo.provide("unc.AudioSelector");
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require('dijit.form.Button');
dojo.require('dijit.form.TextBox');
dojo.require('dijit.form.ValidationTextBox');
dojo.require('dojox.form.FileInput');
dojo.require('dijit.form.Form');
dojo.require('dijit.layout.ContentPane');
dojo.require('dijit.layout.TabContainer');
dojo.require('dojox.grid.DataGrid');
dojo.require('dijit.Dialog');
// a store singleton for use by all of these
dojo.ready(function() {
unc._AudioMediaStore = uow.getDatabase({
database: 'Media',
collection: 'Audio',
mode: 'rc'
});
});
dojo.declare("unc.AudioSelector", [ dijit._Widget, dijit._Templated ], {
templatePath: dojo.moduleUrl('unc', 'AudioSelector.html'),
widgetsInTemplate: true,
name: '', // the name of this variable
value: '', //this is the URL
disabled: false,
tags: '',
duration: '',
postCreate: function() {
this.handle1 = this.connect(this.at_addSoundButton, 'onClick', "showDialog");
this.handle2 = this.connect(this.at_playCurrentSoundButton, 'onClick', "playSound");
//this should set the init value
this.set('value', this.value);
},
showDialog: function() {
console.log('about to create dialog');
var dialog = new unc.AudioSearchUpload();
console.log('created dialog');
dojo.connect(dialog, 'onOK', dojo.hitch(this, function() {
var sound = dialog.selected;
dialog.hide();
dialog.destroyRecursive();
this.setSoundValue(sound.URL, sound.tags, sound.duration);
//set sound to value
this.value = sound.URL;
this.onChange(this.value);
}));
console.log('about to show');
dialog.show();
console.log('show returns');
},
setSoundValue: function(url, tags, duration) {
console.log(url, ",", tags, ",", duration);
this.value = url;
this.at_soundTextBox.attr('value', tags.join(' ') + ", " + duration );
},
playSound: function() {
console.log('this should play', this.value);
if(this.value) {
uow.getAudio().then(dojo.hitch(this, function(a) {
a.play({url : this.value});
}));
}
},
onChange: function(value) {
//this is the onChange method holder
},
_setDisabledAttr: function(value) {
console.log('AS disable', value);
dojo.query("[widgetId]", this.domNode).forEach(function(childNode) {
var child = dijit.byId(dojo.attr(childNode, 'widgetid'));
child.set('disabled', value);
});
},
_setValueAttr: function(value) {
this.value = value;
if(this.value) {
console.log("starting set Value");
dojo.when(unc._AudioMediaStore, dojo.hitch(this, function(db) {
db.fetch({
query:{'URL':this.value},
onComplete: dojo.hitch(this, function(items) {
if(items.length > 0) {
this.tags = items[0].tags;
this.duration = items[0].duration;
}
this.setSoundValue(this.value, this.tags, this.duration);
})
});
}));
}
}
});
function formatTags(tags) {
return tags.join(' ');
}
function formatTime(t) {
return t.toFixed(2);
}
dojo.declare('unc.AudioSearchUpload', [ dijit._Widget ], {
// I can't make templated creation work so I'll have to do it programmatically. yuk.
popup: null, // the dialog goes here
url: '', // base URL without the extension for the sound
selected: '',
postCreate: function(self) {
console.log('postCreate starts');
this.inherited(arguments);
// wait on the database
dojo.when(unc._AudioMediaStore, dojo.hitch(this, function(store) {
this.store = store;
this.tabc = new dijit.layout.TabContainer({
style: 'width:600px; height:400px',
'class': 'AudioSearchUpload'
});
var d = dojo.create('div');
var t1 = new dijit.layout.ContentPane({
title: 'Search',
content: d
});
var l = dojo.create('label', {innerHTML: 'Query: '}, d);
this.query = new dijit.form.TextBox();
dojo.place(this.query.domNode, l);
this.search = new dijit.form.Button({label: 'Search'});
dojo.place(this.search.domNode, d);
this.connect(this.search, 'onClick', function() {
this.grid.setQuery({ tags: { '$all': this.query.get('value').split(' ') }});
});
dojo.create('br', {}, d);
// create grid placeholder
// if I don't specify a height the grid will have only one line
this.gridGoesHere = dojo.create('div', {style: "height:300px;"}, d);
this.grid = new dojox.grid.DataGrid({
structure: [{
name: 'Tags',
field: 'tags',
formatter: formatTags,
width: 'auto'
}, {
name: 'Duration',
field: 'duration',
formatter: formatTime,
width: '5em'
}
],
store: store
}, this.gridGoesHere);
this.connect(this.grid, 'onSelected', 'lightSelect');
this.connect(this.grid, 'onRowDblClick', 'hardSelect');
this.select = new dijit.form.Button({label: 'OK'});
dojo.place(this.select.domNode, d);
this.connect(this.select, 'onClick', 'onOK');
this.play = new dijit.form.Button({label: 'Preview'});
dojo.place(this.play.domNode, d);
this.connect(this.play, 'onClick', 'onPlay');
this.cancel = new dijit.form.Button({label: 'Cancel'});
dojo.place(this.cancel.domNode, d);
this.connect(this.cancel, 'onClick', 'onCancel');
this.tabc.addChild(t1);
var d2 = dojo.create('div');
var t2 = new dijit.layout.ContentPane({
title: 'Upload',
content: d2
});
this.uploadForm = dojo.create('form', { method:"POST",
enctype:"multipart/form-data" });
dojo.place(this.uploadForm, d2);
var f = this.uploadForm;
l = dojo.create('label', {innerHTML: 'Title: '}, f);
this.title = new dijit.form.TextBox({ name: 'title' });
dojo.place(this.title.domNode, l);
dojo.create('br', {}, f);
l = dojo.create('label', {innerHTML: 'Tags: '}, f);
this.tags = new dijit.form.TextBox({ name: 'tags' });
dojo.place(this.tags.domNode, l);
dojo.create('br', {}, f);
l = dojo.create('label', {innerHTML: 'Description: '}, f);
this.description = new dijit.form.TextBox({ name: 'description' });
dojo.place(this.description.domNode, l);
dojo.create('br', {}, f);
l = dojo.create('label', {innerHTML: 'Credit URL: '}, f);
this.credit = new dijit.form.TextBox({ name: 'creditURL' });
dojo.place(this.credit.domNode, l);
dojo.create('br', {}, f);
l = dojo.create('label', {innerHTML: 'Choose File: '}, f);
this.file = new dojox.form.FileInput({ name: 'file' });
dojo.place(this.file.domNode, l);
dojo.create('br', {}, f);
this.messages = dojo.create('p', {}, f);
this.submit = new dijit.form.Button({ type:'button',
label: 'Submit' });
dojo.place(this.submit.domNode, d2);
this.connect(this.submit, 'onClick', 'onSubmit');
this.cancel2 = new dijit.form.Button({ type:'button',
label: 'Cancel' });
this.connect(this.cancel2, 'onClick', 'onCancel');
dojo.place(this.cancel2.domNode, d2);
//this.uploadForm.startup();
this.tabc.addChild(t2);
}));
},
lightSelect: function(idx) {
this.selected = this.grid.getItem(idx);
console.log("selected ", this.selected);
},
hardSelect: function(evt) {
var selection = this.grid.selection.getSelected();
console.log("selected ", this.selected);
this.selected = selection[0];
},
onOK: function() {
},
onPlay: function() {
uow.getAudio().then(dojo.hitch(this, function(a) {
a.play({url : this.selected.URL});
}));
},
onCancel: function() {
this.hide();
},
onSubmit: function() {
console.log('submit', this.uploadForm.value);
var f = this.uploadForm;
theForm = this.uploadForm;
if (!f.file.value) {
this.showMessage('You must specify a file to upload.');
return;
}
if (!f.title.value) {
this.showMessage('Please give your sound a title so others can find it');
//return;
}
if (!f.tags.value) {
this.showMessage('Please give your sound some descriptive tags so others can find it.');
//return;
}
dojo.when(unc._AudioMediaStore, dojo.hitch(this, function(db) {
db.upload({
form: this.uploadForm,
load: dojo.hitch(this, function(data, ioArgs) {
console.log('load', data);
this.selected = data;
this.onOK();
console.log("Upload has been confirmed!");
}),
error: dojo.hitch(this, function(msg, ioArgs) {
console.log('error', msg);
this.showMessage(msg);
//dojo.byId('messages').innerHTML = msg;
})
});
}));
},
showMessage: function(msg) {
this.messages.innerHTML = msg;
},
show: function() {
this.popup = new dijit.Dialog({
content: this.tabc
});
this.popup.show();
this.tabc.startup();
this.grid.startup();
},
hide: function() {
this.popup.destroy();
}
});