forked from cmanley/jquery.ui.checkListDialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.ui.checkListDialog.js
171 lines (162 loc) · 3.92 KB
/
jquery.ui.checkListDialog.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
/*
jQuery-ui checkListDialog plugin
Copyright © 2013 Craig Manley
http://www.craigmanley.com/
Licensed under MIT
http://www.opensource.org/licenses/mit-license.php
TODO: Implement key up/down navigation
Id: jquery.ui.checkListDialog.js,v 1.1 2013/10/12 18:24:39 cmanley Exp
*/
(function($) {
$.fn.checkListDialog = function(options) {
$.each(['pairs', 'callbacks'], function(i, name) {
if (!options[name]) {
throw "Mandatory option '" + name + "' is missing!";
}
});
$.each(['ok'], function(i, name) {
if (!options.callbacks[name]) {
throw "Mandatory option callbacks." + name + " is missing!";
}
});
$.each(['ok', 'cancel'], function(i, name) {
if (options.callbacks[name] && !$.isFunction(options.callbacks[name])) {
throw "Option callbacks." + name + " must be a function!";
}
});
var opts = $.extend(true, {
callbacks: {
ok: null,
cancel: null
},
class: 'CheckListDialog',
dialog: {
maxHeight: $(window).height(),
maxWidth: $(window).width(),
modal: true,
position: {
of: this
}
},
label: {
mouseout: {
css: {
backgroundColor: ''
}
},
mouseover: {
css: {
backgroundColor: '#c4e8fd'
}
}
},
enter_is_ok: true,
pairs: null,
checked: []
}, options);
// Dynamically create content for use in dialog.
var d = document.createElement('div');
d.className = opts.class;
var checked = {}; // map of checked value => true pairs
$.each(opts.checked, function(i,v) {
checked[v] = true;
});
// This should be implemented with an argument.
if (1) {
var label = document.createElement('label');
$(label).css({display: 'block'})
.on('mouseover', function() {
$(this).css(opts.label.mouseover.css);
})
.on('mouseout', function() {
$(this).css(opts.label.mouseout.css);
})
var cb = document.createElement('input');
$(cb).attr({
type: 'checkbox',
value: ''
});
$(cb).click(function() {
$("input[type=checkbox]").prop('checked',this.checked);
});
label.appendChild(cb);
label.appendChild(document.createTextNode('check/uncheck all'));
d.appendChild(label);
}
$.each(opts.pairs, function(value, text) {
var label = document.createElement('label');
label.className = 'cldlg_label';
$(label).css({display: 'block'})
.on('mouseover', function() {
$(this).css(opts.label.mouseover.css);
})
.on('mouseout', function() {
$(this).css(opts.label.mouseout.css);
})
var cb = document.createElement('input');
if (value in checked) {
cb.defaultChecked = true;
}
$(cb).attr({
type: 'checkbox',
value: value
});
label.appendChild(cb);
label.appendChild(document.createTextNode(text));
d.appendChild(label);
});
// Show jQuery-ui dialog
var ok_clicked = false;
var overridable_dialog_opts = {
buttons: {
Cancel: function() {
$(this).dialog("close");
}
}
};
var locked_dialog_opts = {
buttons: {
Ok: function() {
ok_clicked = true;
$(this).dialog("close");
}
},
close: function( event, ui ) {
var result = [];
if (ok_clicked) {
var nodes = this.querySelectorAll('input[type=checkbox]:checked'); // jQuery doesn't work on dynamically create DOM.
for (var i = 0; i < nodes.length; ++i) {
result.push(nodes[i].value);
}
}
this.parentNode.removeChild(this);
if (ok_clicked) {
opts.callbacks.ok.call(this, result);
}
else {
if (opts.callbacks.cancel) {
opts.callbacks.cancel.call(this);
}
}
}
};
var $d = $(d);
$d.css({
display : 'none',
height : '100%',
width : '100%',
overflow: 'auto'
});
if (opts.enter_is_ok) {
$d.keyup(function(e) {
if (e.keyCode == 13) {
ok_clicked = true;
$(this).dialog('close');
}
});
}
$d.dialog($.extend(true, overridable_dialog_opts, opts.dialog, locked_dialog_opts));
// Chainable result
return this;
};
})(jQuery);