PageSourceSearch

https://www.newspostonline.com/wp-content/plugins/jnews-gallery/assets/js/jquery.previewslider.js

js newspostonline.com collected 2026-10-02 11:20:10 UTC 22,654 bytes, 805 lines download raw bytes

1/**
2 * Preview Slider by Jegtheme
3 */
4
5(function ($) {
6    "use strict";
7
8    var Slider = function(element, options)
9    {
10        this.element           = $(element);
11        this.options            = options;
12
13        // base element
14        this.holder             = $('.jeg_preview_media_content_holder', element);
15        this.slider             = $('.jeg_preview_bottom_slider', element);
16        this.nav_next           = $('.jeg_preview_media_content_navigation > div.next', element);
17        this.nav_prev           = $('.jeg_preview_media_content_navigation > div.prev', element);
18        this.control_index      = $('.jeg_preview_control .counter .current', element);
19        this.thumb              = $('.jeg_preview_bottom_slider > a', element);
20        this.subtitle           = $('.jeg_preview_control .subtitle', element);
21        this.description        = $('.jeg_hidden_preview_description', element);
22        this.description_wrap   = $('.jeg_preview_description_wrapper', element);
23        this.slider_ads         = $('.jeg_preview_slider_ads', element);
24
25        // switch mode
26        this.mode               = 'normal'; // or fullscreen
27        this.mode_switcher      = $('.fullscreen-switch', element);
28        this.preview_holder     = $('.jeg_preview_holder', element);
29        this.description_holder = $('.jeg_preview_description', element);
30        this.preview_content    = $('.jeg_preview_media_content', element);
31        this.text_title         = $('.jeg_preview_media_holder h3', element);
32
33        // number slide
34        this.index              = 0;
35        this.max                = this.thumb.length - 1;
36
37        // zoom variable
38        this.zoom_size          = 0;
39        this.zoom_reduce        = $('.jeg_preview_control .reduce', element);
40        this.zoom_increase      = $('.jeg_preview_control .increase', element);
41        this.zoom_cache         = null;
42        this.zoom_lock          = true;
43        this.zoom_limit         = [0,0,0,0];
44
45        this.init();
46    };
47
48    Slider.DEFAULTS = {
49        rtl: false,
50        native_zoom: true,
51        slideSpeed: 100,
52        fit: 'fit',
53        zoom_max: 5,
54        zoom_step: 20,
55        fullscreen_stop: 976
56    };
57
58    Slider.prototype.init = function()
59    {
60        this.bind_event();
61        this.create_slider_normal();
62        this.assign_slider_event();
63        this.first_load();
64    };
65
66    Slider.prototype.first_load = function()
67    {
68        var base = this;
69
70        base.change_content();
71        base.change_subtitle();
72        base.slider_navigation_check();
73    };
74
75    Slider.prototype.bind_event = function()
76    {
77        var base = this;
78
79        // zoom changed
80        base.zoom_reduce.on('click', function(){
81            base.change_zoom('reduce');
82        });
83
84        base.zoom_increase.on('click', function(){
85            base.change_zoom('increase');
86        });
87
88        base.holder.on('dblclick', function(){
89            base.mode_switcher.trigger('click')
90            // base.change_zoom('increase');
91        });
92
93
94        // thumb clicked
95        base.thumb.on('click', function(e){
96            e.preventDefault();
97
98            base.index = $(this).data('id');
99            base.do_change_slider();
100        });
101
102
103        // draggable event
104        base.holder.on('mousedown', function(e){
105            $(this).addClass('draggable');
106
107            var pos_y = e.pageY,
108                pos_x = e.pageX;
109
110            $(window).on('mousemove', function(e)
111            {
112                base.do_dragging(e.pageX - pos_x , e.pageY - pos_y);
113
114                pos_y = e.pageY;
115                pos_x = e.pageX;
116            }).on('mouseup', function(){
117                $(this).removeClass('draggable');
118                $(window).unbind('mousemove');
119            });
120
121            e.preventDefault();
122        }).on('mouseup', function(){
123            $(this).removeClass('draggable');
124            $(window).unbind('mousemove');
125        });
126
127        // fullscreen switcher
128        base.mode_switcher.on('click', $.proxy(function (e) {
129            if(base.options.native_zoom) {
130                 base.switch_mode();
131            } else {
132                if(jnewsoption === null) {
133                    base.open_magnific_popup();
134                } else {
135                    if(jnewsoption.popup_script === 'photoswipe') {
136                        base.open_photoswipe_popup();
137                    } else {
138                        base.open_magnific_popup();
139                    }
140                }
141
142            }
143            e.preventDefault();
144        },base));
145
146
147        // resize even
148        var resizetimeout;
149        $(window).bind('resize', function(){
150            clearTimeout(resizetimeout);
151            resizetimeout = setTimeout(function(){
152                if(base.mode === 'normal') {
153                    base.resize_normal();
154                } else if(base.mode === 'fullscreen') {
155                    base.resize_fullscreen();
156                }
157            }, 50);
158        });
159    };
160
161    Slider.prototype.open_magnific_popup = function(){
162        var base = this;
163        var sequence = base.options.image_sequence;
164        var current = base.index;
165
166        $.magnificPopup.instance.next = function() {
167            $.magnificPopup.proto.next.call(this);
168            if(base.index >= base.max) {
169                base.index = 0;
170            } else {
171                base.index = base.index + 1;
172            }
173            base.do_change_slider();
174        };
175
176        $.magnificPopup.instance.prev = function() {
177            $.magnificPopup.proto.prev.call(this);
178            if(base.index === 0 ) {
179                base.index = base.max;
180            } else {
181                base.index = base.index - 1;
182            }
183            base.do_change_slider();
184        };
185
186        $.magnificPopup.open({
187            items: sequence,
188            gallery: {
189                enabled: true
190            },
191            type: 'image',
192            closeOnContentClick: true,
193            closeBtnInside: false,
194            fixedContentPos: true,
195            mainClass: 'mfp-no-margins mfp-with-zoom',
196            image: {
197                verticalFit: true
198            },
199        }, current);
200    };
201
202    Slider.prototype.open_photoswipe_popup = function(){
203        var base = this;
204        var items = base.options.image_sequence;
205        var current = base.index;
206        var pswpElement = $('.pswp').get(0);
207
208        var options = {
209            index: current,
210            history: false,
211            focus: false,
212            showAnimationDuration: 0,
213            hideAnimationDuration: 0
214        };
215
216        var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
217
218        gallery.listen('afterChange', function() {
219            base.index = gallery.getCurrentIndex();
220            base.do_change_slider();
221        });
222
223        gallery.init();
224    };
225
226    Slider.prototype.switch_mode = function()
227    {
228        var base = this;
229
230        if(base.mode === 'normal') {
231            base.fullscreen_mode();
232        } else if(base.mode === 'fullscreen') {
233            base.normal_mode();
234        }
235    };
236
237    Slider.prototype.fullscreen_mode = function()
238    {
239        var base = this;
240
241        base.mode = 'fullscreen';
242        $('body').addClass('jeg_preview_body_hidden');
243        base.go_fullscreen(true);
244        base.keyboard_event('bind');
245    };
246
247    Slider.prototype.normal_mode = function()
248    {
249        var base = this;
250
251        base.mode = 'normal';
252        $('body').removeClass('jeg_preview_body_hidden');
253        base.go_normal();
254        base.keyboard_event('unbind');
255    };
256
257
258    Slider.prototype.keyboard_event = function(event)
259    {
260        var base = this;
261
262        if(event === 'bind')
263        {
264            $('body').on('keydown', function(e)
265            {
266                if(e.keyCode == 37) {
267                    base.change_slider('prev');
268                } else if(e.keyCode == 39) {
269                    base.change_slider('next');
270                } else if(e.keyCode == 27) {
271                    base.mode = 'normal';
272                    base.normal_mode();
273                }
274                e.preventDefault();
275            });
276        } else if(event === 'unbind'){
277            $('body').unbind('keydown');
278        }
279    };
280
281    Slider.prototype.resize_normal = function()
282    {
283        var base = this;
284
285        // fix image position
286        base.change_content();
287    };
288
289    Slider.prototype.resize_fullscreen = function()
290    {
291        var base = this;
292
293        if($(window).width() > base.options.fullscreen_stop)
294        {
295            base.go_fullscreen(false);
296        } else {
297            base.mode = 'normal';
298            base.normal_mode();
299        }
300    };
301
302    Slider.prototype.go_fullscreen = function(recreate_carousel)
303    {
304        var base = this;
305
306        var window_width = $(window).width();
307        var window_height = $(window).height();
308        var preview_width, description_width;
309
310        // preview_content
311        description_width = 400;
312        preview_width = window_width - description_width;
313
314        base.element.addClass('fullscreen');
315        base.preview_holder.outerWidth(preview_width);
316        base.description_holder.outerWidth(description_width);
317        base.description_wrap.outerHeight(window_height - base.slider_ads.outerHeight());
318
319        // recreate owl carousel
320        if(recreate_carousel) {
321            base.recreate_slider();
322            base.create_slider_fullscreen();
323        }
324
325        // canvas
326        base.preview_content.outerHeight(window_height - base.slider.outerHeight() - base.text_title.outerHeight());
327
328        // fix image position
329        base.change_content();
330    };
331
332
333    Slider.prototype.go_normal = function()
334    {
335        var base = this;
336
337        base.element.removeClass('fullscreen');
338
339        // normalize width & height
340        base.preview_holder.css('width', 'auto');
341        base.description_holder.css('width', 'auto');
342        base.preview_content.css('height', 'auto');
343
344        // recreate owl carousel
345        base.recreate_slider();
346        base.create_slider_normal();
347
348        //fix image position
349        base.change_content();
350    };
351
352
353    Slider.prototype.recreate_slider = function()
354    {
355        var base = this;
356
357        base.slider.trigger('destroy.owl.carousel').removeClass('owl-loaded');
358        base.slider.find('.owl-stage-outer').children().unwrap();
359    };
360
361    Slider.prototype.create_slider_normal = function()
362    {
363        var base = this;
364
365        base.slider.owlCarousel({
366            rtl: base.options.rtl,
367            lazyLoad:true,
368            nav: false,
369            margin:15,
370            navText: ['',''],
371            dots: false,
372            stagePadding: 15,
373            responsive : {
374                0 : {
375                    items: 3
376                },
377                480 : {
378                    items: 4
379                },
380                768 : {
381                    items: 5
382                },
383                1000:{
384                    items: 7
385                }
386            }
387        });
388    };
389
390    Slider.prototype.create_slider_fullscreen = function()
391    {
392        var base = this;
393
394        base.slider.owlCarousel({
395            rtl: base.options.rtl,
396            lazyLoad:true,
397            nav: false,
398            margin:15,
399            navText: ['',''],
400            dots: false,
401            stagePadding: 15,
402            responsive : {
403                0 : {
404                    items: 2
405                },
406                480 : {
407                    items: 3
408                },
409                768 : {
410                    items: 5
411                },
412                1000 : {
413                    items: 6
414                },
415                1200 : {
416                    items: 8
417                },
418                1600:{
419                    items: 10
420                }
421            }
422        });
423    };
424
425    Slider.prototype.assign_slider_event = function()
426    {
427        var base = this;
428
429        base.nav_next.on('click', function(e){
430            e.preventDefault();
431            base.change_slider('next');
432        });
433
434        base.nav_prev.on('click', function(e){
435            e.preventDefault();
436            base.change_slider('prev');
437        });
438    };
439
440    Slider.prototype.change_slider = function(goto)
441    {
442        var base = this;
443
444        base.index = ( goto === 'next' ) ? base.index + 1 : base.index - 1;
445
446        if(base.index < 0 || base.index > base.max)
447        {
448            if(base.index < 0) {
449                base.index = 0;
450            }
451
452            if(base.index > base.max) {
453                base.index = base.max;
454            }
455            return;
456        }
457
458        base.do_change_slider();
459    };
460
461
462    Slider.prototype.slider_navigation_check = function()
463    {
464        var base = this;
465
466        if(base.index === 0) {
467            base.nav_prev.hide();
468        } else if(base.index === base.max) {
469            base.nav_next.hide();
470        } else {
471            base.nav_prev.show();
472            base.nav_next.show();
473        }
474    };
475
476    Slider.prototype.do_change_slider = function()
477    {
478        var base = this;
479        var current_thumb = $(base.thumb.get(base.index));
480
481
482        if(!current_thumb.parent('.owl-item').hasClass('active'))
483        {
484            base.slider.trigger('to.owl.carousel', [base.index, base.options.slideSpeed, true]);
485        }
486
487        // add active class
488        $(base.thumb).removeClass('active');
489        current_thumb.addClass('active');
490
491        // change control index text
492        base.control_index.text(base.index + 1);
493        base.change_content();
494
495        // slider navigation check
496        base.slider_navigation_check();
497    };
498
499    Slider.prototype.reset_zoom = function()
500    {
501        var base = this;
502
503        base.zoom_size = 0;
504        base.zoom_lock = true;
505        $(base.zoom_reduce).addClass('off');
506        $(base.zoom_increase).removeClass('off');
507        base.holder.removeClass('jeg_preview_grabbing');
508    };
509
510    Slider.prototype.change_subtitle = function()
511    {
512        var base = this;
513
514        var thumb = $(base.thumb.get(base.index));
515        var subtitle = $(thumb).data('title');
516
517        if(subtitle === '') {
518            base.subtitle.hide();
519        } else {
520            base.subtitle.show();
521        }
522
523        base.subtitle.text(subtitle);
524
525        return subtitle;
526    };
527
528    Slider.prototype.change_description = function()
529    {
530        var base = this;
531
532        base.description.removeClass('active');
533        $(base.description.get(base.index)).addClass('active');
534    };
535
536    Slider.prototype.change_content = function()
537    {
538        var base = this;
539        var img = new Image();
540        var cur_index = base.index;
541        var image = $(base.thumb.get(cur_index)).data('image');
542        base.reset_zoom();
543
544        base.holder.find('img').fadeOut(function(){
545            $(this).remove();
546        });
547
548        base.change_subtitle();
549        base.change_description();
550
551        $(img).load(function(){
552            if(cur_index === base.index)
553            {
554                var imgsize = base.image_resize_calc(img, base.holder, 'fit');
555                base.zoom_cache = imgsize;
556
557                $(img).addClass('jeg_preview_hide');
558                base.holder.append(img);
559
560                $(img).css({
561                    'height': Math.floor(imgsize[0]),
562                    'width': Math.floor(imgsize[1]),
563                    'left' : Math.floor(imgsize[2]) + "px",
564                    'top' : Math.floor(imgsize[3]) + "px",
565                    'max-width': 'inherit'
566                }).fadeIn();
567                base.zoom_lock = false;
568            }
569        }).attr('src', image);
570    };
571
572    Slider.prototype.change_zoom = function(mode)
573    {
574        var base = this;
575        if(base.zoom_lock) return;
576
577        if(mode === 'increase') {
578            base.zoom_size = base.zoom_size + 1;
579        } else {
580            base.zoom_size = base.zoom_size - 1;
581        }
582
583        if(base.zoom_size < 0 || base.zoom_size > base.options.zoom_max)
584        {
585            if(base.zoom_size < 0) {
586                base.zoom_size = 0;
587            }
588            if(base.zoom_size > base.options.zoom_max) {
589                base.zoom_size = base.options.zoom_max;
590            }
591            return;
592        }
593
594        if(base.zoom_size <= 0) {
595            $(base.zoom_reduce).addClass('off');
596            $(base.zoom_increase).removeClass('off');
597            base.holder.removeClass('jeg_preview_grabbing');
598            base.zoom_size = 0;
599        } else if(base.zoom_size >= base.options.zoom_max) {
600            $(base.zoom_reduce).removeClass('off');
601            $(base.zoom_increase).addClass('off');
602            base.holder.addClass('jeg_preview_grabbing');
603            base.zoom_size = base.options.zoom_max;
604        } else {
605            $(base.zoom_reduce).removeClass('off');
606            $(base.zoom_increase).removeClass('off');
607            base.holder.addClass('jeg_preview_grabbing');
608        }
609
610        base.change_image_zoom(mode);
611    };
612
613    Slider.prototype.do_dragging = function(x, y)
614    {
615        var base = this;
616        if(base.zoom_size > 0) {
617            var img = base.holder.find('img');
618            var imgposition = img.position();
619
620            var toppos = imgposition.top + y;
621            var leftpos = imgposition.left + x;
622
623            if(toppos < base.zoom_limit[1]) {
624                toppos = base.zoom_limit[1];
625            }
626
627            if(toppos > base.zoom_limit[0]) {
628                toppos = base.zoom_limit[0];
629            }
630
631            if(leftpos < base.zoom_limit[3]) {
632                leftpos = base.zoom_limit[3];
633            }
634
635            if(leftpos > base.zoom_limit[2]){
636                leftpos = base.zoom_limit[2];
637            }
638
639            img.css({
640                top: toppos,
641                left: leftpos
642            });
643        }
644    };
645
646    Slider.prototype.change_image_zoom = function(mode)
647    {
648        var base = this;
649        var current_image = $(base.holder).find('img');
650        var zoom_size = base.zoom_size;
651        var zoom_step  = base.options.zoom_step;
652        var new_top, new_left;
653
654        var new_height =  Math.floor(base.zoom_cache[0] + ( base.zoom_cache[0] * zoom_step * zoom_size / 100 ));
655        var new_width =  Math.floor(base.zoom_cache[1] + ( base.zoom_cache[1] * zoom_step * zoom_size / 100 ));
656
657        if(mode === 'increase') {
658            new_top = current_image.position().top - base.zoom_cache[0] * zoom_step / 2 / 100;
659            new_left = current_image.position().left - base.zoom_cache[1] * zoom_step / 2 / 100;
660        } else {
661            new_top = current_image.position().top + base.zoom_cache[0] * zoom_step / 2  / 100;
662            new_left = current_image.position().left + base.zoom_cache[1] * zoom_step / 2 / 100
663        }
664
665        // calculate limit of this.zoom_limit
666        var zoom_limit_top;
667        var zoom_limit_bottom;
668        var zoom_limit_left;
669        var zoom_limit_right;
670        var holder_height = base.holder.height();
671        var holder_width = base.holder.width();
672
673        // holder limitation
674        if(holder_height > new_height) {
675            zoom_limit_top = ( holder_height - new_height ) / 2;
676            zoom_limit_bottom = zoom_limit_top + new_height;
677        } else {
678            zoom_limit_top = 0;
679            zoom_limit_bottom = holder_height - new_height;
680        }
681
682        if(holder_width > new_width) {
683            zoom_limit_left = ( holder_width - new_width ) / 2;
684            zoom_limit_right = zoom_limit_left + new_width;
685        } else {
686            zoom_limit_left = 0;
687            zoom_limit_right = holder_width - new_width;
688        }
689
690        base.zoom_limit = [
691            zoom_limit_top,
692            zoom_limit_bottom,
693            zoom_limit_left,
694            zoom_limit_right
695        ];
696
697        if(base.zoom_size > 0)
698        {
699            if( new_top < zoom_limit_bottom) {
700                new_top = zoom_limit_bottom;
701            }
702
703            // prioritize zoom top
704            if(new_top > zoom_limit_top) {
705                new_top = zoom_limit_top;
706            }
707
708            if( new_left < zoom_limit_right) {
709                new_left = zoom_limit_right;
710            }
711
712            // prioritize zoom left
713            if(new_left > zoom_limit_left) {
714                new_left = zoom_limit_left;
715            }
716        } else {
717            new_top = ( holder_height - new_height ) / 2;
718            new_left = ( holder_width - new_width ) / 2;
719        }
720
721        $(current_image).css({
722            'height': new_height,
723            'width': new_width,
724            'left' : new_left + "px",
725            'top' : new_top + "px"
726        });
727    };
728
729    Slider.prototype.image_resize_calc = function (img, container)
730    {
731        // height / width / top / left
732        var nh, nw, nt, nl;
733
734        var h = $(img).get(0).naturalHeight;
735        var w = $(img).get(0).naturalWidth;
736
737        if (h === 0) {
738            h = $(img).data('height');
739            w = $(img).data('width');
740        }
741
742        var wh = $(container).height();
743        var ww = $(container).width();
744        var r = ( h / w );
745        var wr = wh / ww;
746
747        var resizeWidth = function () {
748            nw = ww;
749            nh = ww * r;
750            nl = ( ww - nw ) / 2;
751            nt = ( wh - nh ) / 2;
752
753            return [nh, nw, nl, nt];
754        };
755
756        var resizeHeight = function () {
757            nw = wh / r;
758            nh = wh;
759            nl = ( ww - nw ) / 2;
760            nt = ( wh - nh ) / 2;
761            return [nh, nw, nl, nt];
762        };
763
764        var resizeSmall = function(){
765            nh = h;
766            nw = w;
767            nl = ( ww - nw ) / 2;
768            nt = ( wh - nh ) / 2;
769
770            return [nh, nw, nl, nt];
771        };
772
773        if(wh > h && ww > w) {
774            return resizeSmall();
775        } else {
776            if (wr > r) {
777                return resizeWidth();
778            }
779            return resizeHeight();
780        }
781    };
782
783    function Plugin(option)
784    {
785        return $(this).each(function()
786        {
787            var $this = $(this);
788            var options = $.extend({}, Slider.DEFAULTS, $this.data(), typeof option == 'object' && option);
789            var data = $this.data('jeg.previewslider');
790
791            if (!data) $this.data('jeg.previewslider', (data = new Slider(this, options)));
792        });
793    }
794
795    var old = $.fn.jpreviewslider;
796
797    $.fn.jpreviewslider = Plugin;
798    $.fn.jpreviewslider.Constructor = Slider;
799
800    $.fn.jpreviewslider.noConflict = function () {
801        $.fn.jpreviewslider = old;
802        return this
803    };
804
805})(jQuery);

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.