1 | |
---|
2 | |
---|
3 | /** |
---|
4 | * @fileOverview functions for handling options |
---|
5 | * @author |
---|
6 | * @version |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | gets an option, checking with the values in the navigation-UI |
---|
11 | */ |
---|
12 | function opt(key) { |
---|
13 | |
---|
14 | if ($('#' + input_prefix + key) && (opts[key].value != $('#' + input_prefix + key).val())) { |
---|
15 | opts[key].value = $('#' + input_prefix + key).val(); |
---|
16 | } else if (opts[key].value) { |
---|
17 | return opts[key].value |
---|
18 | } else if (opts[key]) { |
---|
19 | return opts[key] |
---|
20 | } else { |
---|
21 | return "" |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | function setOpt(input_object) { |
---|
26 | |
---|
27 | var id = $(input_object).attr("id"); |
---|
28 | var val = $(input_object).val(); |
---|
29 | key = id.substring(id.indexOf(input_prefix) + input_prefix.length) |
---|
30 | opts[key].value = val; |
---|
31 | return opts[key].value; |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | function fillOpts(trg_container) { |
---|
36 | |
---|
37 | for ( var key in opts ) { |
---|
38 | if ($('#' + input_prefix + key).length) { |
---|
39 | $('#' + input_prefix + key).value = opts[key].value; |
---|
40 | } else if (trg_container) { |
---|
41 | var new_input_label = "<label>" + key + "</label>"; |
---|
42 | var new_input; |
---|
43 | |
---|
44 | if (opts[key].widget == "slider") { |
---|
45 | [new_input,new_widget] = genSlider(key, opts[key].values); |
---|
46 | } else if (opts[key].widget =="selectone") { |
---|
47 | [new_input,new_widget] = genCombo(key, opts[key].values); |
---|
48 | // set initial value |
---|
49 | $(new_input).val(opts[key].value); |
---|
50 | |
---|
51 | // $(new_input).autocomplete({"source":opts[key].values}); |
---|
52 | } |
---|
53 | |
---|
54 | /* hook changing options + redrawing the graph, when values in navigation changed */ |
---|
55 | new_input.change(function () { |
---|
56 | setOpt(this); |
---|
57 | var related_widget = $(this).data("related-widget"); |
---|
58 | if ( $(related_widget).hasClass("widget-slider")) {$(related_widget).slider("option", "value", $(this).val()); } |
---|
59 | renderGraph(); |
---|
60 | }); |
---|
61 | |
---|
62 | $(trg_container).append(new_input_label, new_input, new_widget); |
---|
63 | |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | /** generating my own comboboxes, because very annoying trying to use some of existing jquery plugins (easyui.combo, combobox, jquery-ui.autocomplete) */ |
---|
71 | function genCombo (key, data) { |
---|
72 | |
---|
73 | var select = $("<select id='widget-" + key + "' />") |
---|
74 | select.attr("id", input_prefix + key) |
---|
75 | data.forEach(function(v) { $(select).append("<option value='" + v +"' >" + v + "</option>") }); |
---|
76 | return [select, null]; |
---|
77 | } |
---|
78 | |
---|
79 | function genSlider (key, data) { |
---|
80 | |
---|
81 | var new_input = $("<input />"); |
---|
82 | new_input.attr("id", input_prefix + key) |
---|
83 | .val(opts[key].value) |
---|
84 | /* .attr("type", "text")*/ |
---|
85 | .attr("size", 3); |
---|
86 | |
---|
87 | var new_widget = $("<div class='widget-" + opts[key].widget + "'></div>"); |
---|
88 | new_widget.attr("id", "widget-" + key); |
---|
89 | new_widget.slider( opts[key]); |
---|
90 | |
---|
91 | // set both-ways references between the input-field and its slider - necessary for updating |
---|
92 | new_widget.data("related-input-field",new_input); |
---|
93 | new_input.data("related-widget",new_widget); |
---|
94 | |
---|
95 | // console.log("widget:" + opts[key].widget); |
---|
96 | |
---|
97 | new_widget.bind( "slidechange", function(event, ui) { |
---|
98 | // console.log(ui.value); |
---|
99 | $(this).data("related-input-field").val(ui.value); |
---|
100 | // update the opts-object, but based on the (updated) value of the related input-field |
---|
101 | setOpt($(this).data("related-input-field")); |
---|
102 | renderGraph(); |
---|
103 | }); |
---|
104 | |
---|
105 | return [new_input,new_widget]; |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | function notify (msg) { |
---|
110 | $("#notify").append(msg); |
---|
111 | } |
---|