-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.checklist.js
More file actions
75 lines (74 loc) · 2.39 KB
/
jquery.checklist.js
File metadata and controls
75 lines (74 loc) · 2.39 KB
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
/*!
* Turn multiple <select> form control into user friendly checklist.
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
$.fn.checklist = function () {
// Create a wrapper
var wrapperId = this.wrapAll('<div>').parent().uniqueId().attr('id');
// Hide this element
this.hide();
// Deal with settings
// @todo More robust API with options
/*
var settings = $.extend(
{},
{ wrapper: '' },
options
);
*/
// Colect the optionLisit array
var optionList = this.find('option').map(function (i, el) {
return {
value: $(this).attr('value'),
text: this.innerHTML
};
}).get();
// Initialize selectList
var selectList = this.find('option').map(function (i, el) {
if ($(this).is(':selected')) {
return $(this).attr('value');
}
}).get();
// Replace the original option with Vue implementation
this.attr('v-model', 'selectList');
this.attr('v-on:change', 'onSelectChanged');
this.html(
'<option v-for="option in optionList" :value="option.value">' +
'{{ option.text }}' +
'</option>'
);
// Show the checklist
this.parent().prepend(
'<div class="ui relaxed list">' +
'<div class="item" v-for="option in optionList">' +
'<div class="ui checkbox">' +
'<input type="checkbox" v-bind:value="option.value" v-model="selectList" v-on:change="onCheckChanged">' +
'<label>{{ option.text }}</label>' +
'</div>' +
'</div>' +
'</div>'
);
// Instance Vue object
var vm = new Vue({
el: '#' + wrapperId,
data: {
optionList: optionList,
selectList: selectList
},
});
// Activate Semantic UI checkbox
$('.ui.checkbox').checkbox();
// Chainable
return this;
};
}));