Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 (function($) {
|
Daniel@0
|
4 $.widget("cgpma.tickbox", {
|
Daniel@0
|
5
|
Daniel@0
|
6 options: {
|
Daniel@0
|
7 value: null,
|
Daniel@0
|
8 baseValue: null,
|
Daniel@0
|
9 },
|
Daniel@0
|
10
|
Daniel@0
|
11 _create : function() {
|
Daniel@0
|
12 var widget = this;
|
Daniel@0
|
13 widget.$element = this.element;
|
Daniel@0
|
14 widget.$element.disableSelection();
|
Daniel@0
|
15 widget.$tick = $.bem.generateElement("cgpma", "tickbox-tick");
|
Daniel@0
|
16 widget.$tick.appendTo(widget.$element);
|
Daniel@0
|
17
|
Daniel@0
|
18 widget.$element.data("widget", widget);
|
Daniel@0
|
19 widget.$element.bind("click", widget.__handleTickClick);
|
Daniel@0
|
20 },
|
Daniel@0
|
21
|
Daniel@0
|
22 _destroy : function() {
|
Daniel@0
|
23 this.$element.removeData();
|
Daniel@0
|
24 },
|
Daniel@0
|
25
|
Daniel@0
|
26 _setOption: function (key, value) {
|
Daniel@0
|
27 var widget = this;
|
Daniel@0
|
28
|
Daniel@0
|
29 // Check if such option exists, throw an error if not
|
Daniel@0
|
30 if (!widget.options.hasOwnProperty(key)) {
|
Daniel@0
|
31 throw "Option " + key + " does not exist";
|
Daniel@0
|
32 }
|
Daniel@0
|
33
|
Daniel@0
|
34 // Check if value matches what it was, do nothing if yes
|
Daniel@0
|
35 if (value === widget.options[key] || (_.isArray(value) && _.isEqual(value, widget.options[key]))) {
|
Daniel@0
|
36 return;
|
Daniel@0
|
37 }
|
Daniel@0
|
38
|
Daniel@0
|
39 // Save old option value
|
Daniel@0
|
40 var prev = widget.options[key];
|
Daniel@0
|
41
|
Daniel@0
|
42 // Apply the option
|
Daniel@0
|
43 widget._super(key, value);
|
Daniel@0
|
44
|
Daniel@0
|
45 // Call corresponding update method depending on the option key
|
Daniel@0
|
46 switch (key) {
|
Daniel@0
|
47
|
Daniel@0
|
48 case "value":
|
Daniel@0
|
49 widget._applyValue();
|
Daniel@0
|
50 break;
|
Daniel@0
|
51
|
Daniel@0
|
52 case "baseValue":
|
Daniel@0
|
53 widget._updateStatus();
|
Daniel@0
|
54 break;
|
Daniel@0
|
55 }
|
Daniel@0
|
56
|
Daniel@0
|
57 widget._trigger("change" + key.toLowerCase(), null, {newValue: value, prevValue: prev});
|
Daniel@0
|
58 },
|
Daniel@0
|
59
|
Daniel@0
|
60 _applyValue: function() {
|
Daniel@0
|
61 var widget = this;
|
Daniel@0
|
62 widget.$element.toggleClass("cgpma__tickbox_ticked", !!widget.options.value);
|
Daniel@0
|
63 widget._updateStatus();
|
Daniel@0
|
64 },
|
Daniel@0
|
65
|
Daniel@0
|
66 _updateStatus: function() {
|
Daniel@0
|
67 var widget = this;
|
Daniel@0
|
68 widget.$element.toggleClass("cgpma__tickbox_status_modified", !!widget.options.baseValue != !!widget.options.value);
|
Daniel@0
|
69 },
|
Daniel@0
|
70
|
Daniel@0
|
71 __handleTickClick: function() {
|
Daniel@0
|
72 var $this = $(this);
|
Daniel@0
|
73 var widget = $this.data("widget");
|
Daniel@0
|
74 var newValue = widget.options.value ? "" : "1";
|
Daniel@0
|
75 if (newValue == "" && $this.hasClass("cgpma__tickbox_type_radio")) {
|
Daniel@0
|
76 return;
|
Daniel@0
|
77 }
|
Daniel@0
|
78 widget._setOption("value", newValue);
|
Daniel@0
|
79 },
|
Daniel@0
|
80 });
|
Daniel@0
|
81 })(jQuery); |