-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controls.js
2079 lines (1767 loc) · 76.2 KB
/
Controls.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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Copyright (c) 2015 Multiplicom NV
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software
//and associated documentation files (the "Software"), to deal in the Software without restriction,
//including without limitation the rights to use, copy, modify, merge, publish, distribute,
//sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
//is furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
//INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
define([
"require", "jquery", "datetimepicker", "AXM/Externals/awesomplete/awesomplete", "_",
"AXM/AXMUtils", "AXM/DOM", "AXM/Icon", "AXM/Color", "AXM/Controls/Compound", "he", "AXM/Controls/Control"
],
function (
require, $, datetimepicker, awesomplete, _,
AXMUtils, DOM, Icon, Color, Compound
) {
var Control = require("AXM/Controls/Control");
/**
* Module encapsulating a set of classes that represent HTML controls
* @type {{}}
*/
var Module = {
Compound: Compound
};
/**
* Base class for a single control
* @returns {Object}
* @constructor
* @deprecated Superceded by Control (ES5 constructor function) but kept for backwards compatibility.
*/
Module.SingleControlBase = function() {
return new Control();
};
function ToggleControl(props) {
Control.call(this, props);
this.props = props || {
defaultState: false
};
this.value = !!this.props.defaultState; // Coerce to boolean
this.defaultStyles = {
width: "40px",
height: "21px"
};
this.createHtml = function createHTML() {
return DOM.Div(
$.extend(this.handlers, {
id: this._getSubId("-control"),
className: "toggle-control",
style: {
display: "flex",
"align-items": "center",
cursor: "pointer"
}
}),
[
DOM.Input(
$.extend(this.value ? { checked: "checked" } : {}, {
id: this._getSubId(""),
type: "checkbox",
style: this.props.style
? $.extend(this.defaultStyles, this.props.style)
: this.defaultStyles
})
),
DOM.Label({ for: this._getSubId("") }, [this.props.text || ""])
]
);
};
};
Object.defineProperties(ToggleControl.prototype, {
value: {
writable: true
}
});
ToggleControl.prototype = Object.create(Control.prototype);
Module.Toggle = ToggleControl;
Module.Text = function(text, settings) {
var control = Module.SingleControlBase(settings || {});
control._text = text;
control.textEl = DOM.Text(text || "");
control.setReactOnClick = function() {
control._reactOnClick = true;
return control;
};
/**
* Creates the html of the control
* @returns {String}
*/
control.createHtml = function() {
return DOM.Span({ id: control._getSubId("") }, [control.textEl]);
};
/**
* Returns the jQuery element containing the control
* @returns {jQuery}
*/
control.get$El = function() {
return $("#" + control._getSubId(''));
};
/**
* Modifies the text of the control
* @param {string} newText - new text content
*/
control.modifyText = function(newText) {
control._text = newText;
var selectedEl = document.getElementById(control._id)
selectedEl.textContent = newText;
};
/**
* Modifies the css Class of the control
* @param {string} newClass - new css class
*/
control.modifyCssClass = function(newClass) {
control._getSub$El('').removeClass(control._cssClass);
control.setCssClass(newClass);
control._getSub$El('').addClass(newClass);
};
/**
* Modifies the tooltip of the control
* @param {string} newText - new tooltip content
*/
control.modifyTooltip = function(newText) {
control._title = newText;
control.get$El().prop('title', newText);
};
/**
* Attaches html event handlers after DOM insertion
*/
control.attachEventHandlers = function() {
if (control._reactOnClick)
control._getSub$El('').click(control._onClicked);
};
/**
* Detaches html event handlers
*/
control.detachEventHandlers = function() {
if (control && control._reactOnClick)
control._getSub$El('').unbind('click');
};
/**
* Handles the on click event
* @param ev
* @returns {boolean}
* @private
*/
control._onClicked = function(ev) {
control.performNotify();
ev.stopPropagation();
return false;
};
return control;
};
/**
* Implements a static text control
* @param {{}} settings - control settings
* @param {string} settings.text - control text
* @returns {Object} - control instance
* @constructor
*/
Module.Static = function(settings) {
var control = Module.SingleControlBase(settings);
control._text = settings.text || '';
control._cssClass = settings.cssClass || '';
control._reactOnClick = settings.reactOnClick || false;
control._inLine = true;
control._title = settings.title || '';
if (settings.fullWidth)
control._inLine = false;
control.setCssClass = function(cssClass) {
control._cssClass = cssClass;
return control;
};
control.setReactOnClick = function() {
control._reactOnClick = true;
return control;
};
/**
* Creates the html of the control
* @returns {String}
*/
control.createHtml = function() {
var div = DOM.Div({ id: control._getSubId("") });
if (control._title) div.addAttribute("title", control._title);
if (control._inLine) {
div.addStyle("display", "inline-block").addStyle(
"vertical-align",
"middle"
);
}
if (settings.maxWidth) {
div.addStyle("max-width", settings.maxWidth + "px")
.addStyle("overflow-x", "hidden")
.addStyle("text-overflow", "ellipsis");
}
if (control._cssClass) {
div.addCssClass(control._cssClass);
}
div.addElem(control._text);
return div;
};
/**
* Returns the jQuery element containing the control
* @returns {jQuery}
*/
control.get$El = function() {
return $("#" + control._getSubId(''));
};
/**
* Modifies the text of the control
* @param {string} newText - new text content
*/
control.modifyText = function(newText) {
control._text = newText;
$('#'+control._id).html(newText);
};
/**
* Modifies the css Class of the control
* @param {string} newClass - new css class
*/
control.modifyCssClass = function(newClass) {
control._getSub$El('').removeClass(control._cssClass);
control.setCssClass(newClass);
control._getSub$El('').addClass(newClass);
};
/**
* Modifies the tooltip of the control
* @param {string} newText - new tooltip content
*/
control.modifyTooltip = function(newText) {
control._title = newText;
control.get$El().prop('title', newText);
};
/**
* Attaches html event handlers after DOM insertion
*/
control.attachEventHandlers = function() {
if (control._reactOnClick)
control._getSub$El('').click(control._onClicked);
};
/**
* Detaches html event handlers
*/
control.detachEventHandlers = function() {
if (control && control._reactOnClick)
control._getSub$El('').unbind('click');
};
/**
* Handles the on click event
* @param ev
* @returns {boolean}
* @private
*/
control._onClicked = function(ev) {
control.performNotify();
ev.stopPropagation();
return false;
};
return control;
};
/**
* Implements a control representing HTML content
* @param {string} content - html content
* @returns {Object} - control instance
* @constructor
*/
Module.RawHtml = function(content) {
var control = Module.SingleControlBase({});
control.createHtml = function() {
return content;
};
return control;
};
/**
* Implements a button control. clicking the button invokes a notification
* @param {{}} settings - control settings
* @param {int} settings.width - (optional) button width
* @param {int} settings.height - (optional) button height
* @param {string} settings.buttonClass - (optional) css class of the button
* @param {boolean} settings.enabled - (optional) initial enabled state of the control
* @param {AXM.Icon|string} settings.icon - (optional) icon displayed in the button
* @param {float} settings.iconSizeFraction - (optional) icon magnification factor
* @param {string} settings.text - (optional) button text
* @param {string} settings.hint - (optional) button hover hint
* @param {string} settings.helpId - (optional) document Id of a help text associated with the control. displayed as a little help icon on top of the button
* @returns {Object} - control instance
* @constructor
*/
Module.Button = function(settings) {
var control = Module.SingleControlBase(settings);
control._width = settings.width || 110;
control._height = settings.height || 50;
control._buttonClass = settings.buttonClass || 'AXMButton';
control._extraClasses = [];
control._enabled = true;
if (settings.enabled === false)
control._enabled = false;
control._checked = false;
if (settings.checked)
control._checked = true;
control._icon = null;//default: no icon
if (settings.icon) {
if (AXMUtils.isObjectType(settings.icon, "icon"))
control._icon = settings.icon.clone();
else {
control._icon = Icon.createFA(settings.icon);
}
var sizeFactor = 1.0;
if (settings.iconSizeFraction)
sizeFactor *= settings.iconSizeFraction;
control._icon.changeSize(sizeFactor);
}
/**
* Adds a css class to the button html
* @param {string} className - css class
* @returns {Object} - self
*/
control.addClass = function(className) {
control._extraClasses.push(className);
return control;
};
/**
* Creates the control html
* @returns {String}
*/
control.createHtml = function() {
var div = DOM.Div({ id:control._getSubId('') })
.addStyle('width',control._width+'px')
.addStyle('height',control._height+'px')
.addStyle('white-space', 'normal')
.addStyle('position', 'relative');
div.addCssClass(control._buttonClass);
$.each(control._extraClasses, function(idx, className) {
div.addCssClass(className);
});
var aligner = DOM.Div({parent: div})
.addStyle('display', 'inline-block').addStyle('line-height', 'inherit').addStyle('vertical-align', 'middle')
.addStyle('height', '100%')
.addStyle('width', '1px');
if (control._icon && !settings.text) {
var iconSize = control._icon.getSize();
div.addStyle('text-align','center');
var divIcon = DOM.Div({parent: div})
.addCssClass('AXMButtonIcon')
.addStyle('display', 'inline-block').addStyle('line-height', 'inherit').addStyle('vertical-align', 'middle');
divIcon.addElem(control._icon.renderHtml());
}
if (!control._icon && settings.text) {
//div.addStyle('text-align','center');
var divText = DOM.Div({parent: div})
.addStyle('display', 'inline-block').addStyle('line-height', 'inherit').addStyle('vertical-align', 'middle')
.addStyle('padding-left','5px')
.addElem(settings.text);
if (control._width)
divText.addStyle('max-width', (control._width-12)+'px');
}
if (control._icon && settings.text) {
var iconWidth = Math.round(control._height*0.75);
var iconSize = control._icon.getSize();
if (control._icon) {
var divIcon = DOM.Div({parent: div})
.addCssClass('AXMButtonIcon')
.addStyle('display', 'inline-block').addStyle('line-height', 'inherit').addStyle('vertical-align', 'middle')
.addStyle('width', iconWidth + 'px')
.addStyle('text-align','center');
divIcon.addElem(control._icon.renderHtml());
}
if (settings.text) {
var divText = DOM.Div({parent: div})
.addStyle('display', 'inline-block').addStyle('line-height', '14px').addStyle('vertical-align', 'middle')
.addElem(settings.text)
.addStyle('width', (control._width-iconWidth-10)+'px');
}
}
if (settings.hint)
div.addAttribute('title', settings.hint);
if (settings.helpId) {
var helpDiv = DOM.Div({parent: div})
.addStyle('position', 'absolute')
.addStyle('font-size', '18px')
.addStyle('top', '2px')
.addStyle('right', '2px')
.addCssClass('AXMButtonHelp');
helpDiv.addElem('<i class="fa fa-question-circle"></i>');
}
return div;
};
/**
* Updates the html for a change in enabled state
* @private
*/
control._updateEnabledState = function() {
if (control._enabled)
control._getSub$El('').css('opacity', 1);
else
control._getSub$El('').css('opacity', 0.4);
};
/**
* Modifies the enabled state of the button
* @param {boolean} newStatus - new enabled status
*/
control.setEnabled = function(newStatus) {
control._enabled = newStatus;
control._updateEnabledState();
};
/**
* Updates the html for a change in checked state
* @private
*/
control._updateCheckedState = function() {
if (control._checked)
control._getSub$El('').addClass('AXMButtonChecked');
else
control._getSub$El('').removeClass('AXMButtonChecked');
};
/**
* Modifies the checked state of the button
* @param {boolean} newStatus - new checked state
*/
control.setChecked = function(newStatus) {
control._checked = newStatus;
control._updateCheckedState();
};
/**
* Attaches html event handlers after DOM insertion
*/
control.attachEventHandlers = function() {
control._getSub$El('').click(control._onClicked);
control._getSub$El('').find('.AXMButtonHelp').click(control._onHelp);
control._updateEnabledState();
control._updateCheckedState();
};
/**
* Detaches html event handlers
*/
control.detachEventHandlers = function() {
if(control){
control._getSub$El('').unbind('click');
control._getSub$El('').find('.AXMButtonHelp').unbind('click');
}
};
/**
* Handles the on click event
* @param ev
* @returns {boolean}
* @private
*/
control._onClicked = function(ev) {
if (!control._enabled)
return;
control.performNotify();
ev.stopPropagation();
return false;
};
/**
* Handles the on click event for the optional help button
* @param ev
* @returns {boolean}
* @private
*/
control._onHelp = function(ev) {
require('AXM/Windows/DocViewer').create(settings.helpId);
ev.stopPropagation();
return false;
};
return control;
};
/**
* Creates a standard template button displaying an 'open' action
* @returns {Object} - control instance
* @constructor
*/
Module.OpenButton = function() {
var control = Module.Button({
icon:'fa-external-link-square',
buttonClass: 'AXMButtonInline',
width:25,
height:20,
iconSizeFraction: 0.9
});
return control;
};
/**
* Creates a standard template button displaying an 'edit' action
* @returns {Object} - control instance
* @constructor
*/
Module.EditTextItemButton = function() {
var control = Module.Button({
icon:'fa-pencil',
buttonClass: 'AXMButtonInline',
width:25,
height:16,
iconSizeFraction: 0.75
});
return control;
};
/**
* Creates a standard template button
* @param {string} helpId - doc id of the help text
* @returns {Object} - control instance
* @constructor
*/
Module.HelpButton = function(helpId) {
var control = Module.Button({
icon: 'fa-question-circle',
buttonClass: 'AXMButtonHelp',
width:26,
height:20,
iconSizeFraction: 1.05
});
control.addNotificationHandler(function() {
require('AXM/Windows/DocViewer').create(helpId);
});
return control;
};
/**
* Implements a hyperlink control. clicking on the link invokes a notification
* @param {{}} settings - control settings
* @param {string} settings.text - hyperlink text
* @param {string} settings.class - (optional) hyperlink css class
* @returns {Object} - control instance
* @constructor
*/
Module.HyperLink = function(settings) {
var control = Module.SingleControlBase(settings);
control._class = settings.class || 'AXMHyperLinkButton';
control._extraClasses = [];
/**
* Adds a css class to the hyperlink
* @param {string} className
* @returns {Object} - self
*/
control.addClass = function(className) {
control._extraClasses.push(className);
return control;
};
/**
* Creates the control html
* @returns {string} - html
*/
control.createHtml = function() {
var div = DOM.Create('div', { id:control._getSubId('') });
div.addCssClass(control._class);
$.each(control._extraClasses, function(idx, className) {
div.addCssClass(className);
});
div.addElem(settings.text);
return div;
};
/**
* Attached the html event handlers after DOM insertion
*/
control.attachEventHandlers = function() {
control._getSub$El('').click(control._onClicked);
};
/**
* Detach the html event handlers
*/
control.detachEventHandlers = function() {
if(control){
control._getSub$El('').unbind('click');
}
};
/**
* Handles the on click event
* @param ev
* @returns {boolean}
* @private
*/
control._onClicked = function(ev) {
control.performNotify();
ev.stopPropagation();
return false;
};
return control;
};
/**
* Implements a check box control. changing the checked state invokes a notification
* @param {{}} settings - control settings
* @param {boolean} settings.enabled - (optional) initial enabled state
* @param {boolean} settings.checked - (optional) initial checked state
* @param {string} settings.checkedClass - (optional) css class of the checked state
* @param {string} settings.text - text of the control
* @returns {Object} - control instance
* @constructor
*/
Module.Check = function(settings, children) {
var control = Module.SingleControlBase(settings);
control._width = settings.width || 120;
control._height = settings.height || 45;
control._enabled = settings.enabled === false ? false : true;
control._value = settings.checked || false;
control._checkedClass = settings.checkedClass || null;
children = children ? children : [];
control.createHtml = function() {
return DOM.Div({}, [
DOM.Input(
$.extend(
{
type: "checkbox",
id: control._getSubId("")
},
control._value ? { checked: "" } : {}
)
),
DOM.Label(
{
id: control._getSubId("label"),
for: control._getSubId("")
},
[]
.concat(settings.text ? [DOM.Text(settings.text)] : [])
.concat(children)
)
]);
};
/**
* Attaches the html event handlers after DOM insertion
*/
control.attachEventHandlers = function() {
control._getSub$El("").click(control._onClicked);
control._checkCheckedClass();
control._updateEnabledState();
};
/**
* Detach the html event handlers
*/
control.detachEventHandlers = function() {
if (control) {
control._getSub$El("").unbind("click");
}
};
/**
* Handles the on click event
* @param ev
* @private
*/
control._onClicked = function(ev) {
control._value = control._getSub$El("").is(":checked");
control._checkCheckedClass();
control.performNotify();
};
/**
* Updates the html reflecting the enabled state
* @private
*/
control._updateEnabledState = function() {
if (control._enabled) {
control._getSub$El("").prop("disabled", false);
control._getSub$El("label").removeClass("AXMDisabledText");
} else {
control._getSub$El("").prop("disabled", true);
control._getSub$El("label").addClass("AXMDisabledText");
}
};
/**
* Modifies the enabled state of the control
* @param {boolean} newStatus - new state
*/
control.setEnabled = function(newStatus) {
control._enabled = newStatus;
control._updateEnabledState();
};
/**
* Returns the current checked state of the control
* @returns {boolean}
*/
control.getValue = function() {
if (control._getSub$El("").length > 0) control._value = control._getSub$El("").is(":checked");
return control._value;
};
/**
* Modifies the checked state of the control
* @param {boolean} newVal - new checked state
* @param {boolean} preventNotify - if true, no notification will be invoked for this change
*/
control.setValue = function(newVal, preventNotify) {
if (newVal == control.getValue()) return false;
control._value = newVal;
if (control._value) control._getSub$El("").prop("checked", "checked");
else control._getSub$El("").removeProp("checked");
if (!preventNotify) control.performNotify();
control._checkCheckedClass();
return true;
};
/**
* Modifies the css checked class
* @private
*/
control._checkCheckedClass = function() {
if (!control._checkedClass) return;
if (control.getValue()) control._getSub$El("label").addClass(control._checkedClass);
else control._getSub$El("label").removeClass(control._checkedClass);
};
return control;
};
/**
* Implements a drop-down list control. changing the selected state invokes a notification
* @param {{}} settings - control settings
* @param {int} settings.width - width of the box
* @param {int} settings.height - height of the box
* @param {int} settings.value - initial state id
* @param {boolean} settings.disabled - if true, the control is disabled
* @returns {Object} - control instance
* @constructor
*/
Module.DropList = function(settings) {
var control = Module.SingleControlBase(settings);
control._width = settings.width || 120;
control._height = settings.height || 45;
control._states = [];
control._value = settings.value || '';
control._disabled = settings.disabled || false;
control._title = settings.title || '';
/**
* Removes all the states from the list
*/
control.clearStates = function() {
control._states = [];
control._getSub$El('').html(control._buildSelectContent());
};
/**
* Add a new state to the list
* @param {string} id - state id
* @param {string} name - state display name
* @param {string} group - (optional) group name the state should belong to
*/
control.addState = function(id, name, group) {
if (!group) group = '';
control._states.push({id:id, name:name, group:group});
control._getSub$El('').html(control._buildSelectContent());
};
/**
* Returns the current active state
* @returns {boolean}
*/
control.getValue = function () {
var item = control._getSub$El('').find(":selected");
if (item.length>0)
control._value = item.attr('value');
return control._value;
};
/**
* Returns the html implementing this control
* @returns {string}
*/
control.createHtml = function() {
var wrapper = DOM.Div();
wrapper.addStyle('display', 'inline-block');
if(control._title)
wrapper.addAttribute('title', control._title);
var cmb = DOM.Create('select', { id: control._getSubId(''), parent: wrapper });
if (control._width)
cmb.addStyle('width',control._width+'px');
if (control._disabled)
cmb.addAttribute('disabled', "disabled");
cmb.addElem(control._buildSelectContent());
return wrapper;
};
/**
* Helper function building the content of the select control
* @returns {string}
* @private
*/
control._buildSelectContent = function() {
var st = '';
var lastGroupName = '';
$.each(control._states, function(idx, state) {
var groupName = state.group || '';
if (groupName != lastGroupName) {
if (lastGroupName)
st += '</optgroup>';
lastGroupName = groupName;
if (groupName)
st += '<optgroup label="{name}">'.AXMInterpolate({name: groupName});
}
st += '<option value="{id}" {selected}>{name}</option>'.AXMInterpolate({
id: state.id,
name: state.name,
selected: (state.id == control._value) ? 'selected="selected"' : ''
});
});
if (lastGroupName)
st += '</optgroup>';
return st;
};
/**
* Attached the html event handlers after DOM insertion
*/
control.attachEventHandlers = function() {
var target = 'change.controlevent';
control._getSub$El('').unbind(target).bind(target, control._onChange);
//control._getSub$El('').click(control._onClicked);
if (control._hasDefaultFocus)
control._getSub$El('').focus();
};
/**
* Detach the html event handlers
*/
control.detachEventHandlers = function() {
if(control){
var target = 'change.controlevent';
control._getSub$El('').unbind(target);
}
};
/**
* Html handler implementing the state change event
* @private
*/
control._onChange = function(ev) {
var oldVal = control._value;
var newVal = control.getValue();
if (newVal != oldVal)
control.performNotify();
};
/**
* Sets a new active state
* @param {string} newVal - new state id
* @param {boolean} preventNotify - if true, no notification is issued about the state change
*/
control.setValue = function(newVal, preventNotify) {
if (newVal == control.getValue()) return false;
control._value = newVal;
control._getSub$El('').html(control._buildSelectContent());
if (!preventNotify)
control.performNotify();
return true;
};
/**
* Modifies the enabled state of the droplist
* @param {boolean} newStatus - new enabled status
*/
control.setEnabled = function(newStatus) {
control._disabled = !newStatus;
control._getSub$El('').prop("disabled", control._disabled);
};
return control;
};
/**
* Implements a radiobutton group control. changing the selected radiobutton invokes a notification
* @param {{}} settings - control settings
* @param {int} settings.width - width of the group
* @param {int} settings.height - height of the group
* @param {int} settings.value - initial state id
* @param {boolean} settings.disabled - if true, the control is disabled
* @returns {Object} - control instance
* @constructor
*/
Module.RadioGroup = function(settings) {
var control = Module.SingleControlBase(settings);
control._width = settings.width || null;
control._height = settings.height || 45;
control._states = [];
control._value = settings.value || '';
control._disabled = settings.disabled || false;
/**
* Removes all the states from the list
*/
control.clearStates = function() {
control._states = [];
control._getSub$El('').html(control._buildButtonContent());