PageSourceSearch

https://www.blu-digital.co.uk/app/public/js/backend/function.js

js blu-digital.co.uk collected 2026-10-07 19:27:15 UTC 24,877 bytes, 846 lines download raw bytes

1"use strict";
2
3let ajaxUrl,
4    files,
5    intervals = [],
6    ajaxVars = {},
7    ajaxScreens = {
8        main: '#content_place'
9    },
10    GLOBAL_URL = ''; // GLOBAL_URL
11
12
13function clearFiles() { files = null; }
14
15$.fn.hasAttr = function (name) {
16    return this.attr(name) !== undefined;
17};
18
19$(function () {
20    // Cancel transitions
21    $("body").on('click', 'a', function (e) {
22        console.log('Cancel transitions');
23        if ($(this).hasAttr('onclick')) {
24            e.preventDefault();
25        }
26        //if (!$(this).hasClass('nob'))
27    });
28});
29
30
31window.addEventListener('popstate', function(event) {
32    // back to prev page
33    load(window.location.pathname.replace('/amsource', ''));
34    // history.pushState(null, null, window.location.pathname);
35});
36
37
38let timeStatus = 0; // ??? test
39
40function load(url = null, ...fileds) {
41    let data = new FormData();
42    let type = 'POST';
43    let sysFunctions = {};
44
45    // console.log('$(this)');
46    // console.log(this);
47
48
49    // Files
50    let fieldOfFiles = $("#files");
51    if (typeof fieldOfFiles[0] != "undefined")
52        files = fieldOfFiles[0].files;
53
54    $.each(files, function (key, value) {
55        data.append(key, value);
56        // data[key] = value;
57    });
58
59
60    // #text - значення input з id='text'
61    // .text - значення input з class='text'
62    // age=25 - передаємо ключ та значення
63    // name#field - передаємо ключ та значення форми ( ключ=name, значення=field )
64    // !name#el - передаємо ключ та значення елементу(не input, ex: <p id="el">Text</p>)
65    // form:#login - передаємо форму з id='login'
66    // json:{} - передаємо json // TODO ...
67    // *SYS_OPTION* - системна опція для load() ex: *move*
68
69
70    let _SPIN_LOAD_ = false; // add "this," to load() for spin-load indicator
71
72
73    for (let i = 0; i < fileds.length; i++) {
74
75        if (typeof fileds[i] === "object") {
76            console.log('OBJECT');
77            _SPIN_LOAD_ = fileds[i];
78            $(fileds[i]).addClass('spin--load');
79            continue;
80        }
81
82        if (fileds[i].charAt(0) === '#' || fileds[i].charAt(0) === '.') {
83            data.append(fileds[i], $(fileds[i]).val());
84        } else {
85
86            // if (fileds[i].indexOf('=') >= 0) {
87            if (/^\w{1,32}=(.*?)$/i.test(fileds[i])) {
88                let arr = fileds[i].split('=');
89                data.append(arr[0], arr[1]);
90            } else {
91                if (/^!?\w{1,32}#(.*?)$/i.test(fileds[i])) { // name#field
92                    let arr = fileds[i].split('#');
93                    if (arr[0].charAt(0) === '!')
94                        data.append(arr[0].replace('!', ''), $('#'+arr[1]).text());
95                    else
96                        data.append(arr[0], $('#'+arr[1]).val());
97
98                } else if (/^!?\w{1,32}\.(.*?)$/i.test(fileds[i])) { // name.field
99                    let arr = fileds[i].split('.');
100                    if (arr[0].charAt(0) === '!')
101                        data.append(arr[0].replace('!', ''), $('.'+arr[1]).text());
102                    else
103                        data.append(arr[0], $('.'+arr[1]).val());
104
105                } else if (/^\*(.*?)\*$/i.test(fileds[i])) { // *SYS_OPTION*
106                    let arr = fileds[i].split('=');
107                    sysFunctions[arr[0]] = (arr[1]) ? arr[1] : true;
108
109                } else if ('url:url' === (fileds[i])) { // url:url curent page url
110                    data.append('_url', window.location.href);
111
112                } else {
113                    // form serialize
114                    if (/^form:#(.*?)$/i.test(fileds[i])) {
115                        let arr = fileds[i].split('#');
116                        let elements = document.forms[arr[1]].elements;
117                        // console.log($('#' + arr[1]).serialize());
118
119                        for (let i = 0; i < elements.length; i++){
120                            let formField = $(elements[i]);
121
122                            if (formField[0].type === 'radio' || formField[0].type === 'checkbox') {
123                                if (formField[0].checked === true) {
124                                    data.append(formField.attr("name"), formField[0].value);
125                                }
126                            } else {
127                                data.append(formField.attr("name"), formField.val());
128                            }
129                        }
130                    }
131                    // console.log(fileds[i]);
132                }
133            }
134        }
135    }
136
137
138    function isSysFunction(key) {
139        if (typeof sysFunctions[key] !== "undefined")
140            return true;
141        return false;
142    }
143
144    let startMoveTime = 0; // time of the animation start
145    if (isSysFunction('*move*')) {
146        startMoveTime = new Date().getTime(); // time of the animation start
147        movePage();
148    }
149
150
151    let contentType = false;
152    if (/Edge/.test(navigator.userAgent)) {
153        // contentType = "application/x-www-form-urlencoded"; // [-----------------------------7e314734a0746 Content-Disposition:_form-data;_name] => "email" [email protected]
154        // contentType = "multipart/form-data;"; // Missing boundary in multipart/form-data POST data in <b>Unknown</b> on line <b>0</b>
155        // contentType = "application/json; charset=utf-8;"; // empty
156        // contentType = "multipart/form-data; charset=utf-8; boundary=" + Math.random() .toString().substr(2); // empty
157    }
158
159    $.ajax({
160        url:         site_url + trim(url, '/'),
161        type:        type,
162        data:        data,
163        cache:       false,
164        dataType:    'json',
165        processData: false,
166        contentType: contentType,
167
168
169        success: function (result) {
170            if (_SPIN_LOAD_ !== false) {
171                $(_SPIN_LOAD_).removeClass('spin--load');
172            }
173
174
175            if (result.error == false) {
176                //prev page
177                ajaxUrl = window.location.href;
178
179                // console.log(ajaxUrl);
180
181                let attrID = false;
182                let timeoutCountdown = 0;
183                if (isSysFunction('*move*')) {
184                    attrID = ajaxScreens.main; // set attrID
185                    let moveTime = new Date().getTime() - startMoveTime;
186                    if (moveTime > 400) moveTime = 400;
187                    timeoutCountdown = 400 - moveTime; // count wait time
188                }
189
190                setTimeout(function () {
191                    // Move page
192                    if (isSysFunction('*move*')) {
193                        // Create new #content_place block
194                        $('.content').remove();
195                        $('header').after('<div id="' + attrID + '" class="content arrive_right"></div>');
196                        // $(attrID).removeClass('leave').addClass('arrive');
197                        closeSearch(); // Close search
198                    }
199
200                    // Parse result
201                    for (let key in result.res)
202                        processField(result.res[key], attrID);
203
204                    callAfterLoad(); // After Load func
205                }, timeoutCountdown);
206
207            } else {
208                if (Array.isArray(result.error)) {
209                    result.error.forEach(function (item, i, arr) {
210                        $('.' + item.key).addClass('error');
211                        $('.' + item.key + ' span.error_text').text(item.value);
212                    });
213                } else {
214                    alert(result.error);
215                }
216
217                callAfterLoad(); // After Load func
218            }
219
220            // callAfterLoad();
221        },
222        error: function (result) {
223            // alert("Error!");
224        }
225    });
226
227    callAfterClick();
228}
229
230
231function movePage() {
232    // closeSearch(); // Close search
233
234    let leaveContent = $(".content");
235    leaveContent.removeAttr('id').removeClass('arrive').addClass('leave_right');
236
237    // setTimeout(function () {
238    //     leaveContent.remove();
239    // }, 500);
240    //
241    //
242    // // ???
243    // console.log("func -> Remove -> Start Move -> Add new content block / " + (new Date().getTime() - timeStatus) );
244    // timeStatus = new Date().getTime();
245    //
246    // leaveContent.removeAttr("id").removeClass('arrive').addClass('leave');
247    // leaveContent.after('<div id="content" class="content"></div>');
248}
249
250
251function processField(jsonObj, attrID) {
252    attrID = attrID || false;
253
254    try {
255        if (jsonObj.action === 'delete') {
256            // delete
257            $(jsonObj.key).remove();
258
259        } else if (jsonObj.action === 'prepend') {
260            // paste at the begin
261            $(jsonObj.key).prepend(jsonObj.value);
262
263        } else if (jsonObj.action === 'append') {
264            // paste et the end
265            $(jsonObj.key).append(jsonObj.value);
266
267        } else if (jsonObj.action === 'after') {
268            // paste after
269            $(jsonObj.key).after(jsonObj.value);
270
271            console.log(jsonObj.key);
272            console.log(jsonObj.value);
273
274        } else if (jsonObj.action === 'before') {
275            // paste before
276            $(jsonObj.key).before(jsonObj.value);
277
278        } else if (jsonObj.action === 'attr') {
279            // set attr
280            $(jsonObj.key).attr(jsonObj.attrName, jsonObj.value);
281
282        } else if (jsonObj.action === 'addClass') {
283            // set attr
284            $(jsonObj.key).addClass(jsonObj.value);
285
286        } else if (jsonObj.action === 'removeClass') {
287            // set attr
288            $(jsonObj.key).removeClass(jsonObj.value);
289
290        } else if (jsonObj.action === 'remove') {
291            // remove element
292            $(jsonObj.key).remove();
293
294        } else if (jsonObj.action === 'val') {
295            // set value
296            $(jsonObj.key).val(jsonObj.value);
297
298        } else if (jsonObj.action === 'func') {
299            // function
300            window[jsonObj.key](jsonObj.value);
301
302        } else if (jsonObj.action === 'jvar') {
303            // переменная в глобальном масиве ajaxVars
304            ajaxVars[jsonObj.key] = jsonObj.value;
305
306        } else if (jsonObj.action === 'load') {
307            // load page
308            if (jsonObj.value)
309                load(jsonObj.key, jsonObj.value);
310            else
311                load(jsonObj.key);
312
313        } else if (jsonObj.action === 'redirect') {
314            // redirect
315            window.location.href = jsonObj.value;
316
317        } else if (jsonObj.action === 'title') {
318            // set title
319            document.title = jsonObj.value;
320
321        } else if (jsonObj.action === 'url') { //url
322            // add history
323            history.pushState('', '', jsonObj.value);
324            GLOBAL_URL = jsonObj.value;
325
326        } else if (jsonObj.url != false && typeof jsonObj.url !== 'undefined') {
327            // add history
328            history.pushState('', '', jsonObj.url);
329            GLOBAL_URL = jsonObj.url;
330
331        } else if (jsonObj.click != false && typeof jsonObj.click !== 'undefined') {
332            // trigger click
333            $(jsonObj.click).trigger("click");
334
335        } else {
336            // paste html
337            // if (attrID) // if moving (exist parent block)
338            //     $(attrID + jsonObj.key).html(jsonObj.value);
339            // else
340                $(jsonObj.key).html(jsonObj.value);
341        }
342        return;
343    } catch (e) {
344        console.log("Error process", e);
345        return;
346    }
347}
348
349
350let CHECKcallAfterClick = false;
351let CHECKcallAfterLoad = false;
352
353/**
354 * Function calling after click load()
355 */
356function callAfterClick() {
357    if (!CHECKcallAfterClick) {
358        CHECKcallAfterClick = true;
359
360        // // Hide boards panel
361        // $(document).on('click', function(e) {
362        //     // Board
363        //     if (!$(e.target).closest('.boards_window').length && !$(e.target).closest('header a').length) {
364        //         // console.log("click not boards_window");
365        //         $('#boards').removeClass('open').addClass('close');
366        //         // $('#boards').fadeOut(200);
367        //     }
368        //
369        //     // Board link
370        //     if ($(e.target).closest('.boards_window .board_choose a').length) {
371        //         // console.log("click not boards_window > a");
372        //         // $('#boards').removeClass('open').addClass('close'); // <--- work before
373        //         // $('#search').addClass('close').removeClass('open');
374        //     }
375        //
376        //     e.stopPropagation();
377        // });
378    }
379}
380
381function callAfterLoad() {
382    if (!CHECKcallAfterLoad) {
383        CHECKcallAfterLoad = true;
384
385        // // Hide search
386        // $(document).on('click', function(e) {
387        //     // Search -> Home
388        //     if ($(e.target).closest('._home').length) {
389        //         // console.log("click home");
390        //         // closeSearch();
391        //     }
392        //
393        //     // Search -> tool open
394        //     if ($(e.target).closest('a.head').length) {
395        //         // console.log("click search tool");
396        //         // closeSearch();
397        //     }
398        //
399        //     e.stopPropagation();
400        // });
401    }
402}
403
404
405/**
406 * Function event_stop
407 * - event.preventDefault() stop go by link
408 * - event.stopPropagation() stop next events
409 */
410function stopEv() {
411    event.preventDefault(); // cancel switch href
412    event.stopPropagation();
413}
414
415
416function trim( str, charlist ) {
417    charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
418    let re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
419    return str.replace(re, '');
420}
421
422function formatString(str) {
423    return str
424        .replace(/\\\'/g, '\'')
425        .replace(/\\\"/g, '\"')
426        .replace(/\\\//g, '\/')
427        .replace(/\\\\/g, '\\');
428}
429
430
431function putText(target, html) {
432    $(target).html(html);
433}
434
435
436function getTime (mode) {
437    mode = mode || 's';
438
439    if (mode === 'ms')
440        return new Date().getTime(); // result in ms
441    else
442        return Math.floor(new Date().getTime() / 1000); // result in s
443}
444
445
446function addZero(value) {
447    if (value.toString().length === 1)
448        value = '0' + value;
449
450    return value;
451}
452
453function focusMe(key) {
454    $(key).focus();
455}
456
457
458function scrollToEl(el) {
459    let $page = $('html, body'),
460        speed = 1500;
461
462    // el = el || '#content_place';
463    el = el || '#site';
464
465
466    if (/^(.*?)\|\w{1,32}$/i.test(el)) {
467        // Scroll with time
468        let arr = el.split('|');
469        el      = arr[0];
470        speed   = arr[1];
471
472        // $page.animate({
473        //     scrollTop: $(el).offset().top
474        // }, speed);
475    } else {
476        // // Scroll
477        // $(el).animate({
478        //     scrollTop: 0
479        // }, 0);
480    }
481
482    $page.animate({
483        scrollTop: $(el).offset().top
484    }, speed);
485}
486
487
488/**
489 * isObjEmpty
490 * @param obj
491 * @returns {boolean}
492 */
493function isObjEmpty(obj) {
494    for (let key in obj)
495        return false;
496
497    return true;
498}
499
500
501/**
502 * checkInt - перевіряє чи значення є чилом і повертає його, якщо ні - повертається значення за замовчуванням
503 * @param value
504 * @param defaultValue
505 */
506function checkInt(value, defaultValue) {
507    if (!defaultValue && defaultValue !== false)
508        defaultValue = 0;
509
510    if (!isNaN(parseFloat(value)) && isFinite(value))
511        return parseInt(value); // added parseInt 12.11.2018
512    else
513        return defaultValue;
514}
515
516/**
517 * incrementAction
518 * @param el
519 */
520
521function incrementAction(el) {
522    let value = parseInt( $(el).text() );
523    if (isNaN(value))
524        value = 0;
525
526    $(el).text(value + 1);
527}
528
529function hideNotice(id) {
530    setTimeout(function(){ $('#'+id).fadeOut(2000); }, 7000);
531}
532
533
534function randomInteger(min, max) {
535    return Math.round( min + Math.random() * (max - min) );
536}
537
538function docW() { return $(document).width(); }
539function docH() { return $(document).height(); }
540function winW() { return $(window).width(); }
541function winH() { return $(window).height(); }
542
543
544/**
545 * copyToClipboard
546 * @param target
547 */
548function copyToClipboard(target) {
549    /* Get the text field */
550    var copyText = document.getElementById(target);
551
552    /* Select the text field */
553    copyText.select();
554    copyText.setSelectionRange(0, 99999); /*For mobile devices*/
555
556    /* Copy the text inside the text field */
557    document.execCommand("copy");
558
559    /* Alert the copied text */
560    alert("Copied!");
561}
562
563
564
565
566
567
568/* Crop */
569
570var minHeightForCrop = 300;
571var minWidthForCrop = 300;
572
573function setMinHeightForCrop(height) {
574    minHeightForCrop = height;
575}
576function setMinWidthForCrop(width) {
577    minWidthForCrop = width;
578}
579
580function addCrop(cropEl) {
581    var xy_h = document.getElementById("xy_h");
582    var div = document.getElementById("xy");
583    var flagOnMove = false;
584    var flagOnResize = false;
585
586    var crop = document.getElementById(cropEl);
587    var cropHeight = parseInt(crop.style.height);
588    var cropWidth = parseInt(crop.style.width);
589
590    var imgCoefficient = $("#coef").val();
591    if (imgCoefficient <= 1)
592        imgCoefficient = 1;
593
594    var minCropHeight = parseInt(minHeightForCrop / imgCoefficient);
595    var minCropWidth = parseInt(minWidthForCrop / imgCoefficient);
596
597
598    //console.log(cropWidth + ' * ' + cropHeight);
599    //console.log(imgCoefficient);
600
601    xy_h.style.top = 0 + "px";
602    xy_h.style.left = 0 + "px";
603    $("#posY").val(0);
604    $("#posX").val(0);
605
606    xy_h.style.width = minCropWidth + "px";
607    xy_h.style.height = minCropHeight + "px";
608    div.style.width = minCropWidth + "px";
609    div.style.height = minCropHeight + "px";
610    $("#width").val(minCropWidth);
611    $("#height").val(minCropHeight);
612
613    // ---C1--------------------------
614    var c1 = document.getElementById("c1");
615
616    c1.onmousedown = function (event) {
617        flagOnResize = true;
618
619        var width = div.offsetWidth;
620        var height = div.offsetHeight;
621        var top = xy_h.offsetTop;
622        var left = xy_h.offsetLeft;
623
624        var posX = event.pageX;
625        var posY = event.pageY;
626
627        document.onmousemove = function (event) {
628            if (flagOnResize == true) {
629                var res = width - (event.pageX - posX);
630                var resTop = top + event.pageX - posX;
631                var resLeft = left + event.pageX - posX;
632
633                if (res >= minCropWidth && resTop >= 0 && resLeft >= 0) {
634                    xy_h.style.top = resTop + "px";
635                    xy_h.style.left = resLeft + "px";
636                    $("#posY").val(resTop);
637                    $("#posX").val(resLeft);
638                }
639
640                if (res >= minCropWidth && resTop >= 0 && resLeft >= 0) {
641                    xy_h.style.width = res + "px";
642                    xy_h.style.height = res + "px";
643                    div.style.width = res + "px";
644                    div.style.height = res + "px";
645
646                    $("#width").val(res);
647                    $("#height").val(res);
648                }
649
650                scaling('mini');
651            }
652        };
653    };
654
655    // ---C2--------------------------
656    var c2 = document.getElementById("c2");
657
658    c2.onmousedown = function (event) {
659        flagOnResize = true;
660
661        var width = div.offsetWidth;
662        var height = div.offsetHeight;
663        var top = xy_h.offsetTop;
664        var left = xy_h.offsetLeft;
665
666        var posX = event.pageX;
667        var posY = event.pageY;
668
669        document.onmousemove = function (event) {
670            if (flagOnResize == true) {
671                var res = height + (posY - event.pageY);
672                var resTop = top - (posY - event.pageY);
673
674                if (res >= minCropHeight && resTop >= 0) {
675                    if (left + res <= cropWidth) {
676                        xy_h.style.top = resTop + "px";
677                        $("#posY").val(resTop);
678                    }
679                }
680
681                if (res >= minCropHeight && resTop >= 0) {
682
683                    if (left + res <= cropWidth) {
684                        //res = cropWidth - left;
685                        xy_h.style.width = res + "px";
686                        xy_h.style.height = res + "px";
687                        div.style.width = res + "px";
688                        div.style.height = res + "px";
689
690                        $("#width").val(res);
691                        $("#height").val(res);
692                    }
693                }
694
695                scaling('mini');
696            }
697        };
698    };
699
700    // ---C3--------------------------
701    var c3 = document.getElementById("c3");
702
703    c3.onmousedown = function (event) {
704        flagOnResize = true;
705
706        var width = div.offsetWidth;
707        var height = div.offsetHeight;
708        var top = xy_h.offsetTop;
709        var left = xy_h.offsetLeft;
710
711        var posX = event.pageX;
712        var posY = event.pageY;
713
714        document.onmousemove = function (event) {
715            if (flagOnResize == true) {
716                var res = width + event.pageX - posX;
717
718                if (res <= minCropWidth)
719                    res = minCropWidth;
720                if (left + res >= cropWidth)
721                    res = cropWidth - left;
722                if (top + res >= cropHeight)
723                    res = cropHeight - top;
724
725                xy_h.style.width = res + "px";
726                xy_h.style.height = res + "px";
727                div.style.width = res + "px";
728                div.style.height = res + "px";
729
730                $("#width").val(res);
731                $("#height").val(res);
732
733                scaling('mini');
734            }
735        };
736    };
737
738    // ---C4--------------------------
739    var c4 = document.getElementById("c4");
740
741    c4.onmousedown = function (event) {
742        flagOnResize = true;
743
744        var width = div.offsetWidth;
745        var height = div.offsetHeight;
746        var top = xy_h.offsetTop;
747        var left = xy_h.offsetLeft;
748
749        var posX = event.pageX;
750        var posY = event.pageY;
751
752        document.onmousemove = function (event) {
753            if (flagOnResize == true) {
754                var res = height + (event.pageY - posY);
755                var resLeft = left + (posY - event.pageY);
756
757                if (res >= minCropHeight && resLeft >= 0 && top + res <= cropHeight) {
758                    xy_h.style.left = resLeft + "px";
759                    $("#posX").val(resLeft);
760                }
761
762                if (res >= minCropHeight && resLeft >= 0 && top + res <= cropHeight) {
763                    xy_h.style.width = res + "px";
764                    xy_h.style.height = res + "px";
765                    div.style.width = res + "px";
766                    div.style.height = res + "px";
767
768                    $("#width").val(res);
769                    $("#height").val(res);
770                }
771
772                scaling('mini');
773            }
774        };
775    };
776
777    // -----------------------------
778
779    div.onmousedown = function (event) {
780        flagOnMove = true;
781
782        var width = div.offsetWidth;
783        var height = div.offsetHeight;
784        var top = xy_h.offsetTop;
785        var left = xy_h.offsetLeft;
786
787        var posX = event.pageX;
788        var posY = event.pageY;
789
790        document.onmousemove = function (event) {
791            if (flagOnMove == true) {
792                var newTop = top + event.pageY - posY;
793                if (newTop <= 0)
794                    newTop = 0;
795                if (newTop + height >= cropHeight)
796                    newTop = cropHeight - height;
797
798                var newLeft = left + event.pageX - posX;
799                if (newLeft <= 0)
800                    newLeft = 0;
801                if (newLeft + width >= cropWidth)
802                    newLeft = cropWidth - width;
803
804                xy_h.style.top = newTop + "px";
805                xy_h.style.left = newLeft + "px";
806
807                $("#width").val(width);
808                $("#height").val(height);
809                $("#posY").val(newTop);
810                $("#posX").val(newLeft);
811
812                scaling('mini');
813            }
814        };
815    };
816
817    document.onmouseup = function () {
818        flagOnMove = false;
819        flagOnResize = false;
820    };
821}
822
823function scaling(id) {
824    var obj = document.getElementById(id);
825    var xy_h = document.getElementById("xy_h");
826    var div = document.getElementById("xy");
827
828    var width = div.offsetWidth;
829    var height = div.offsetHeight;
830    var top = xy_h.offsetTop;
831    var left = xy_h.offsetLeft;
832
833    var imgW = 265;
834    var imgH = 265;
835    var miniature = 50;
836
837    var cofW = miniature/width;
838    var cofH = miniature/height;
839
840    if (obj != null) {
841        obj.style.width = imgW*cofW + "px";
842        obj.style.height = imgH*cofH + "px";
843        obj.style.marginTop = "-"+top*cofW + "px";
844        obj.style.marginLeft = "-"+left*cofH + "px";
845    }
846}

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.