UNPKG

vpn.email

Version:
245 lines (202 loc) 7.44 kB
$.widget( "metro.tile" , { version: "3.0.0", options: { effect: 'slideLeft', period: 4000, duration: 700, easing: 'doubleSqrt', onClick: function(tile){} }, _frames: {}, _currentIndex: 0, _interval: 0, _outPosition: 0, _size: {}, _create: function () { var that = this, element = this.element, o = this.options; $.each(element.data(), function(key, value){ if (key in o) { try { o[key] = $.parseJSON(value); } catch (e) { o[key] = value; } } }); this._createTransformTile(); this._createLiveTile(); this._createEvents(); element.data('tile', this); }, _createEvents: function(){ var that = this, element = this.element, o = this.options; var event = isTouchDevice() ? 'touchstart' : 'click'; element.on(event, function(e){ if (element[0].tagName === "A") { } else { if (typeof o.onClick === 'function') { o.onClick(element); } else { if (typeof window[o.onClick] === 'function') { window[o.onClick](element); } else { var result = eval("(function(){"+o.onClick+"})"); result.call(element); } } } }); }, _createLiveTile: function(){ var that = this, element = this.element, o = this.options; var event_down = isTouchDevice() ? 'touchstart' : 'mouseenter'; var event_up = isTouchDevice() ? 'touchend' : 'mouseleave'; this._frames = element.find(".live-slide"); if (this._frames.length <= 1) {return false;} $.easing.doubleSqrt = function(t) {return Math.sqrt(Math.sqrt(t));}; this._size = { 'width': element.width(), 'height': element.height() }; element.on(event_down, function(){ that.stop(); }); element.on(event_up, function(){ that.start(); }); this.start(); }, start: function(){ var that = this; this._interval = setInterval(function(){ that._animate(); }, this.options.period); }, stop: function(){ clearInterval(this._interval); }, _animate: function(){ var currentFrame = this._frames[this._currentIndex], nextFrame; this._currentIndex += 1; if (this._currentIndex >= this._frames.length) {this._currentIndex = 0;} nextFrame = this._frames[this._currentIndex]; switch (this.options.effect) { case 'slideLeft': this._effectSlideLeft(currentFrame, nextFrame); break; case 'slideRight': this._effectSlideRight(currentFrame, nextFrame); break; case 'slideDown': this._effectSlideDown(currentFrame, nextFrame); break; case 'slideUpDown': this._effectSlideUpDown(currentFrame, nextFrame); break; case 'slideLeftRight': this._effectSlideLeftRight(currentFrame, nextFrame); break; default: this._effectSlideUp(currentFrame, nextFrame); } }, _effectSlideLeftRight: function(currentFrame, nextFrame){ if (this._currentIndex % 2 === 0) { this._effectSlideLeft(currentFrame, nextFrame); } else { this._effectSlideRight(currentFrame, nextFrame); } }, _effectSlideUpDown: function(currentFrame, nextFrame){ if (this._currentIndex % 2 === 0) { this._effectSlideUp(currentFrame, nextFrame); } else { this._effectSlideDown(currentFrame, nextFrame); } }, _effectSlideUp: function(currentFrame, nextFrame){ var _out = this._size.height; var options = { 'duration': this.options.duration, 'easing': this.options.easing }; $(currentFrame) .animate({top: -_out}, options); $(nextFrame) .css({top: _out}) .show() .animate({top: 0}, options); }, _effectSlideDown: function(currentFrame, nextFrame){ var _out = this._size.height; var options = { 'duration': this.options.duration, 'easing': this.options.easing }; $(currentFrame) .animate({top: _out}, options); $(nextFrame) .css({top: -_out}) .show() .animate({top: 0}, options); }, _effectSlideLeft: function(currentFrame, nextFrame){ var _out = this._size.width; var options = { 'duration': this.options.duration, 'easing': this.options.easing }; $(currentFrame) .animate({left: _out * -1}, options); $(nextFrame) .css({left: _out}) .show() .animate({left: 0}, options); }, _effectSlideRight: function(currentFrame, nextFrame){ var _out = this._size.width; var options = { 'duration': this.options.duration, 'easing': this.options.easing }; $(currentFrame) .animate({left: _out}, options); $(nextFrame) .css({left: -_out}) .show() .animate({left: 0}, options); }, _createTransformTile: function(){ var that = this, element = this.element, o = this.options; var dim = {w: element.width(), h: element.height()}; var event_down = isTouchDevice() ? 'touchstart' : 'mousedown'; var event_up = isTouchDevice() ? 'touchend' : 'mouseup'; var event_leave = isTouchDevice() ? 'touchend' : 'mouseleave'; element.on(event_down, function(e){ var X = e.pageX - $(this).offset().left, Y = e.pageY - $(this).offset().top; var transform = 'top'; if (X < dim.w * 1/3 && (Y < dim.h * 1/2 || Y > dim.h * 1/2 )) { transform = 'left'; } else if (X > dim.w * 2/3 && (Y < dim.h * 1/2 || Y > dim.h * 1/2 )) { transform = 'right'; } else if (X > dim.w*1/3 && X<dim.w*2/3 && Y > dim.h/2) { transform = 'bottom'; } $(this).addClass("tile-transform-"+transform); //console.log(transform); if (element[0].tagName === 'A' && element.attr('href')) { setTimeout(function(){ document.location.href = element.attr('href'); }, 500); } }); element.on(event_up, function(){ $(this) .removeClass("tile-transform-left") .removeClass("tile-transform-right") .removeClass("tile-transform-top") .removeClass("tile-transform-bottom"); }); element.on(event_leave, function(){ $(this) .removeClass("tile-transform-left") .removeClass("tile-transform-right") .removeClass("tile-transform-top") .removeClass("tile-transform-bottom"); }); }, _destroy: function () { }, _setOption: function ( key, value ) { this._super('_setOption', key, value); } });