Welcome to mirror list, hosted at ThFree Co, Russian Federation.

grid.js « js « static « exampleSite - github.com/mcrwfrd/hugo-frances-theme.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 42ae34bf008432429bc500c56331016154e99dc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// ======================= imagesLoaded Plugin ===============================
// https://github.com/desandro/imagesloaded

// $('#my-container').imagesLoaded(myFunction)
// execute a callback when all images have loaded.
// needed because .load() doesn't work on cached images

// callback function gets image collection as argument
//  this is the container

// original: MIT license. Paul Irish. 2010.
// contributors: Oren Solomianik, David DeSandro, Yiannis Chatzikonstantinou

// blank image data-uri bypasses webkit log warning (thx doug jones)
var BLANK = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';

$.fn.imagesLoaded = function( callback ) {
    var $this = this,
        deferred = $.isFunction($.Deferred) ? $.Deferred() : 0,
        hasNotify = $.isFunction(deferred.notify),
        $images = $this.find('img').add( $this.filter('img') ),
        loaded = [],
        proper = [],
        broken = [];

    // Register deferred callbacks
    if ($.isPlainObject(callback)) {
        $.each(callback, function (key, value) {
            if (key === 'callback') {
                callback = value;
            } else if (deferred) {
                deferred[key](value);
            }
        });
    }

    function doneLoading() {
        var $proper = $(proper),
            $broken = $(broken);

        if ( deferred ) {
            if ( broken.length ) {
                deferred.reject( $images, $proper, $broken );
            } else {
                deferred.resolve( $images );
            }
        }

        if ( $.isFunction( callback ) ) {
            callback.call( $this, $images, $proper, $broken );
        }
    }

    function imgLoaded( img, isBroken ) {
        // don't proceed if BLANK image, or image is already loaded
        if ( img.src === BLANK || $.inArray( img, loaded ) !== -1 ) {
            return;
        }

        // store element in loaded images array
        loaded.push( img );

        // keep track of broken and properly loaded images
        if ( isBroken ) {
            broken.push( img );
        } else {
            proper.push( img );
        }

        // cache image and its state for future calls
        $.data( img, 'imagesLoaded', { isBroken: isBroken, src: img.src } );

        // trigger deferred progress method if present
        if ( hasNotify ) {
            deferred.notifyWith( $(img), [ isBroken, $images, $(proper), $(broken) ] );
        }

        // call doneLoading and clean listeners if all images are loaded
        if ( $images.length === loaded.length ){
            setTimeout( doneLoading );
            $images.unbind( '.imagesLoaded' );
        }
    }

    // if no images, trigger immediately
    if ( !$images.length ) {
        doneLoading();
    } else {
        $images.bind( 'load.imagesLoaded error.imagesLoaded', function( event ){
            // trigger imgLoaded
            imgLoaded( event.target, event.type === 'error' );
        }).each( function( i, el ) {
            var src = el.src;

            // find out if this image has been already checked for status
            // if it was, and src has not changed, call imgLoaded on it
            var cached = $.data( el, 'imagesLoaded' );
            if ( cached && cached.src === src ) {
                imgLoaded( el, cached.isBroken );
                return;
            }

            // if complete is true and browser supports natural sizes, try
            // to check for image status manually
            if ( el.complete && el.naturalWidth !== undefined ) {
                imgLoaded( el, el.naturalWidth === 0 || el.naturalHeight === 0 );
                return;
            }

            // cached images don't fire load sometimes, so we reset src, but only when
            // dealing with IE, or image is complete (loaded) and failed manual check
            // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
            if ( el.readyState || el.complete ) {
                el.src = BLANK;
                el.src = src;
            }
        });
    }

    return deferred ? deferred.promise( $this ) : $this;
};

// == now my code ==

var Grid = (function() {
    var $selector = '#og-grid',
        $grid = $($selector),
        $items = $grid.children('li'),
        current = -1,
        previewPosition = -1,
        scrollExtra = 0,
        marginExpanded = 10,
        $window = $(window), winSize,
        $body = $('html, body'),
        transEndEventNames = {
            'WebkitTransition' : 'webkitTransitionEnd',
            'MozTransition' : 'transitionend',
            'OTransition' : 'oTransitionEnd',
            'msTransition' : 'MSTransitionEnd',
            'transition' : 'transitionend'
        },
        transEndEventName = transEndEventNames[Modernizr.prefixed( 'transition')],
        support = Modernizr.csstransitions,
        settings = {
            minHeight : 500,
            speed : 250,
            easing : 'ease'
        };

    function init(config) {
        settings = $.extend(true, {}, settings, config);

        $grid.imagesLoaded(function () {
            saveItemInfo(true);
            getWinSize();
            initEvents();
        });
    }

    function addItems($newItems) {
        $items = $items.add($newItems);
        $newItems.each(function() {
           var $item = $(this);
           $item.data({
              offsetTop : $item.offset().top,
              height : $item.height()
           });
        });
        initItemsEvents($newItems);
    }

    function saveItemInfo(saveHeight) {
        $items.each(function() {
            var $item = $(this);
            $item.data('offsetTop', $item.offset().top);

            if (saveHeight) {
                $item.data('height', $item.height());
            }

        });
    }

    function initEvents() {
        initItemsEvents($items);

        $window.on('debouncedresize', function() {
            scrollExtra = 0;
            previewPosition = -1;

            saveItemInfo();
            getWinSize();
            var preview = $.data(this, 'preview');
            if (typeof preview != 'undefined') {
                hidePreview();
            }
        });
    }

    function initItemsEvents($items) {
        $items.on('click', 'span.og-close', function() {
           hidePreview();
           return false;
        }).children('a').on('click', function(e) {
            var $item = $(this).parent();
            current === $item.index() ? hidePreview() : showPreview($item);
            return false;
        });
    }

    function getWinSize() {
        winSize = {
            width: $window.width(),
            height: $window.height()
        };
    }

    function showPreview($item) {
        var preview = $.data(this, 'preview'),
            position = $item.data('offsetTop');

        scrollExtra = 0;

        if (typeof preview !== 'undefined') { // this block needs to be refactored to be more readable
            if (previewPosition != position) {
                if (position > previewPosition) {
                    scrollExtra = preview.height;
                }
                hidePreview();
            } else {
                preview.update($item);
                return false;
            }
        }

        previewPosition = position;
        preview = $.data(this, 'preview', new Preview($item));
        preview.open();
    }

    function hidePreview() {
        current = -1;
        var preview = $.data(this, 'preview');
        preview.close();
        $.removeData(this, 'preview');
    }

    function Preview($item) {
        this.$item = $item;
        this.expandedIndex = this.$item.index();
        this.create();
        this.update();
    }

    Preview.prototype = {
        create : function() {
            this.$title = $('<h3></h3>');
            this.$description = $('<p></p>');
            this.$mediums = $('<p></p>');
            this.$dimensions = $('<p></p>');
            this.$details = $('<div class="og-details"></div>').append(this.$title, this.$description, this.$mediums, this.$dimensions);
            this.$loading = $('<div class="og-loading"></div>');
            this.$fullimage = $('<div class="og-fullimg"></div>').append(this.$loading);
            this.$closePreview = $('<span class="og-close"></span>');
            this.$previewInner = $('<div class="og-expander-inner"></div>').append(this.$closePreview, this.$fullimage, this.$details);
            this.$previewElement = $('<div class="og-expander"></div>').append(this.$previewInner);

            this.timout = 25;

            this.$item.append(this.getElement());

            if (support) {
                this.setTransition();
            }

        },
        update : function($item) {

            if ($item) {
                this.$item = $item;
            }

            if (current !== -1) {
                var $currentItem = $items.eq(current);
                $currentItem.removeClass('og-expanded');
                this.$item.addClass('og-expanded');
                this.positionPreview();
            }

            current = this.$item.index();

            var $itemElement = this.$item.children('a'),
                elementData = {
                    largesrc : $itemElement.data('largesrc'),
                    title : $itemElement.data('title'),
                    description : $itemElement.data('description'),
                    mediums : $itemElement.data('mediums'),
                    dimensions : $itemElement.data('dimensions')
                };

            this.$title.html(elementData.title);
            this.$description.html(elementData.description);
            this.$mediums.html(elementData.mediums);
            this.$dimensions.html(elementData.dimensions);

            var self = this;

            if (typeof self.$largeImg != 'undefined') {// change name to largeSrc
                self.$largeImg.remove();
            }

            if (self.$fullimage.is(':visible')) {
                this.$loading.show();
                $('<img/>').load( function() {
                    var $img = $(this);

                    if ($img.attr('src') === self.$item.children('a').data('largesrc') ) {
                        self.$loading.hide();
                        self.$fullimage.find('img').remove();
                        self.$largeImg = $img.fadeIn(350);
                        self.$fullimage.append(self.$largeImg);
                    }

                } ).attr('src', elementData.largesrc);
            }
        },
        open : function() {
            setTimeout($.proxy(function() {
                this.setHeight();
                this.positionPreview();
            }, this), this.timeout)
        },
        close : function() {
            var self = this,
                onEndFn = function() { // good got rename this function!
                    if( support ) {
                        $(this).off(transEndEventName);
                    }
                    self.$item.removeClass('og-expanded');
                    self.$previewElement.remove();
                };

            setTimeout($.proxy( function() {

                if (typeof this.$largeImg !== 'undefined') {
                    this.$largeImg.fadeOut( 'fast' );
                }

                this.$previewElement.css('height', 0);
                var $expandedItem = $items.eq(this.expandedIndex);
                $expandedItem.css('height', $expandedItem.data('height')).on(transEndEventName, onEndFn);

                if (!support) {
                    onEndFn.call();
                }

            }, this ), this.timout);

            return false;
        },
        getHeight : function() {
            var previewHeight = winSize.height - this.$item.data('height') - marginExpanded,
                itemHeight = winSize.height;

            if (previewHeight < settings.minHeight) {
                previewHeight = settings.minHeight;
                itemHeight = settings.minHeight + this.$item.data( 'height' ) + marginExpanded;
            }

            this.height = previewHeight;
            this.itemHeight = itemHeight;
        },
        setHeight : function() {
            var self = this,
                onEndFn = function() {

                    if (support) {
                        self.$item.off(transEndEventName);
                    }

                    self.$item.addClass('og-expanded');
                };

            this.getHeight();
            this.$previewElement.css('height', this.height);
            this.$item.css('height', this.itemHeight).on(transEndEventName, onEndFn);

            if (!support) {
                onEndFn.call();
            }
        },
        positionPreview : function() {
            var position = this.$item.data( 'offsetTop' ),
                previewOffsetTop = this.$previewElement.offset().top - scrollExtra,
                scrollValue = this.height + this.$item.data( 'height' ) + marginExpanded <= winSize.height ? position : this.height < winSize.height ? previewOffsetTop - ( winSize.height - this.height ) : previewOffsetTop;

            $body.animate({scrollTop : scrollValue}, settings.speed);
        },
        setTransition : function() {
            this.$previewElement.css('transition', 'height ' + settings.speed + 'ms ' + settings.easing);
            this.$item.css('transition', 'height ' + settings.speed + 'ms ' + settings.easing);
        },
        getElement : function() {
            return this.$previewElement;
        }
    }

    return {
        init : init,
        addItems : addItems
    };

})();