PageSourceSearch

https://community-tu.org/wp-content/themes/Community/js/shared.js?1790920170

js community-tu.org collected 2026-10-02 06:17:11 UTC 11,412 bytes, 311 lines download raw bytes

1function loadSelectOptions(elem, type, ref, async, textProp, valueProp, sort, forceValue) {
2
3    $ = jQuery;
4
5    if(!$(elem).siblings(".loading-icon").length)
6        $(elem).hide().after($("<i/>", { class: "loading-icon fas fa-spinner fa-spin fa-2x" })); 
7
8    $.ajax({ 
9        url: apiURL + "?type=" + type + "&reference=" + ref,
10        method: "get",
11        async: typeof async == "boolean" ? async : true                          
12    })
13    .done(function(response) {
14       
15         $(elem).empty().siblings(".otherinput").remove();
16        
17        var data = JSON.parse(response);
18        
19        if(sort) {
20            switch(sort) {
21                case "alphabetically":
22                    data.sort(function (a, b) { a[textProp || (type == "endpoint" ? "Name" : "name")].toLowerCase().localeCompare(b[textProp || (type == "endpoint" ? "Name" : "name")].toLowerCase()) });
23                    break;
24            }
25        } 
26                
27        var otherValue = "Other";
28        
29        $(elem).append($("<option\>", { value: "", disabled: "", selected: "", text: "Select..." }));
30        
31        data.forEach(function(option){
32            var label = option[textProp || (type == "endpoint" ? "Name" : "name")],
33                value = option[valueProp || (type == "endpoint" ? "Id" : "value")];			
34			
35            if(label.toLowerCase() == "other")
36                otherValue = value;
37            else if(label.toLowerCase() != "unknown" &&
38                (
39                    (!$(elem).data("ignore") && !$(elem).data("showonly")) 
40                    || ($(elem).data("ignore") && $(elem).data("ignore").split(",").indexOf(label.toLowerCase()) == -1)
41                    || ($(elem).data("showonly") && $(elem).data("showonly").split(",").indexOf(label.toLowerCase()) > -1)
42                ) 
43            ) {				
44				var $option = $("<option\>", { value: value, text: label, "data-reference": option.reference, "data-related": option.related })
45                $(elem).append($option);
46				
47				var hideConditions = option.hasOwnProperty("hidden") ? option.hidden.split("|").map(function(item) { 
48					var splitted = item.split(":");
49					return { name: splitted[0], values: splitted[1] };                
50				}) : [];
51				
52				if(hideConditions.length > 0) {
53					hideConditions.forEach(function(cond) {
54						$("[name='" + cond.name + "']").filter("select, :radio").on("change.hideOption", function() {
55							var value = $(this).is(":disabled") ? "" : ($(this).is("select") ? ($(this).find(":checked").data("reference") || $(this).find(":checked").val()) : ($(this).is(":radio") ? ($(this).filter(":checked").data("reference") || $(this).filter(":checked").val()) : ($(this).data("reference") || $(this).val()))),                       
56								hide = hideConditions.some(function(c) { return c.values.split(",").indexOf(value) > -1 }) && typeof value != "undefined";
57
58							if(hide) {
59								$(elem).find("option:first").prop("selected", true); // select "Select..."
60								$option.hide();
61							} else {
62								$option.show();
63							}
64						});
65					});
66				}
67			}
68        });       
69
70        // other
71        if(!$(elem).is("[data-noother]")) {
72            $(elem).append($("<option\>", { value: otherValue, text: "Other", "data-reference": "other_" + ref })).on("change.other", function() {
73                var $input = $(this).next("input.otherinput"),
74                    isOther = $(this).find(":checked").data("reference") == "other_" + ref;
75
76                $input.toggle(isOther);
77                if(isOther && !$(elem).is("[data-otherfield]")) 
78                    $(elem).find(":checked").attr("value", $input.val()); 
79            });
80
81            if($(elem).siblings(".otherinput").length == 0) {
82                var $inputOther = $("<input\>", { class: "otherinput", type: "text", placeholder: "please specify...", name: $(elem).is("[data-otherfield]") ? $(elem).data("otherfield") : "" });
83                $inputOther.on("keyup.other", function() {
84                    if($(elem).find(":checked").data("reference") == "other_" + ref && !$(elem).is("[data-otherfield]"))
85                        $(elem).find(":checked").attr("value", $(this).val());
86                });
87                $(elem).after($inputOther);
88            }
89        }   
90
91        $(elem)
92            .data("dataloaded", ref)
93            .show().trigger("change")
94            .siblings(".loading-icon").remove();
95        
96        if (forceValue)
97            $(elem).val(forceValue).trigger("change");
98        
99        $(elem).find("[disabled]").removeAttr("disabled"); // fix for trigger not showing error
100    }) 
101    .fail(function() {
102        $(elem).empty().siblings(".otherinput").remove();
103        console.log("error loading select options");
104    })
105    .always(function() { 
106
107    });
108}
109
110function loadRadioOptions(elem, type, reference, async, textProp, valueProp, sort) { 
111
112    $ = jQuery;
113
114    $(elem).empty().siblings("otherinput").hide();
115
116    if(!$(elem).siblings(".loading-icon").length) 
117        $(elem).hide().after($("<i/>", { class: "loading-icon fas fa-spinner fa-spin fa-2x" }));
118
119    $.ajax({ 
120        url: apiURL + "?type=" + type + "&reference=" + reference,
121        method: "get", 
122        async: typeof async == "boolean" ? async : true                          
123    })
124    .done(function(response) {
125        var data = JSON.parse(response);
126        
127        if(sort) {
128            switch(sort) {
129                case "alphabetically":
130                    data.sort(function (a, b) { a[textProp || (type == "endpoint" ? "Name" : "name")].toLowerCase().localeCompare(b[textProp || (type == "endpoint" ? "Name" : "name")].toLowerCase()) });
131                    break;
132            }
133        } 
134        
135        data.forEach(function(radio) {
136            var label = radio[textProp || (type == "endpoint" ? "Name" : "name")],
137                value = radio[valueProp || (type == "endpoint" ? "Id" : "value")];
138            
139            if(
140                (!$(elem).data("ignore") && !$(elem).data("showonly")) 
141                || ($(elem).data("ignore") && $(elem).data("ignore").split(",").indexOf(label.toLowerCase()) == -1)
142                || ($(elem).data("showonly") && $(elem).data("showonly").split(",").indexOf(label.toLowerCase()) > -1)
143            ) {
144                
145                var replaceLabels = $(elem).data("replace-labels") || {};
146                $(elem).append(
147                    $("<label/>", { class: "radio-container", text: replaceLabels[label] || label })
148                    .append($("<input/>", { type: "radio", name: $(elem).data("name"), value: value, "data-reference": radio.reference, "data-related": radio.related }))
149                    .append($("<span/>", { class: "checkmark" }))
150                );
151            }
152        });
153        $(elem).siblings(".loading-icon").remove();
154        $(elem).show().trigger("change"); 
155    }) 
156    .fail(function() { 
157        console.log("error loading radio options");
158    })
159    .always(function() {
160
161    });
162}
163
164jQuery.fn.serializeObject = function(){
165    
166    $ = jQuery;
167    
168    var self = this,
169        json = {},
170        push_counters = {},
171        patterns = {
172            "validate": /^[a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,
173            "key":      /[a-zA-Z0-9_]+|(?=\[\])/g,
174            "push":     /^$/,
175            "fixed":    /^\d+$/,
176            "named":    /^[a-zA-Z0-9_]+$/
177        };
178
179
180    this.build = function(base, key, value){ 
181        base[key] = value;
182        return base;
183    };
184
185    this.push_counter = function(key){
186        if(push_counters[key] === undefined){
187            push_counters[key] = 0;
188        }
189        return push_counters[key]++;
190    };
191
192    $.each($(this).serializeArray(), function(){
193
194        // Skip invalid keys
195        if(!patterns.validate.test(this.name)){ 
196            return;
197        }
198
199        var k,
200            keys = this.name.match(patterns.key),
201            merge = this.value,
202            reverse_key = this.name;
203
204        while((k = keys.pop()) !== undefined){
205
206
207            // Adjust reverse_key
208            reverse_key = reverse_key.replace(new RegExp("\\[" + k + "\\]$"), '');
209
210            // Push
211            if(k.match(patterns.push)){
212                merge = self.build([], self.push_counter(reverse_key), merge);
213            }
214
215            // Fixed
216            else if(k.match(patterns.fixed)){
217                merge = self.build([], k, merge);
218            }
219
220            // Named
221            else if(k.match(patterns.named)){
222                merge = self.build({}, k, merge);
223            }
224        }
225
226        json = $.extend(true, json, merge);
227    });
228
229    return json;
230};
231
232if (!String.prototype.startsWith) {
233    Object.defineProperty(String.prototype, 'startsWith', {
234        value: function(search, rawPos) {
235            var pos = rawPos > 0 ? rawPos|0 : 0;
236            return this.substring(pos, pos + search.length) === search;
237        }
238    });
239}
240
241// encode e decode de Base64
242function b64EncodeUnicodeURLSafe(str) {
243    // first we use encodeURIComponent to get percent-encoded UTF-8,
244    // then we convert the percent encodings into raw bytes which
245    // can be fed into btoa.
246    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
247        function toSolidBytes(match, p1) {
248            return String.fromCharCode('0x' + p1);
249        })).replace(/\//g, '_').replace(/\+/g, '-');
250}
251
252function b64DecodeUnicodeURLSafe(str) { 
253    // Going backwards: from bytestream, to percent-encoding, to original string.
254    return decodeURIComponent(atob(str.replace(/_/g, '/').replace(/-/g, '+')).split('').map(function(c) {
255        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
256    }).join(''));
257}
258
259function setCookie(cname, cvalue, seconds) {
260  const d = new Date();
261  d.setTime(d.getTime() + (seconds*1000));
262  let expires = "expires="+ d.toUTCString();
263  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
264}
265
266function getCookie(cname) {
267  let name = cname + "=";
268  let ca = document.cookie.split(';');
269  for(let i = 0; i < ca.length; i++) {
270    let c = ca[i];
271    while (c.charAt(0) == ' ') {
272      c = c.substring(1);
273    }
274    if (c.indexOf(name) == 0) {
275      return c.substring(name.length, c.length);
276    }
277  }
278  return "";
279}
280
281function checkCookie(cname) {
282  return getCookie(cname) != ""; 
283}
284
285function strToBool(s) {
286    // will match one and only one of the string 'true','1', or 'on' rerardless
287    // of capitalization and regardless off surrounding white-space.
288    //
289    regex=/^\s*(true|1|on)\s*$/i
290
291    return regex.test(s);
292}
293
294function loading(action, containerID) {
295    
296    var options = {
297        'overlayBackgroundColor': '#ffffff',
298        'overlayOpacity': 0.6,
299        'spinnerIcon': 'ball-spin-clockwise-fade',
300        'spinnerColor': '#bf1e74',
301        'spinnerSize': '2x',
302        'overlayIDName': 'loading-overlay',
303        'spinnerIDName': 'spinner-loading-overlay',
304        'offsetY': 0,
305        'offsetX': 0,
306        'lockScroll': false,
307        'containerID': containerID,
308    }
309    
310    JsLoadingOverlay[action](options);
311}

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.