summaryrefslogtreecommitdiffstats
path: root/web/js/labitrack.d/38-labelform.js
blob: 6136e6b96347a1304e0054992ffc163e3f842f97 (plain) (blame)
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
(function(){

	λ.labelform = function(opts){
		var model = opts.model;
		var canvas = $('#label')[0];
		var label = new λ.label();
		label.set_canvas(canvas);

		var rendertime = $('#rendertime');
		(label.trigger_refresh = function(){
			label.set_data(model.toJSON());
			label.render(function(data, ms){
				rendertime.text(ms);
			});
		})();

		var f = $('#labelform');
		function update_data(){
			var t = this;
			setTimeout(function(){
				var v = t.value;
				var foo = {};
				foo[t.name] = model.get(t.name);
				switch (t.type) {
				case 'text':
				case 'textarea':
					v = v.trim();
					if (v.length === 0) {
						v = $(t).attr('placeholder');
					}
					foo[t.name] = v.trim();
					break;
				case 'checkbox':
					var i = $.inArray(v, foo[t.name]);
					if (t.checked) {
						if (i === -1) {
							foo[t.name].push(t.value);
						}
					} else if (i !== -1) {
						foo[t.name].splice(i, 1);
					}
					break;
				}
				model.set(foo);
				label.trigger_refresh();
			}, 0);
		};
		f.find(':input').each(function (i,e){
			$(e).bind('keypress', update_data);
			$(e).bind('click', update_data);
			$(e).bind('change', update_data);
			update_data.apply(this);
		});

		print = null;

		submit_btn = f.find('button[type="submit"]');

		f.find('a#saveandprint').bind('click', function(){
			print = true;
			submit_btn.click();
		});

		f.find('a#justsave').bind('click', function(){
			print = false;
			submit_btn.click();
		});

		reset_btn = f.find('button[type="reset"]');

		f.find('a#reset').bind('click', function(){
			reset_btn.click();
		});

		f.bind('submit', function(){
			opts.submit(label, model, print);
			return false;
		});
	};
}());