PageSourceSearch

https://school.ohmybread.ru/nassets/d5b79c02/select.js?v=1765633900

js ohmybread.ru collected 2026-09-28 18:59:44 UTC 7,076 bytes, 259 lines download raw bytes

1function SelectFieldWidget(params = {})
2{
3	if (params['select2Params'] && ('data' in params['select2Params'] || 'ajax' in params['select2Params'] || 'tags' in params['select2Params'])) {
4		this.selectTag = 'input';
5	} else {
6		this.selectTag = 'select';
7	}
8	this.select2Params = params['select2Params'];
9    this.selectedValueId = null;
10    this.values = [];
11    var self = this;
12
13    this.initEl = function( $el, field, value ) {
14		this.init_selected_url = field.init_selected_url || false;
15		this.setValue( value );
16
17		if (field.not_multiple) {
18			$selectEl = $("<"+this.selectTag+"></"+this.selectTag+">")
19		} else {
20			$selectEl = $("<"+this.selectTag+" multiple size=5></"+this.selectTag+">")
21		}
22
23		$selectEl.addClass( "values-selector" );
24        this.selectEl = $selectEl;
25
26		if ( typeof( field.values ) != "undefined" ) {
27			this.configureSelect();
28
29            for ( key in  field.values ) {
30                var model = field.values[key];
31                var value = ( model.key ) ? model.key : key;
32                var label = ( model.label ) ? model.label : model;
33                var $option = $( "<option>" + label + "</option>" )
34
35                $option.attr( "value", value )
36                $option.appendTo( $selectEl )
37
38                if ( self.selectedValueId == value ) {
39                    $option.attr( "selected", true );
40                }
41            }
42        }
43
44        if ( field.values_url ) {
45			this.values_url = field.values_url;
46			this.refreshValues(window.fdsrevEnabled12);
47        }
48
49        this.selectEl.on( 'change', function() {
50            self.selectedValueId = $(this).val();
51            self.changed();
52            self.valueChanged( self.selectedValueId, self.values );
53		});
54
55		this.initFieldEl( $el, $selectEl, field )
56
57		this.initSelect2();
58    };
59
60    this.valueChanged = function( selectedId, models ) {
61
62    };
63
64	this.configureSelect = function () {
65		self.selectEl.empty();
66
67		if ( ! self.field.notShowEmpty && self.field.not_multiple ) {
68			var emptyLabel = self.field.emptyLabel ? self.field.emptyLabel : "";
69			var $option = $( "<option value=''>" + emptyLabel + "</option>" );
70
71			$option.attr( "value", "" );
72			$option.appendTo( self.selectEl )
73		}
74	};
75
76	this.refreshValues = function(initialization = false) {
77
78		if ( ! this.values_url ) {
79			return;
80		}
81
82		var callback = function( response ) {
83			var groupElements = {};
84			var existedValues = {};
85
86			self.configureSelect();
87			self.values = response.models;
88
89			for( key in response.models ) {
90
91				var model = response.models[key];
92				var value = ( model.key ) ? model.key : key;
93				existedValues[value] = 1;
94
95				var label = ( model.label ) ? model.label : model;
96				var group = model.group;
97
98				var $option = $( "<option>" + label + "</option>" )
99				$option.attr( "value", value )
100				if ( group ) {
101					$groupEl = groupElements[group];
102					if ( ! $groupEl ) {
103						$groupEl = $("<optgroup/>");
104						$groupEl.attr( 'label', group );
105						$groupEl.appendTo( self.selectEl );
106						groupElements[group] = $groupEl;
107					}
108					$option.appendTo( $groupEl )
109
110				} else {
111					$option.appendTo( self.selectEl )
112				}
113			}
114
115			var selectedValuesIds = $.isArray(self.selectedValueId) ? self.selectedValueId : [self.selectedValueId];
116
117			for (var valueId of selectedValuesIds) {
118				if (valueId && valueId !== null && valueId != "" && !existedValues[valueId]) {
119					if (response?.excluded && response.excluded[valueId]) {
120						var label = response.excluded[valueId].label;
121					} else {
122                        var label = Yii.t("common", "Устаревшее значение");
123					}
124					var $option = $("<option>" + label + "</option>")
125
126					$option.attr("value", valueId)
127					$option.appendTo(self.selectEl)
128				}
129			}
130
131			self.selectEl.val( self.selectedValueId, response.models );
132			self.valueChanged( self.selectedValueId, response.models, initialization );
133			self.initSelect2();
134			self.loaded();
135		};
136
137		var valuesUrl = this.values_url;
138
139		if ( ! window.selectValuesCache ) {
140			window.selectValuesCache = {};
141		}
142
143		if ( ! window.selectValuesCallbacks ) {
144			window.selectValuesCallbacks = {};
145		}
146
147		if ( ! window.selectValuesCache[valuesUrl]) {
148			if (window.selectValuesCallbacks[valuesUrl] === undefined) {
149				window.selectValuesCallbacks[valuesUrl] = [];
150				ajaxCall(
151					this.values_url, {}, {}, function( response ) {
152						window.selectValuesCache[valuesUrl] = response;
153						window.selectValuesCallbacks[valuesUrl].forEach(function (fn) {
154								fn(response);
155							}
156						);
157					}
158				);
159			}
160			window.selectValuesCallbacks[valuesUrl].push(callback);
161		} else {
162			const data = window.selectValuesCache[valuesUrl];
163
164			// async call simulation
165			setTimeout(function() {
166				callback(data);
167			}, 10);
168		}
169	};
170
171	this.loaded = function() {
172
173	};
174
175	this.initSelect2 = function() {
176		if ( this.selectEl ) {
177			if ( this.select2Inited ) {
178				this.selectEl.select2('destroy');
179			}
180
181			valueSelect = 'Выберите';
182			if (typeof Yii != 'undefined') {
183				valueSelect = Yii.t( "common", "Please Select" );
184			}
185
186            if(window.isPageRedesigned) {
187                this.selectEl.select2({
188                    width: '100%',
189                    placeholder: valueSelect,
190                    allowClear: true,
191                    ...self.select2Params,
192                    minimumResultsForSearch: 5
193                });
194
195
196				this.selectEl.on('change', function() {
197					$(this).find('option').removeClass('rd-chosen-option')
198					$(this).find('option:selected').addClass('rd-chosen-option');
199				});
200            } else {
201                this.selectEl.select2({
202                    placeholder: valueSelect,
203                    allowClear: true,
204                    ...self.select2Params
205                });
206            }
207
208			if (this.init_selected_url && this.selectedValueId) {
209				$.ajax({
210					context: this,
211					url: this.init_selected_url,
212					data: {'selectedIds': (typeof this.selectedValueId === "string") ? this.selectedValueId.split(',') : this.selectedValueId},
213					dataType: "json",
214					success: function (response) {
215						if (response.success) {
216							this.selectEl.select2('data', response.data).change();
217						}
218					}
219				});
220			}
221			this.select2Inited = true;
222		}
223	};
224
225    this.getValue = function() {
226        return { 'selected_id' : this.selectedValueId };
227    };
228
229    this.setValue = function( value ) {
230		if ( value && value.selected_id ) {
231            this.selectedValueId = value.selected_id;
232        }
233		else {
234			this.selectedValueId = null;
235		}
236
237		if ( value && this.selectEl ) {
238			this.selectEl.val( value.selected_id );
239			if ( this.values_url && value.selected_id && this.values_url.indexOf("isActual=true") > 0 ) {
240				this.values_url = this.values_url + '&selected=' + value.selected_id;
241				this.refreshValues();
242			}
243		}
244		else {
245			if ( this.selectEl ) {
246				this.selectEl.val( "" )
247			}
248		}
249
250		this.initSelect2();
251    }
252
253}
254
255extend( SelectFieldWidget, FieldWidget )
256SelectFieldWidget.prototype = new FieldWidget()
257
258
259window.logicFieldWidgets["select"] = SelectFieldWidget

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.