PageSourceSearch

https://actionprgroup.com/wp-content/plugins/cred-frontend-editor/…/toolset-select2-compatibility.js?ver=4.4.6

js actionprgroup.com collected 2026-09-29 10:04:52 UTC 5,961 bytes, 185 lines download raw bytes

1/*
2 * This module is for hijacking toolset_select2 4.0 prototype to make it compatible with toolset_select2 2.0 method calling
3 */
4
5var ToolsetCommon = ToolsetCommon || {};
6
7ToolsetCommon.initSelect2Compatibility = function( $ ){
8	if(!jQuery().toolset_select2)
9		return
10	//preserve original toolset_select2 object
11	jQuery.fn.toolset_select2_original = jQuery.fn.toolset_select2;
12	//backwards compatible object
13	jQuery.fn.toolset_select2  = function(options, param){
14		if( typeof options == "string" ){
15			ToolsetCommon.toolset_select2ExecMethods(this, options, param)
16		}else if (typeof options == "object"){
17			if(jQuery(this)){
18				return ToolsetCommon.toolset_select2ConvertInputToSelect(jQuery(this), options);
19			}
20		}else if(options === null || options === undefined){
21			if(jQuery(this)){
22				return ToolsetCommon.toolset_select2ConvertInputToSelect(jQuery(this));
23			}
24		}
25	};
26};
27/*
28 * @description convert ordinary inputs to select elements, and apply toolset_select2 to them.
29 */
30ToolsetCommon.toolset_select2ConvertInputToSelect = function(el, options){
31	if(ToolsetCommon.toolset_select2ConversionRequired(el, options) && typeof el[0] != "string" && el[0] !== undefined){
32		//check if the element isn't previously initailized to prevent duplicate elements
33		if(jQuery(el).data("toolset_select2"))
34			return el;
35
36		//create a select element to replace the normal input
37		var convertedEl = jQuery("<select></select>");
38
39		//if is a tags field create a hidden input to hold value for backwards compatibility with v2
40		if(options && options.hasOwnProperty("tags") && options.tags){
41			var hiddenInput = jQuery("<input type='hidden' />");
42		}
43
44		//copy all attributes to the new select element
45		jQuery.each(jQuery(el).get(0).attributes, function() {
46		    if(hiddenInput && this.specified && (this.name == "name" || this.name == "class")){
47		    	jQuery(hiddenInput).attr(this.name, this.value);
48		    }
49		    if(this.specified && this.name != "type") {
50		      jQuery(convertedEl).attr(this.name, this.value);
51		    }
52		});
53
54		//create dynamicClass with toolset_select2 prefix and add to the new element to be able to reference the element in the future
55		var dynamicClass = ToolsetCommon.addSelect2RandomClassName(convertedEl);
56		var hiddenDynamicClass = ToolsetCommon.addSelect2RandomClassName(hiddenInput);
57
58		//remove the old input and replace it with the new select
59		jQuery(el).replaceWith(convertedEl);
60		jQuery(el).remove();
61
62		//Insert the hidden input after the select
63		if(hiddenInput){
64			jQuery(hiddenInput).insertAfter("."+dynamicClass);
65		}
66		//initialize toolset_select2
67		try{
68			convertedEl = jQuery("."+dynamicClass).toolset_select2_original(options);
69		}catch(err){
70			console.log(err.message);
71		}
72
73		//Add event listener on tags fields to update hidden inputs on change
74		if(options && options.hasOwnProperty("tags")){
75			jQuery("."+dynamicClass).on("change", function(event){
76				var actualValue = jQuery(event.target).val();
77				if(actualValue && actualValue.length > 0 && hiddenInput){
78					jQuery("."+hiddenDynamicClass).attr("value", actualValue.join(","));
79				}
80			});
81		}
82		return convertedEl;
83	}else{
84		if(!jQuery(el).data("toolset_select2")){
85			var dynamicClass = ToolsetCommon.addSelect2RandomClassName(el);
86			var convertedEl = null;
87
88			try{
89				convertedEl = jQuery("."+dynamicClass).toolset_select2_original(options);
90			}catch(err){
91				console.log(err.message);
92			}
93
94			return convertedEl;
95		}else{
96			return el;
97		}
98	}
99};
100/*
101 * @description checks if input needs to be converted to a select element.
102 */
103ToolsetCommon.toolset_select2ConversionRequired = function(el, options){
104	if(!jQuery(el).hasClass("toolset_select2_converted")){
105		if(options && options.hasOwnProperty("tags")){
106			jQuery(el).prop("multiple", "multiple");
107			options.multiple = true;
108			if(options.tags instanceof Array && options.tags.length > 0){
109				options.data = [];
110				options.tags.forEach(function(item){
111					options.data.push({
112						id: item,
113						text: item
114					});
115				});
116				options.tags = true;
117			}
118			return true;
119		}else{
120			return (jQuery(el).prop("tagName") !== "SELECT");
121		}
122	}
123};
124
125/*
126 * @description executes toolset_select2 methods after filtering deprecated ones, and applying compatible replacement.
127 */
128
129ToolsetCommon.toolset_select2ExecMethods = function(el, method, param){
130	if(jQuery(el).data("toolset_select2")){
131		var elm_id = jQuery(el).attr("id");
132		try{
133			switch(method){
134				case "val":
135					if(param !== undefined && param !== null){
136						jQuery(el).val(param).trigger("change");
137					}else{
138						return jQuery(el).val();
139					}
140					break;
141				case "enable":
142					jQuery(el).prop("disabled", !param);
143					break;
144				case "data":
145					jQuery(el).val(param.ID).trigger("change").trigger("toolset_select2:selecting");
146					break;
147				case "close":
148					if(jQuery(el).data("toolset_select2") != null && jQuery(el).data("toolset_select2") != undefined){
149						jQuery(el).toolset_select2_original("close");
150					}
151					break;
152				case "open":
153					if(jQuery(el).data("toolset_select2") != null && jQuery(el).data("toolset_select2") != undefined){
154						jQuery(el).toolset_select2_original("open");
155					}
156					break;
157				case "destroy":
158				case "destory":
159					jQuery(el).removeClass("toolset_select2_converted");
160					jQuery(el).toolset_select2_original("destroy");
161					break;
162				default:
163					jQuery("#"+elm_id).toolset_select2_original(method, param);
164					break;
165			}
166		}catch(err){
167
168		}
169	}
170};
171
172/*
173 * @description creates and adds dynamic class name to the element.
174 */
175ToolsetCommon.addSelect2RandomClassName = function(el) {
176    var className = ("toolset_select2_prefix_" + (Math.round(Math.random() * (100000 - 99) + 99)).toString());
177    jQuery(el).addClass(className);
178    jQuery(el).addClass("toolset_select2_converted");
179    return className;
180};
181
182/*
183 * @description start the compatibility listener on document ready.
184 */
185jQuery(function($){ ToolsetCommon.initSelect2Compatibility(); });

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.