/
main.js
897 lines (794 loc) · 27 KB
/
main.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
(function() {
var AddDimensionWidget, DEFAULT_MAPPING, DIMENSION_TYPE_META, Delegator, DimensionWidget, DimensionsWidget, FIELDS_META, ModelEditor, UniquesWidget, Widget, dim_config, log, util,
__slice = Array.prototype.slice,
__hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; },
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
$.plugin = function(name, object) {
return jQuery.fn[name] = function(options) {
var args;
args = Array.prototype.slice.call(arguments, 1);
return this.each(function() {
var instance;
instance = $.data(this, name);
if (instance) {
return options && instance[options].apply(instance, args);
} else {
instance = new object(this, options);
return $.data(this, name, instance);
}
});
};
};
$.a2o = function(ary) {
var obj, walk;
obj = {};
walk = function(o, path, value) {
var key;
key = path[0];
if (path.length === 2 && path[1] === '') {
if ($.type(o[key]) !== 'array') o[key] = [];
return o[key].push(value);
} else if (path.length === 1) {
if ($.inArray(key, ["key", "facet"]) === -1) {
return o[key] = value;
} else if (value === "true") {
return o[key] = true;
} else {
return delete o[key];
}
} else {
if ($.type(o[key]) !== 'object') o[key] = {};
return walk(o[key], path.slice(1), value);
}
};
$.each(ary, function() {
var p, path;
path = this.name.split('[');
path = [path[0]].concat(__slice.call((function() {
var _i, _len, _ref, _results;
_ref = path.slice(1);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
p = _ref[_i];
_results.push(p.slice(0, -1));
}
return _results;
})()));
return walk(obj, path, this.value);
});
return obj;
};
$.fn.serializeObject = function() {
var ary;
ary = this.serializeArray();
return $.a2o(ary);
};
$.fn.insertAt = function(idx, element) {
var lastIdx;
lastIdx = this.children().size();
if (idx < 0) idx = Math.max(0, lastIdx + 1 + idx);
this.append(element);
if (idx < lastIdx) this.children().eq(idx).before(this.children().last());
return this;
};
Delegator = (function() {
Delegator.prototype.events = {};
Delegator.prototype.options = {};
Delegator.prototype.element = null;
function Delegator(element, options) {
this.options = $.extend(true, {}, this.options, options);
this.element = $(element);
this.on = this.subscribe;
this.addEvents();
}
Delegator.prototype.addEvents = function() {
var event, functionName, sel, selector, _i, _ref, _ref2, _results;
_ref = this.events;
_results = [];
for (sel in _ref) {
functionName = _ref[sel];
_ref2 = sel.split(' '), selector = 2 <= _ref2.length ? __slice.call(_ref2, 0, _i = _ref2.length - 1) : (_i = 0, []), event = _ref2[_i++];
_results.push(this.addEvent(selector.join(' '), event, functionName));
}
return _results;
};
Delegator.prototype.addEvent = function(bindTo, event, functionName) {
var closure, isBlankSelector,
_this = this;
closure = function() {
return _this[functionName].apply(_this, arguments);
};
isBlankSelector = typeof bindTo === 'string' && bindTo.replace(/\s+/g, '') === '';
if (isBlankSelector) bindTo = this.element;
if (typeof bindTo === 'string') {
this.element.delegate(bindTo, event, closure);
} else {
if (this.isCustomEvent(event)) {
this.subscribe(event, closure);
} else {
$(bindTo).bind(event, closure);
}
}
return this;
};
Delegator.prototype.isCustomEvent = function(event) {
var natives;
natives = "blur focus focusin focusout load resize scroll unload click dblclick\nmousedown mouseup mousemove mouseover mouseout mouseenter mouseleave\nchange select submit keydown keypress keyup error".split(/[^a-z]+/);
event = event.split('.')[0];
return $.inArray(event, natives) === -1;
};
Delegator.prototype.publish = function() {
this.element.triggerHandler.apply(this.element, arguments);
return this;
};
Delegator.prototype.subscribe = function(event, callback) {
var closure;
closure = function() {
return callback.apply(this, [].slice.call(arguments, 1));
};
closure.guid = callback.guid = ($.guid += 1);
this.element.bind(event, closure);
return this;
};
Delegator.prototype.unsubscribe = function() {
this.element.unbind.apply(this.element, arguments);
return this;
};
return Delegator;
})();
DEFAULT_MAPPING = {
amount: {
type: 'measure',
datatype: 'float',
label: 'Amount'
},
time: {
type: 'date',
datatype: 'date',
label: 'Time'
}
};
DIMENSION_TYPE_META = {
date: {
fixedDataType: true,
helpText: 'The time dimension represents the time or period over which the\nspending occurred. Please choose the column of your dataset which\ncontains an ISO8601 formatted date (YYYY, YYYY-MM, YYYY-MM-DD, etc.).'
},
measure: {
fixedDataType: true,
helpText: 'The most important field in the dataset. Please choose which of\nthe columns in your dataset represents the value of the spending,\nand how you\'d like it to be displayed.'
}
};
FIELDS_META = {
label: {
required: true
},
name: {
required: true
}
};
log = function(s) {
return console.log(s);
};
util = {
flattenObject: function(obj) {
var flat, pathStr, walk;
flat = {};
pathStr = function(path) {
var ary, p;
ary = [path[0]];
ary = ary.concat((function() {
var _i, _len, _ref, _results;
_ref = path.slice(1);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
p = _ref[_i];
_results.push("[" + p + "]");
}
return _results;
})());
return ary.join('');
};
walk = function(path, o) {
var key, newpath, value, _results;
_results = [];
for (key in o) {
value = o[key];
newpath = $.extend([], path);
newpath.push(key);
if ($.type(value) === 'object') {
_results.push(walk(newpath, value));
} else {
if ($.type(value) === 'array') newpath.push('');
_results.push(flat[pathStr(newpath)] = value);
}
}
return _results;
};
walk([], obj);
return flat;
},
compoundType: function(type) {
return $.inArray(type, ['attribute', 'value', 'date', 'measure']) === -1;
},
flattenForm: function(data, form) {
var el, k, v, _ref, _results;
_ref = util.flattenObject(data);
_results = [];
for (k in _ref) {
v = _ref[k];
el = form.find("[name=\"" + k + "\"]");
if (el.attr('type') === 'checkbox') {
_results.push(el.prop('checked', true));
} else {
_results.push(el.val(v));
}
}
return _results;
},
validId: function(s) {
return /[a-z0-9_-]/.test(s);
},
sanitiseId: function(s) {
return s.replace(/[a-zA-Z0-9_-]/i, '');
},
flatten: function(arr) {
return arr.reduce((function(xs, el) {
if (Array.isArray(el)) {
return xs.concat(util.flatten(el));
} else {
return xs.concat([el]);
}
}), []);
},
titlize: function(s) {
return s.replace(/(^|\s)([a-z])/g, function(m, p1, p2) {
return p1 + p2.toUpperCase();
});
},
cmp: function(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
},
tabName: function(modelEditor, name) {
var meName;
meName = $(modelEditor).attr('id');
return "" + meName + "_dim_" + name;
}
};
Widget = (function(_super) {
__extends(Widget, _super);
function Widget() {
Widget.__super__.constructor.apply(this, arguments);
}
Widget.prototype.deserialize = function(data) {};
return Widget;
})(Delegator);
DimensionWidget = (function(_super) {
__extends(DimensionWidget, _super);
DimensionWidget.prototype.events = {
'.add_field click': 'onAddFieldClick',
'.field_rm click': 'onFieldRemoveClick'
};
function DimensionWidget(name, container, nameContainer, modelEditor, options) {
this.formFieldRequired = __bind(this.formFieldRequired, this);
this.formFieldPrefix = __bind(this.formFieldPrefix, this);
var el;
this.name = name;
el = $("<fieldset class='dimension tab-pane' data-dimension-name='" + this.name + "'> </fieldset>").appendTo(container);
DimensionWidget.__super__.constructor.call(this, el, options);
this.id = util.tabName(modelEditor, name);
this.element.attr('id', this.id);
}
DimensionWidget.prototype.linkText = function() {
return $("<li><a class='rm_dimension' rm-dim='" + this.name + "'><i class='icon-remove'></i></a> <a href='#" + this.id + "'>" + this.name + "</a> </li>");
};
DimensionWidget.prototype.deserialize = function(data) {
var formObj;
this.data = (data != null ? data[this.name] : void 0) || {};
this.meta = DIMENSION_TYPE_META[this.data['type']] || {};
if (util.compoundType(data.type) && !('attributes' in this.data)) {
this.data.attributes = {
'name': {
'datatype': 'id'
},
'label': {
'datatype': 'string'
}
};
}
this.element.html($.tmpl('tpl_dimension', this));
this.element.trigger('fillColumnsRequest', [this.element.find('select.column')]);
formObj = {};
formObj[this.name] = this.data;
return util.flattenForm(formObj, this.element);
};
DimensionWidget.prototype.formFieldPrefix = function(fieldName) {
return "" + this.name + "[attributes][" + fieldName + "]";
};
DimensionWidget.prototype.formFieldRequired = function(fieldName) {
var _ref;
return ((_ref = FIELDS_META[fieldName]) != null ? _ref['required'] : void 0) || false;
};
DimensionWidget.prototype.onAddFieldClick = function(e) {
var name, row;
name = prompt("Field name:").trim();
row = this._makeFieldRow(name);
row.appendTo(this.element.find('tbody'));
this.element.trigger('fillColumnsRequest', [row.find('select.column')]);
return false;
};
DimensionWidget.prototype.onFieldRemoveClick = function(e) {
$(e.currentTarget).parents('tr').first().remove();
this.element.parents('form').first().change();
return false;
};
DimensionWidget.prototype._makeFieldRow = function(name, constant) {
if (constant == null) constant = false;
return $.tmpl('tpl_dimension_field', {
'fieldName': name,
'prefix': this.formFieldPrefix,
'required': this.formFieldRequired
});
};
return DimensionWidget;
})(Widget);
dim_config = {
attribute: {
"default": "",
props: {
type: "attribute"
}
},
compound: {
"default": "",
props: {
type: "compound"
}
},
date: {
"default": "time",
props: {
type: "date",
datatype: "date"
}
},
measure: {
"default": "amount",
props: {
type: "measure",
datatype: "float"
}
}
};
AddDimensionWidget = (function(_super) {
__extends(AddDimensionWidget, _super);
AddDimensionWidget.prototype.events = {
'form #dimension_type change': 'onTypeChange',
'.add_dimension click': 'onAddDimensionClick',
'.cancel_add_dimension click': 'onCancelAddDimensionClick'
};
function AddDimensionWidget(element, modelEditor, options) {
AddDimensionWidget.__super__.constructor.apply(this, arguments);
this.modelEditor = modelEditor;
}
AddDimensionWidget.prototype.setName = function(s) {
return this.element.find('[name=new-dimension-name]').val(s);
};
AddDimensionWidget.prototype.getName = function() {
return this.element.find('[name=new-dimension-name]').val();
};
AddDimensionWidget.prototype.getType = function() {
return this.element.find('[name=new-dimension-type]:checked').val();
};
AddDimensionWidget.prototype.onTypeChange = function(e) {
var name, suggestion, type, _ref;
type = this.getType();
name = this.getName();
suggestion = (_ref = dim_config[type]) != null ? _ref['default'] : void 0;
if (!suggestion.length) {
if (name in this.modelEditor.data) {
this.setName('');
} else {
return false;
}
}
if (suggestion in this.modelEditor.data) return false;
return this.setName(suggestion);
};
AddDimensionWidget.prototype.saneName = function(name) {
return name.length && /^[a-zA-Z0-9_-]*$/.test(name);
};
AddDimensionWidget.prototype.nameUnavailable = function(name) {
return name in this.modelEditor.data;
};
AddDimensionWidget.prototype.saveNewDimension = function(name, type) {
var dwidget;
dwidget = this.modelEditor.widgetInfo['.dimensions_widget'][0];
return dwidget.autoAddDimension(name, type);
};
AddDimensionWidget.prototype.onAddDimensionClick = function(e) {
var name, tabTo, type;
name = this.getName();
type = this.getType();
if (!this.saneName(name)) {
alert("Please use only letters, numbers and dashes for names");
return;
}
if (this.nameUnavailable(name)) {
alert("That name is already taken.");
return;
}
this.saveNewDimension(name, type);
this.element.modal('hide');
this.modelEditor.element.trigger('formChange');
tabTo = 'li a[href="#' + util.tabName(this.modelEditor, name) + '"]';
return $(tabTo).click();
};
AddDimensionWidget.prototype.onCancelAddDimensionClick = function(e) {
return this.element.modal('hide');
};
return AddDimensionWidget;
})(Widget);
UniquesWidget = (function(_super) {
__extends(UniquesWidget, _super);
UniquesWidget.prototype.events = {
'.set_uniques click': 'onSetUniquesClick',
'show': 'onShow'
};
function UniquesWidget(element, modelEditor, options) {
UniquesWidget.__super__.constructor.apply(this, arguments);
this.modelEditor = modelEditor;
}
UniquesWidget.prototype.onShow = function(e) {
var add, dim, list, _results,
_this = this;
list = this.element.find('#uniques');
list.empty();
add = function(dim) {
var checked, txt;
checked = _this.modelEditor.data[dim]['key'];
txt = "<input name='" + dim + "' type='checkbox' value='true' /> " + dim;
$("<li>" + txt + "</li>").appendTo(list);
return list.find("[name=\"" + dim + "\"]").prop('checked', checked);
};
_results = [];
for (dim in this.modelEditor.data) {
_results.push(add(dim));
}
return _results;
};
UniquesWidget.prototype.onSetUniquesClick = function(e) {
var list,
_this = this;
list = this.element.find('#uniques');
if (!list.find(':checked').length) {
return alert("You must specify at least one dimension as unique");
} else {
list.find('input').each(function(idx, elt) {
var dim, tgt;
dim = $(elt).attr('name');
tgt = $("[name=\"" + dim + "[key]\"]");
return tgt.prop('checked', $(elt).prop('checked'));
});
this.element.modal('hide');
list.empty();
return this.modelEditor.element.trigger('formChange');
}
};
return UniquesWidget;
})(Widget);
DimensionsWidget = (function(_super) {
__extends(DimensionsWidget, _super);
DimensionsWidget.prototype.events = {
'.add_attribute_dimension click': 'onAddAttributeDimensionClick',
'.add_compound_dimension click': 'onAddCompoundDimensionClick',
'.add_date_dimension click': 'onAddDateDimensionClick',
'.add_measure click': 'onAddMeasureClick',
'.rm_dimension click': 'onRemoveDimensionClick',
'.rm_all_dimensions click': 'onRemoveAllDimensionsClick',
'.dimension-names click': 'onSelectDimensionClick'
};
function DimensionsWidget(element, modelEditor, options) {
DimensionsWidget.__super__.constructor.apply(this, arguments);
this.widgets = [];
this.dimsEl = $('.dimensions').get(0);
this.dimNamesEl = $(modelEditor != null ? modelEditor.namesHook : void 0) || this.element.find('.dimension-names').get(0);
this.modelEditor = modelEditor;
}
DimensionsWidget.prototype.addDimension = function(name) {
var w;
w = new DimensionWidget(name, this.dimsEl, this.dimNamesEl, this.modelEditor);
this.widgets.push(w);
this.refreshNames();
return w;
};
DimensionsWidget.prototype.refreshNames = function() {
var tmp, w, _i, _len, _results;
$(this.dimNamesEl).empty();
tmp = this.widgets;
tmp.sort(function(a, b) {
return util.cmp(a.id, b.id);
});
_results = [];
for (_i = 0, _len = tmp.length; _i < _len; _i++) {
w = tmp[_i];
_results.push(w.linkText().appendTo(this.dimNamesEl));
}
return _results;
};
DimensionsWidget.prototype.removeDimension = function(name) {
var idx, w, _i, _len, _ref;
idx = null;
_ref = this.widgets;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
w = _ref[_i];
if (w.name === name) {
idx = this.widgets.indexOf(w);
break;
}
}
if (idx !== null) this.widgets.splice(idx, 1)[0].element.remove();
delete this.modelEditor.data[name];
this.refreshNames();
return this.element.trigger('modelChange');
};
DimensionsWidget.prototype.deserialize = function(data) {
var dims, name, obj, toRemove, widget, _i, _j, _len, _len2, _ref, _results;
if (this.ignoreParent) return;
dims = data || {};
toRemove = [];
_ref = this.widgets;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
widget = _ref[_i];
if (widget.name in dims) {
widget.deserialize(data);
delete dims[widget.name];
} else {
toRemove.push(widget.name);
}
}
for (_j = 0, _len2 = toRemove.length; _j < _len2; _j++) {
name = toRemove[_j];
this.removeDimension(name);
}
_results = [];
for (name in dims) {
obj = dims[name];
_results.push(this.addDimension(name).deserialize(data));
}
return _results;
};
DimensionsWidget.prototype.createDimension = function(name, props) {
var data;
data = {};
data[name] = props;
data[name]['label'] = util.titlize(name);
return this.addDimension(name.trim()).deserialize(data);
};
DimensionsWidget.prototype.promptAddDimension = function(suggestion, props) {
var name;
name = prompt("Please enter a name for the dimension (without spaces):", suggestion);
if (!name) return false;
return this.createDimension(name, props);
};
DimensionsWidget.prototype.autoAddDimension = function(name, type) {
var props;
props = dim_config[type]['props'];
return this.createDimension(name, props);
};
DimensionsWidget.prototype.onAddDimension = function(type) {
var meta;
meta = dim_config[type];
this.promptAddDimension(meta['default'], meta['props']);
return false;
};
DimensionsWidget.prototype.onAddAttributeDimensionClick = function(e) {
return this.onAddDimension('attribute');
};
DimensionsWidget.prototype.onAddCompoundDimensionClick = function(e) {
return this.onAddDimension('compound');
};
DimensionsWidget.prototype.onAddDateDimensionClick = function(e) {
return this.onAddDimension('date');
};
DimensionsWidget.prototype.onAddMeasureClick = function(e) {
return this.onAddDimension('measure');
};
DimensionsWidget.prototype.onSelectDimensionClick = function(e) {
$('.dimension').hide();
$('.dimensions #help').hide();
$(e.target.hash).show();
return false;
};
DimensionsWidget.prototype.onRemoveDimensionClick = function(e) {
var dimension;
dimension = $(e.currentTarget).attr('rm-dim');
this.removeDimension(dimension);
return false;
};
DimensionsWidget.prototype.onRemoveAllDimensionsClick = function(e) {
var name;
for (name in this.modelEditor.data) {
this.removeDimension(name);
}
return false;
};
return DimensionsWidget;
})(Delegator);
ModelEditor = (function(_super) {
__extends(ModelEditor, _super);
ModelEditor.prototype.widgetTypes = {
'.dimensions_widget': DimensionsWidget,
'#check-uniques': UniquesWidget,
'#add-dimension': AddDimensionWidget
};
ModelEditor.prototype.events = {
'modelChange': 'onModelChange',
'fillColumnsRequest': 'onFillColumnsRequest',
'.forms form submit': 'onFormSubmit',
'.forms form change': 'onFormChange',
'#editor-form change': 'onFormChange',
'formChange': 'onFormChange'
};
function ModelEditor(element, options) {
var ctor, data, e, mapping, selector, w, _i, _len, _ref, _ref2;
ModelEditor.__super__.constructor.apply(this, arguments);
this.target = options.target;
mapping = JSON.parse($(this.target).html());
data = mapping || DEFAULT_MAPPING;
this.data = $.extend(true, {}, data);
this.widgets = [];
this.widgetInfo = {};
this.namesHook = options != null ? options.namesHook : void 0;
this.form = $(element).find('.forms form').eq(0);
console.log(this.form);
this.id = this.element.attr('id');
if (!(this.id != null)) {
this.id = Math.floor(Math.random() * 0xffffffff).toString(16);
this.element.attr('id', this.id);
}
this.element.find('script[type="text/x-jquery-tmpl"]').each(function() {
return $(this).template($(this).attr('id'));
});
this.element.find('select.column').each(function() {
return $(this).trigger('fillColumnsRequest', [this]);
});
_ref = this.widgetTypes;
for (selector in _ref) {
ctor = _ref[selector];
this.widgetInfo[selector] = [];
_ref2 = this.element.find(selector).get();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
e = _ref2[_i];
w = new ctor(e, this);
this.widgets.push(w);
this.widgetInfo[selector].push(w);
}
}
this.element.trigger('modelChange');
}
ModelEditor.prototype.onFormChange = function(e) {
console.log("FORM CHANGE");
if (this.ignoreFormChange) return;
this.data = this.form.serializeObject();
this.ignoreFormChange = true;
this.element.trigger('modelChange');
return this.ignoreFormChange = false;
};
ModelEditor.prototype.onFormSubmit = function(e) {
return false;
};
ModelEditor.prototype.onModelChange = function() {
var payload, w, _i, _len, _ref;
util.flattenForm(this.data, this.form);
_ref = this.widgets;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
w = _ref[_i];
w.deserialize($.extend(true, {}, this.data));
}
payload = JSON.stringify(this.data, null, 2);
$(this.options.target).val(payload);
return this.updateColumns();
};
ModelEditor.prototype.updateColumns = function() {
var classes, column, el, _i, _len, _ref, _results;
el = this.element.find('#new-dimension-columns');
el.empty();
_ref = this.options.columns.sort();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
column = _ref[_i];
classes = this.columnUsed(column) ? 'used' : '';
_results.push(el.append('<li class=' + classes + '>' + column + '</li>'));
}
return _results;
};
ModelEditor.prototype.onFillColumnsRequest = function(elem) {
var unused, used, x;
unused = (function() {
var _i, _len, _ref, _results;
_ref = this.options.columns;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
x = _ref[_i];
if (!this.columnUsed(x)) _results.push(x);
}
return _results;
}).call(this);
$(elem).empty();
if (unused.length) {
$(elem).append("<optgroup label='Unused columns'>");
$(elem).append(((function() {
var _i, _len, _ref, _results;
_ref = unused.sort();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
x = _ref[_i];
_results.push("<option name='" + x + "'>" + x + "</option>");
}
return _results;
})()).join('\n'));
}
used = (function() {
var _i, _len, _ref, _results;
_ref = this.options.columns;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
x = _ref[_i];
if (this.columnUsed(x)) _results.push(x);
}
return _results;
}).call(this);
if (used.length) {
$(elem).append("<optgroup label='Used columns'>");
return $(elem).append(((function() {
var _i, _len, _ref, _results;
_ref = used.sort();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
x = _ref[_i];
_results.push("<option name='" + x + "'>" + x + "</option>");
}
return _results;
})()).join('\n'));
}
};
ModelEditor.prototype.columnsUsed = function() {
var cols_in_dim, d,
_this = this;
cols_in_dim = function(d) {
var attr, dim, _results;
dim = _this.data[d];
if (dim['attributes']) {
_results = [];
for (attr in dim['attributes']) {
_results.push(dim['attributes'][attr]['column']);
}
return _results;
} else {
return [dim['column']];
}
};
return util.flatten((function() {
var _results;
_results = [];
for (d in this.data) {
_results.push(cols_in_dim(d));
}
return _results;
}).call(this));
};
ModelEditor.prototype.columnUsed = function(s) {
return $.inArray(s, this.columnsUsed()) > -1;
};
return ModelEditor;
})(Delegator);
$.plugin('modelEditor', ModelEditor);
this.ModelEditor = ModelEditor;
}).call(this);