1
|
if(typeof(String.prototype.trim) === "undefined")
|
2
|
{
|
3
|
String.prototype.trim = function()
|
4
|
{
|
5
|
return String(this).replace(/^\s+|\s+$/g, '');
|
6
|
};
|
7
|
}
|
8
|
|
9
|
(function ($, undefined) {
|
10
|
$.widget('eo.checkboxwidget', {
|
11
|
options: {
|
12
|
noneCaption: "Aucun",
|
13
|
allCaption: undefined,
|
14
|
},
|
15
|
_create: function () {
|
16
|
this.state = 0;
|
17
|
this.visible = this.element.is(':visible');
|
18
|
this.elements = [];
|
19
|
var inputs = $('input[type="checkbox"]', this.element)
|
20
|
this.placeholder = $('<div></div>')
|
21
|
.css('position', 'relative')
|
22
|
.addClass('eo-checkboxwidget-placeholder');
|
23
|
this.selection = $('<span></span>')
|
24
|
.addClass('eo-checkboxwidget-selection')
|
25
|
.appendTo(this.placeholder);
|
26
|
this.element.before(this.placeholder);
|
27
|
this.menu = $('<div></div>')
|
28
|
.addClass('eo-checkboxwidget-menu')
|
29
|
.css('position', 'fixed')
|
30
|
.hide()
|
31
|
.appendTo(this.placeholder);
|
32
|
for (var i = 0; i < inputs.length; i++) {
|
33
|
var $input = $(inputs[i]);
|
34
|
var content = $input.closest('label').text().trim();
|
35
|
var $menu_element = $('<div></div>')
|
36
|
.addClass('eo-checkboxwidget-menu-element')
|
37
|
.text(content)
|
38
|
.data('input', $input)
|
39
|
.appendTo(this.menu);
|
40
|
if ($input.is(':checked')) {
|
41
|
$menu_element.addClass('eo-checkboxwidget-menu-element-selected');
|
42
|
}
|
43
|
this.elements.push({
|
44
|
menu_element: $menu_element,
|
45
|
content: content,
|
46
|
input: $input
|
47
|
});
|
48
|
}
|
49
|
this.menu.on('click', '.eo-checkboxwidget-menu-element', $.proxy(function (e) {
|
50
|
var $menu_element = $(e.target);
|
51
|
var $input = $menu_element.data('input');
|
52
|
var checked = $input.is(':checked');
|
53
|
$menu_element.toggleClass('eo-checkboxwidget-menu-element-selected',
|
54
|
! checked);
|
55
|
$input.prop('checked', ! checked);
|
56
|
$input.trigger('change');
|
57
|
this._refresh();
|
58
|
}, this));
|
59
|
/* this.menu.closest('body').on('click.eo-checkboxwidget', $.proxy(function (e) {
|
60
|
if ($(e.target).closest(this.element).length == 0) {
|
61
|
this.menu.toggle();
|
62
|
}
|
63
|
}, this)); */
|
64
|
this.placeholder.on('mousemove.eo-checkboxwidget', $.proxy(function () {
|
65
|
if (this.__proto__.block == 1) {
|
66
|
return true;
|
67
|
}
|
68
|
this.__proto__.block = 1;
|
69
|
this.state = 1;
|
70
|
this.menu.show();
|
71
|
var placeholder_top = this.placeholder.offset().top-$(document).scrollTop();
|
72
|
if (placeholder_top < (window.innerHeight/2)) {
|
73
|
this.menu.css('top', placeholder_top+this.placeholder.outerHeight(true));
|
74
|
} else {
|
75
|
this.menu
|
76
|
.css('top', placeholder_top-this.menu.outerHeight(true));
|
77
|
}
|
78
|
return false;
|
79
|
}, this))
|
80
|
this.element.hide();
|
81
|
this.menu.on('mouseleave', $.proxy(function () {
|
82
|
setTimeout($.proxy(function () {
|
83
|
this.__proto__.block = 0
|
84
|
}, this), 100);
|
85
|
this.state = 0;
|
86
|
this.menu.hide();
|
87
|
}, this)
|
88
|
);
|
89
|
},
|
90
|
_init: function() {
|
91
|
this._refresh();
|
92
|
},
|
93
|
_refresh: function () {
|
94
|
this.selection.html('');
|
95
|
var checked = [];
|
96
|
for (var i = 0; i < this.elements.length; i++) {
|
97
|
if (this.elements[i].input.is(':checked')) {
|
98
|
checked.push(i);
|
99
|
}
|
100
|
}
|
101
|
if (checked.length == this.elements.length && this.options.allCaption) {
|
102
|
this.selection.append(this.options.allCaption);
|
103
|
} else if (checked.length) {
|
104
|
for (var i = 0; i < checked.length; i++) {
|
105
|
var content = this.elements[checked[i]].content;
|
106
|
var $caption = $('<span>'+content+'</span>')
|
107
|
.addClass('eo-checkboxwidget-caption');
|
108
|
$caption.appendTo(this.selection);
|
109
|
if (i != (checked.length-1)) {
|
110
|
this.selection.append(', ');
|
111
|
}
|
112
|
}
|
113
|
} else {
|
114
|
this.selection.append(this.options.noneCaption);
|
115
|
}
|
116
|
},
|
117
|
destroy: function () {
|
118
|
$.Widget.prototype.destroy.call(this);
|
119
|
this.placeholder.remove();
|
120
|
if (this.visible) {
|
121
|
this.element.show();
|
122
|
}
|
123
|
}
|
124
|
})
|
125
|
})(window.jQuery)
|