comparison src/DML/MainVisBundle/Resources/assets/marionette/modules/MainRegionModule/MainRegionModule.50-cgpma.tickbox.js @ 0:493bcb69166c

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