Blame view

sources/core/js/singleselect.js 2.6 KB
03e52840d   Kload   Init
1
2
3
  (function ($) {
  	$.fn.singleSelect = function () {
  		return this.each(function (i, select) {
31b7f2792   Kload   Upgrade to ownclo...
4
  			var input = $('<input/>'),
f7d878ff1   kload   [enh] Update to 7...
5
  				gravity = $(select).attr('data-tipsy-gravity'),
6d9380f96   Cédric Dupont   Update sources OC...
6
  				inputTooltip = $(select).attr('data-inputtitle');
31b7f2792   Kload   Upgrade to ownclo...
7
8
9
  			if (inputTooltip){
  				input.attr('title', inputTooltip);
  			}
f7d878ff1   kload   [enh] Update to 7...
10
11
12
  			if (typeof gravity === 'undefined') {
  				gravity = 'n'
  			}
03e52840d   Kload   Init
13
14
  			select = $(select);
  			input.css('position', 'absolute');
03e52840d   Kload   Init
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  			input.css({
  				'box-sizing': 'border-box',
  				'-moz-box-sizing': 'border-box',
  				'margin': 0,
  				'width': (select.width() - 5) + 'px',
  				'height': (select.outerHeight() - 2) + 'px',
  				'border': 'none',
  				'box-shadow': 'none',
  				'margin-top': '1px',
  				'margin-left': '1px',
  				'z-index': 1000
  			});
  			input.hide();
  			$('body').append(input);
  
  			select.on('change', function (event) {
  				var value = $(this).val(),
  					newAttr = $('option:selected', $(this)).attr('data-new');
  				if (!(typeof newAttr !== 'undefined' && newAttr !== false)) {
  					input.hide();
  					select.data('previous', value);
  				} else {
  					event.stopImmediatePropagation();
31b7f2792   Kload   Upgrade to ownclo...
38
39
  					// adjust offset, in case the user scrolled
  					input.css(select.offset());
03e52840d   Kload   Init
40
  					input.show();
31b7f2792   Kload   Upgrade to ownclo...
41
  					if ($.fn.tipsy){
f7d878ff1   kload   [enh] Update to 7...
42
  						input.tipsy({gravity: gravity, trigger: 'manual'});
31b7f2792   Kload   Upgrade to ownclo...
43
44
  						input.tipsy('show');
  					}
03e52840d   Kload   Init
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
  					select.css('background-color', 'white');
  					input.focus();
  				}
  			});
  
  			$(select).data('previous', $(select).val());
  
  			input.on('change', function () {
  				var value = $(this).val();
  				if (value) {
  					select.children().attr('selected', null);
  					var existingOption = select.children().filter(function (i, option) {
  						return ($(option).val() == value);
  					});
  					if (existingOption.length) {
  						existingOption.attr('selected', 'selected');
  					} else {
  						var option = $('<option/>');
  						option.attr('selected', 'selected').attr('value', value).text(value);
  						select.children().last().before(option);
  					}
  					select.val(value);
  					select.css('background-color', null);
  					input.val(null);
  					input.hide();
  					select.change();
  				} else {
  					var previous = select.data('previous');
  					select.children().attr('selected', null);
  					select.children().each(function (i, option) {
  						if ($(option).val() == previous) {
  							$(option).attr('selected', 'selected');
  						}
  					});
  					select.removeClass('active');
  					input.hide();
  				}
  			});
  
  			input.on('blur', function () {
  				$(this).change();
31b7f2792   Kload   Upgrade to ownclo...
86
87
88
  				if ($.fn.tipsy){
  					$(this).tipsy('hide');
  				}
03e52840d   Kload   Init
89
  			});
f7d878ff1   kload   [enh] Update to 7...
90
91
92
93
  			input.click(function(ev) {
  				// prevent clicks to close any container
  				ev.stopPropagation();
  			});
03e52840d   Kload   Init
94
  		});
6d9380f96   Cédric Dupont   Update sources OC...
95
  	};
03e52840d   Kload   Init
96
  })(jQuery);