-
Notifications
You must be signed in to change notification settings - Fork 4
/
cuteform.js
106 lines (102 loc) · 4.6 KB
/
cuteform.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
/***************************************************************/
/* Author: db0 ([email protected], http://db0.fr/) */
/* Sources/Licence: https://github.com/db0company/CuteForm */
/***************************************************************/
function cuteformhtml(option, options) {
var value = option.val();
var text = option.text();
var image = typeof options['images'] !== 'undefined' && typeof options['images'][value] !== 'undefined' ? options['images'][value] : (typeof option.attr('data-cuteform-image') !== 'undefined' ? option.attr('data-cuteform-image') : null);
if (image !== null) {
return $('<img class="cuteform-elt" src="' + image + '" data-cuteform-val="' + value + '" data-cuteform-text="' + text + '">');
}
var html = typeof options['html'] !== 'undefined' && typeof options['html'][value] !== 'undefined' ? options['html'][value] : (typeof option.attr('data-cuteform-html') !== 'undefined' ? option.attr('data-cuteform-html') : value);
return $('<div class="cuteform-elt" data-cuteform-val="' + value + '" data-cuteform-text="' + text + '">' + html + '</div>');
}
function cuteform(select, options) {
var options = typeof options == 'undefined' ? {} : options;
select.addClass('cuteform-select');
var cuteform, modal, modal_button;
// Modal
var with_modal = options['modal'] == 'true' || select.attr('data-cuteform-modal') == 'true';
var with_modal_text = options['modal-text'] == 'true' || select.attr('data-cuteform-modal-text') == 'true';
var title = options['title'] || select.attr('data-cuteform-title');
var name = options['name'] || select.attr('data-cuteform-name') || select.selector.split(' ').reverse()[0].replace(/[^_a-zA-Z0-9-]/g, '');
cuteform = $('<div class="cuteform" data-cuteform-name="' + name + '">' + (hide || with_modal ? '' : '<br>') + '</div>');
select.after(cuteform);
if (with_modal) {
modal = $('#cuteform-modal');
var modal_button = $('<button type="button" class="cuteform-modal-button"></button>');
select.after(modal_button);
select.hide();
cuteform.hide();
} else {
var hide = !((typeof options['hide'] !== 'undefined' && options['hide'].toString() == 'false') || select.attr('data-cuteform-hide') == 'false');
if (hide) {
select.hide();
}
}
// Show images on cuteform div
select.find('option').each(function() {
var option = $(this);
var html = cuteformhtml(option, options);
cuteform.append(html);
if (option.val() == select.find('option:selected').first().val()) {
html.addClass('cuteform-selected');
}
// On click, change current selected option and change images style
html.click(function(e) {
select.val(html.attr('data-cuteform-val'));
select.trigger('change');
cuteform.find('.cuteform-elt').removeClass('cuteform-selected');
html.addClass('cuteform-selected');
if (with_modal) {
modal_button.text('');
if (with_modal_text) {
modal_button.append('<span class="cuteform-modal-text">' + cuteform.find('.cuteform-selected').attr('data-cuteform-text') + '</span>');
} else {
modal_button.append(cuteform.find('.cuteform-selected').clone(true).off());
}
modal.modal('hide');
}
});
});
if (with_modal) {
// Change the content of the modal button and bind click
if (with_modal_text) {
modal_button.append('<span class="cuteform-modal-text">' + cuteform.find('.cuteform-selected').attr('data-cuteform-text') + '</span>');
} else {
modal_button.append(cuteform.find('.cuteform-selected').clone(true).off());
}
modal_button.click(function(e) {
e.preventDefault();
modal.css('display', 'block');
modal.find('.modal-body').text('');
var clone = cuteform.clone(true);
clone.show();
modal.find('.modal-body').append(clone);
if (title && modal.find('.modal-title').length > 0) {
modal.find('.modal-title').text(title);
}
modal.modal();
modal.on('hidden.bs.modal', function (e) {
modal.css('display', 'none');
});
});
}
// Change the selected images when the original select box changes
select.change(function() {
cuteform.find('.cuteform-elt').removeClass('cuteform-selected');
cuteform.find('[data-cuteform-val=' + select.find('option:selected').first().val() + ']').addClass('cuteform-selected');
});
}
function cuteformclear() {
$('.cuteform').remove();
$('.cuteform-selected').removeClass('cuteform-selected');
$('.cuteform-select').show();
$('.cuteform-select').removeClass('cuteform-select');
}
$(document).ready(function() {
$('select[data-cuteform=true]').each(function() {
cuteform($(this));
});
});