summaryrefslogtreecommitdiffstats
path: root/admin/survey/script/jquery/jquery.selectbox-0.6.1/jquery.selectbox-0.6.1.js
blob: 1a1ca5d24951dfc5400788b6a224e296fda13714 (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
 * jQuery custom selectboxes
 * 
 * Copyright (c) 2008 Krzysztof Suszyński (suszynski.org)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * @version 0.6.1
 * @category visual
 * @package jquery
 * @subpakage ui.selectbox
 * @author Krzysztof Suszyński <k.suszynski@wit.edu.pl>
**/
jQuery.fn.selectbox = function(options){
	/* Default settings */
	var settings = {
		className: 'jquery-selectbox',
		animationSpeed: "fast",
		listboxMaxSize: 16,
		replaceInvisible: false,
		sprid: 0,
		spremenljivka_preview: true,
		parent_id: 'jquery-selectbox'
	};
	var commonClass = 'jquery-custom-selectboxes-replaced';
	var listOpen = false;
	var showList = function(listObj) {
		var selectbox = listObj.parents('.' + settings.className + '');
		listObj.slideDown(settings.animationSpeed, function(){
			listOpen = true;
		});
		selectbox.addClass('selecthover');
		jQuery(document).bind('click', onBlurList);
		return listObj;
	}
	var hideList = function(listObj) {
		var selectbox = listObj.parents('.' + settings.className + '');
		listObj.slideUp(settings.animationSpeed, function(){
			listOpen = false;
			jQuery(this).parents('.' + settings.className + '').removeClass('selecthover');
		});
		jQuery(document).unbind('click', onBlurList);
		return listObj;
	}
	var onBlurList = function(e) {
		var trgt = e.target;
		var currentListElements = jQuery('.' + settings.className + '-list:visible').parent().find('*').andSelf();
		if(jQuery.inArray(trgt, currentListElements)<0 && listOpen) {
			hideList( jQuery('.' + commonClass + '-list') );
		}
		return false;
	}
	
	/* Processing settings */
	settings = jQuery.extend(settings, options || {});
	/* Wrapping all passed elements */
	return this.each(function() {
		var _this = jQuery(this);
		if(_this.filter(':visible').length == 0 && !settings.replaceInvisible)
			return;
		if (jQuery(this).attr('spr_id')) { // poseben tip za predogled tipa vprasanja
			settings.spremenljivka_preview = true;
			var replacement = jQuery(
			//		'<div id="spremenljivka_tip_'+jQuery(this).attr('spr_id')+'" class="' + settings.className + ' ' + commonClass + '">' +
					'<div id="'+jQuery(this).attr('id')+'" class="' + settings.className + ' ' + commonClass + '">' +
						'<div class="' + settings.className + '-moreButton" />' +
						'<div class="' + settings.className + '-list ' + commonClass + '-list" />' +
						'<span class="' + settings.className + '-currentItem" />' +
					'</div>'
				);
			//settings.parent_id = 'spremenljivka_tip_'+jQuery(this).attr('spr_id');
			settings.parent_id = jQuery(this).attr('id');
		} else { // normalni nacin dropdown
			settings.spremenljivka_preview = false;
			var replacement = jQuery(
					'<div id="'+jQuery(this).attr('id')+'" class="' + settings.className + ' ' + commonClass + '">' +
						'<div class="' + settings.className + '-moreButton" />' +
						'<div class="' + settings.className + '-list ' + commonClass + '-list" />' +
						'<span class="' + settings.className + '-currentItem" />' +
					'</div>'
				);
			settings.parent_id = jQuery(this).attr('id');
		}
		jQuery('option', _this).each(function(k,v){
			var v = jQuery(v);
			var spr_id = jQuery(this).parents().attr('spr_id');
			var name = jQuery(this).parents().attr('name');

			if ( jQuery(this).attr('disabled') )
				var disabled_string = " option_disabled";
			else
				var disabled_string = "";

			if (settings.spremenljivka_preview)
				var listElement =  jQuery('<span id="'+jQuery(this).parents().attr('spr_id')+'_'+v.val()+'" class="' + settings.className + '-item value-'+v.val()+' item-'+k+'" val="'+v.val()+'">' + v.text() + '</span>');
			else {
				var listElement =  jQuery('<span id="'+jQuery(this).parents().attr('id')+'_'+v.val()+'" class="' + settings.className + '-item value-'+v.val()+' item-'+k+disabled_string+'" val="'+v.val()+'" value="'+v.val()+'">' + v.text() + '</span>');
			}
			if ( !jQuery(this).attr('disabled') ) // ce ni disejblan 
			listElement.click(function(){
				var thisListElement = jQuery(this);
				var thisReplacment = thisListElement.parents('.'+settings.className);
				var thisIndex = thisListElement[0].className.split(' ');
				for( k1 in thisIndex ) {
					if(/^item-[0-9]+$/.test(thisIndex[k1])) {
						thisIndex = parseInt(thisIndex[k1].replace('item-',''), 10);
						break;
					}
				};
				var thisValue = thisListElement[0].className.split(' ');

				for( k1 in thisValue ) {
					if(/^value-.+$/.test(thisValue[k1])) {
						thisValue = thisValue[k1].replace('value-','');
						break;
					}
				};
				thisReplacment
					.find('.' + settings.className + '-currentItem')
					.text(thisListElement.text());
				thisReplacment
					.find('select')
					.val(thisValue)
					.triggerHandler('change');
				var thisSublist = thisReplacment.find('.' + settings.className + '-list');
				if(thisSublist.filter(":visible").length > 0) {
					hideList( thisSublist );
				}else{
					showList( thisSublist );
				};
				jQuery("#"+settings.parent_id).val(thisValue);
				// skrijemo prwview vprasanja
				$("#tip_preview").hide();
			}).bind('mouseenter',function(){
				jQuery(this).addClass('listelementhover');
			}).bind('mouseleave',function(){
				jQuery(this).removeClass('listelementhover');
			}).bind('mouseover',function(){				
				if( (settings.spremenljivka_preview) && (name == 'design') )
					show_tip_preview_subtype(spr_id,v.val(),17); // prikazemo predogled designa pri razvrscanju
				else if( (settings.spremenljivka_preview) && (name == 'sn_design') )
					show_tip_preview_subtype(spr_id,v.val(),9); // prikazemo predogled designa pri sn generatorju imen
				else if( (settings.spremenljivka_preview) && (name == 'orientation') )
					show_tip_preview_subtype(spr_id,v.val(),1); // prikazemo predogled postavitve pri radio buttnih
				else if( (settings.spremenljivka_preview) && (name == 'orientation_checkbox') )
					show_tip_preview_subtype(spr_id,v.val(),2); // prikazemo predogled postavitve pri radio buttnih
				else if( (settings.spremenljivka_preview) && (name == 'enota') )
					show_tip_preview_subtype(spr_id,v.val(),6); // prikazemo predogled postavitve pri multigridu
                else if( (settings.spremenljivka_preview) && (name == 'podtip_lokacija') )
					show_tip_preview_subtype(spr_id,v.val(),26); // prikazemo predogled podtipa pri lokaciji
				else if ( settings.spremenljivka_preview )
					show_tip_preview(spr_id,v.val()); // prikazemo predogled spremenljivke
			}).bind('mouseout',function(){
//				if (settings.spremenljivka_preview)
					$("#tip_preview").hide();
			});
			jQuery('.' + settings.className + '-list', replacement).append(listElement);
			if(v.filter(':selected').length > 0) {
				jQuery('.'+settings.className + '-currentItem', replacement).text(v.text());
			}
		});
		replacement.find('.' + settings.className + '-moreButton').click(function(){
			var thisMoreButton = jQuery(this);
			var otherLists = jQuery('.' + settings.className + '-list')
				.not(thisMoreButton.siblings('.' + settings.className + '-list'));
			hideList( otherLists );
			var thisList = thisMoreButton.siblings('.' + settings.className + '-list');
			if(thisList.filter(":visible").length > 0) {
				hideList( thisList );
			}else{
				showList( thisList );
			}
		}).bind('mouseenter',function(){
			jQuery(this).addClass('morebuttonhover');
		}).bind('mouseleave',function(){
			jQuery(this).removeClass('morebuttonhover');
		});
		_this.hide().replaceWith(replacement).appendTo(replacement);
		var thisListBox = replacement.find('.' + settings.className + '-list');
		var thisListBoxSize = thisListBox.find('.' + settings.className + '-item').length;
		if(thisListBoxSize > settings.listboxMaxSize)
			thisListBoxSize = settings.listboxMaxSize;
		if(thisListBoxSize == 0)
			thisListBoxSize = 1;	
		var thisListBoxWidth = Math.round(_this.width() + 5);
		if(jQuery.browser.safari)
			thisListBoxWidth = thisListBoxWidth * 0.94+10;
		replacement.css('width', thisListBoxWidth + 'px');
		thisListBox.css({
			width: Math.round(thisListBoxWidth-5) + 'px',
			height: Math.round(thisListBoxSize*15.5)+ 'px'
		});
	});
}
jQuery.fn.unselectbox = function(){
	var commonClass = 'jquery-custom-selectboxes-replaced';
	return this.each(function() {
		var selectToRemove = jQuery(this).filter('.' + commonClass);
		selectToRemove.replaceWith(selectToRemove.find('select').show());		
	});
}