Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

core/js/singleselect.js (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
(function ($) {
2
	$.fn.singleSelect = function () {
3
		return this.each(function (i, select) {
4
			var input = $('<input/>'),
5
				gravity = $(select).attr('data-tipsy-gravity'),
6
				inputTooltip = $(select).attr('data-inputtitle');
7
			if (inputTooltip){
8
				input.attr('title', inputTooltip);
9
			}
10
			if (typeof gravity === 'undefined') {
11
				gravity = 'n'
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
12
			}
13
			select = $(select);
14
			input.css('position', 'absolute');
15
			input.css({
16
				'box-sizing': 'border-box',
17
				'-moz-box-sizing': 'border-box',
18
				'margin': 0,
19
				'width': (select.width() - 5) + 'px',
20
				'height': (select.outerHeight() - 2) + 'px',
21
				'border': 'none',
22
				'box-shadow': 'none',
23
				'margin-top': '1px',
24
				'margin-left': '1px',
25
				'z-index': 1000
26
			});
27
			input.hide();
28
			$('body').append(input);
29
30
			select.on('change', function (event) {
31
				var value = $(this).val(),
32
					newAttr = $('option:selected', $(this)).attr('data-new');
33
				if (!(typeof newAttr !== 'undefined' && newAttr !== false)) {
34
					input.hide();
35
					select.data('previous', value);
36
				} else {
37
					event.stopImmediatePropagation();
38
					// adjust offset, in case the user scrolled
39
					input.css(select.offset());
40
					input.show();
41
					if ($.fn.tipsy){
42
						input.tipsy({gravity: gravity, trigger: 'manual'});
43
						input.tipsy('show');
44
					}
45
					select.css('background-color', 'white');
46
					input.focus();
47
				}
48
			});
49
50
			$(select).data('previous', $(select).val());
51
52
			input.on('change', function () {
53
				var value = $(this).val();
54
				if (value) {
55
					select.children().attr('selected', null);
56
					var existingOption = select.children().filter(function (i, option) {
57
						return ($(option).val() == value);
58
					});
59
					if (existingOption.length) {
60
						existingOption.attr('selected', 'selected');
61
					} else {
62
						var option = $('<option/>');
63
						option.attr('selected', 'selected').attr('value', value).text(value);
64
						select.children().last().before(option);
65
					}
66
					select.val(value);
67
					select.css('background-color', null);
68
					input.val(null);
69
					input.hide();
70
					select.change();
71
				} else {
72
					var previous = select.data('previous');
73
					select.children().attr('selected', null);
74
					select.children().each(function (i, option) {
75
						if ($(option).val() == previous) {
76
							$(option).attr('selected', 'selected');
77
						}
78
					});
79
					select.removeClass('active');
80
					input.hide();
81
				}
82
			});
83
84
			input.on('blur', function () {
85
				$(this).change();
86
				if ($.fn.tipsy){
87
					$(this).tipsy('hide');
88
				}
89
			});
90
			input.click(function(ev) {
91
				// prevent clicks to close any container
92
				ev.stopPropagation();
93
			});
94
		});
95
	};
96
})(jQuery);
97