summaryrefslogtreecommitdiffstats
path: root/assets/js/views/sample-add-view.js
blob: 6fea2398f11a67dff072687cd4a2b0087742a507 (plain)
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
ClipPlay.Views.SampleAddView = Marionette.View.extend({
	// Copied from
	// http://stackoverflow.com/a/10430654
	url_matcher: /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/,
	
	events: {
		'click .js-create-sample': 'on_sample_add',
		'submit .js-add-sample-form': 'hijack_form'
	},
	
	initialize: function(options) {
		this.setElement(options.el);
		this.$input = this.$('.js-sample-url');
	},
	
	hijack_form: function() {
		this.on_sample_add();
		
		return false;
	},
	
	add_sample_to_collection: function() {
		this.collection.add({
			url: this.$input.val()
		});
	},
	
	clear_input: function() {
		this.$input.val('');
	},
	
	on_sample_add: function() {
		var input_value = this.$input.val();
		if (input_value
			&& this.url_matcher.test(input_value)) {
			this.add_sample_to_collection();
		}
		
		this.clear_input();
		
		return false;
	}
});