diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e3c099..7f40247 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ #### CHANGE LOG +###### 2.0.2 +* code refactor +* added tests + ###### 2.0.1 * viewport sizing bugfix diff --git a/Newfile b/Newfile index 9a27c9d..b6737cb 100644 --- a/Newfile +++ b/Newfile @@ -2,7 +2,7 @@ sources: default: ~/Code/ruby/new-tasks name: Tiler -version: 2.0.1 +version: 2.0.2 tasks: changelog: git: diff --git a/bower.json b/bower.json index 208f346..29aed5e 100644 --- a/bower.json +++ b/bower.json @@ -5,8 +5,10 @@ "jquery-ui": "latest" }, "devDependencies": { - "chai": "latest", "chai-as-promised": "latest", + "chai-jquery": "latest", + "chai": "latest", + "lolex": "sinonjs/lolex", "mocha": "latest", "sinon-chai": "latest", "sinon": "latest" diff --git a/demo/tiler_demo.coffee b/demo/tiler_demo.coffee index d572dfc..dc48512 100644 --- a/demo/tiler_demo.coffee +++ b/demo/tiler_demo.coffee @@ -2,16 +2,16 @@ $ -> # initalize tiler $('.tiler-viewport').each -> - $(@).tiler().tiler('goTo', 1, false) + $(@).tiler() # set the button text to match the tile title $('button[data-tiler-link]').each -> - $(@).text($(@).data('tiler-title')) + $(@).text $(@).data('tiler-title') # go to a tile on click based on the link id $('button').click -> tileId = $(@).data('tiler-link') - $(@).closest('.tiler-viewport').tiler('goTo', tileId) + $(@).closest('.tiler-viewport').tiler 'goTo', tileId # event to set background $('#background').on 'tiler.goto', (e, data) -> diff --git a/demo/tiler_demo.js b/demo/tiler_demo.js index 0159fce..450455a 100644 --- a/demo/tiler_demo.js +++ b/demo/tiler_demo.js @@ -2,7 +2,7 @@ (function() { $(function() { $('.tiler-viewport').each(function() { - return $(this).tiler().tiler('goTo', 1, false); + return $(this).tiler(); }); $('button[data-tiler-link]').each(function() { return $(this).text($(this).data('tiler-title')); diff --git a/lib/tiler.js b/lib/tiler.js index e7b895c..f10cae5 100644 --- a/lib/tiler.js +++ b/lib/tiler.js @@ -4,7 +4,7 @@ * * tiler * * https://github.com/brewster1134/tiler * * - * * @version 2.0.1 + * * @version 2.0.2 * * @author Ryan Brewster * * Copyright (c) 2014 * * Licensed under the MIT license. @@ -23,19 +23,13 @@ return $.widget('ui.tiler', { widgetEventPrefix: 'tiler', options: { - isReversible: true, - startingActiveTile: 1, - startingPreviousTile: 2 - }, - _create: function() { - this.currentActiveTileIndex = this.options.startingActiveTile; - return this.currentPreviousTileIndex = this.options.startingPreviousTile; + activeTile: 1, + isReversible: true }, _init: function() { - this.element.addClass('animation-disabled'); this.$tiles = $('.tiler-tile', this.element).not(this.element.find('.tiler-viewport .tiler-tile')); - this.$enterTile = this.$tiles.eq(this.currentActiveTileIndex - 1); - this.$exitTile = this.$tiles.eq(this.currentPreviousTileIndex - 1); + this.$currentActiveTile || (this.$currentActiveTile = this._getTile(this.options.activeTile)); + this.$currentPreviousTile || (this.$currentPreviousTile = this._getTile(this._getTileIndex(this.$currentActiveTile) + 1)); this._setupTiles(); return this._setupLinks(); }, @@ -45,90 +39,97 @@ this.element.trigger('tiler.refresh'); return (ref = this.$enterTile) != null ? ref.trigger('tiler.refresh') : void 0; }, + goTo: function(tileValue, animation) { + var $enteringTile, $exitingTile; + $enteringTile = this._getTile(tileValue); + $exitingTile = this.$currentActiveTile; + if (!$enteringTile.length || $enteringTile[0] === this.$currentActiveTile[0]) { + return; + } + this._transitionCss($enteringTile, $exitingTile, animation); + return $enteringTile; + }, _getTile: function(tileValue) { - if (typeof tileValue === 'string') { - return $("#" + tileValue, this.element); - } else if (tileValue.jquery) { - return tileValue.jquery; - } else if (tileValue.nodeType) { - return $(tileValue); + var $tile; + $tile = (function() { + switch (typeof tileValue) { + case 'number': + return this.$tiles.eq(tileValue - 1); + case 'string': + return $("#" + tileValue, this.element); + default: + return $(tileValue, this.element); + } + }).call(this); + if ($tile.length) { + return $tile; } else { - return this.$tiles.eq(tileValue - 1); + return this.$tiles.eq(0); } }, - goTo: function(tile, animation) { - var enterTileIndex; - this.$enterTile = this._getTile(tile); - enterTileIndex = this.$tiles.index(this.$enterTile); - this.$exitTile = this._getTile(this.currentActiveTileIndex + 1); - if (!this.$enterTile.length || this.currentActiveTileIndex === enterTileIndex) { - return; - } - this._transitionCss(this._getAnimationClass(), animation); - this.element.trigger('tiler.goto', { - enterTile: this.$enterTile, - exitTile: this.$exitTile - }); - this.$enterTile.trigger('tiler.enter'); - this.$exitTile.trigger('tiler.exit'); - this.currentActiveTileIndex = enterTileIndex; - this.currentPreviousTileIndex = this.currentActiveTileIndex; - this.element.attr('data-tiler-active-tile', this.$enterTile.attr('id')); - return this.$enterTile; + _getTileIndex: function($tile) { + return this.$tiles.index($tile) + 1; }, - _getAnimationClass: function() { - return this.$enterTile.data('tiler-animation') || this.element.data('tiler-animation') || ''; + _getAnimationClass: function($tile) { + return $tile.data('tiler-animation') || this.element.data('tiler-animation') || ''; }, - _transitionCss: function(animationClass, animation) { - var enterStartClass, enterTileIndex, exitStartClass, otherTileClass, position, reverseClass; - if (typeof animation === 'string') { - animationClass = animation; - } - position = animation === false ? 'end' : 'start'; - enterTileIndex = this.$tiles.index(this.$enterTile); - reverseClass = this.options.isReversible && !this._isNavigatingForward(enterTileIndex) ? 'reverse' : ''; + _transitionCss: function($enteringTile, $exitingTile, animation) { + var animationClass, positionClass, reverseClass; + animationClass = typeof animation === 'string' ? animation : this._getAnimationClass($enteringTile); + reverseClass = this.options.isReversible && this._getTileIndex($enteringTile) < this._getTileIndex(this.$currentActiveTile) ? 'reverse' : ''; + positionClass = animation === false ? 'end' : 'start'; this.element.addClass('animation-disabled'); - enterStartClass = "tiler-tile " + animationClass + " active enter " + reverseClass + " " + position; - exitStartClass = "tiler-tile " + animationClass + " previous exit " + reverseClass + " " + position; - otherTileClass = 'tiler-tile'; - this.$enterTile.attr('class', enterStartClass); - this.$exitTile.attr('class', exitStartClass); - this.$tiles.not(this.$enterTile).not(this.$exitTile).attr('class', otherTileClass); - if (animation !== false) { + $enteringTile.attr('class', "tiler-tile " + animationClass + " active enter " + reverseClass + " " + positionClass); + $exitingTile.attr('class', "tiler-tile " + animationClass + " previous exit " + reverseClass + " " + positionClass); + this.$tiles.not($enteringTile).not($exitingTile).attr('class', 'tiler-tile'); + if (animation === false) { + return this._finalizeNewTiles($enteringTile, $exitingTile); + } else { return setTimeout((function(_this) { return function() { _this.element.removeClass('animation-disabled'); - return _this.$enterTile.add(_this.$exitTile).switchClass('start', 'end'); + $enteringTile.add($exitingTile).switchClass('start', 'end'); + return _this._finalizeNewTiles($enteringTile, $exitingTile); }; })(this), 10); } }, - _setupLinks: function() { - return $('[data-tiler-link]').each(function() { - var tile, tileId, tileIds; - tileIds = $(this).data('tiler-link').split(':').reverse(); - tileId = tileIds[0]; - tile = tileIds[1] ? $(".tiler-tile#" + tileId, "#" + tileIds[1]) : $(".tiler-tile#" + tileId); - if (!tile.length) { - return; - } - return $.extend($(this).data(), tile.data()); + _finalizeNewTiles: function($enterTile, $exitTile) { + this.$currentActiveTile = $enterTile; + this.$currentPreviousTile = $exitTile; + this.element.attr('data-tiler-active-tile', this.$currentActiveTile.attr('id')); + this.element.trigger('tiler.goto', { + enterTile: $enterTile, + exitTile: $exitTile }); + $enterTile.trigger('tiler.enter'); + return $exitTile.trigger('tiler.exit'); }, _setupTiles: function() { - var self; - self = this; - this.$tiles.each(function() { - $(this).attr('data-tiler-viewport-id', self.element.attr('id')); - return $(this).addClass(self._getAnimationClass(true)); - }); - return this.element.add(this.$tiles).css({ + this.element.addClass('animation-disabled'); + this.$tiles.attr('data-tiler-viewport-id', this.element.attr('id')); + this.$tiles.css({ width: this.element.outerWidth(), height: this.element.outerHeight() }); + this.$currentActiveTile.add(this.$currentPreviousTile).addClass(this._getAnimationClass(this.$currentActiveTile)); + this.$currentActiveTile.addClass('active enter end'); + this.$currentPreviousTile.addClass('previous exit end'); + if (this.options.isReversible) { + return this.$currentPreviousTile.addClass('reverse'); + } }, - _isNavigatingForward: function(enterTileIndex) { - return enterTileIndex > this.currentActiveTileIndex; + _setupLinks: function() { + return $('[data-tiler-link]').each(function() { + var $tile, tileId, tileIds; + tileIds = $(this).data('tiler-link').split(':').reverse(); + tileId = tileIds[0]; + $tile = tileIds[1] ? $(".tiler-tile#" + tileId, "#" + tileIds[1]) : $(".tiler-tile#" + tileId); + if (!$tile.length) { + return; + } + return $.extend($(this).data(), $tile.data()); + }); } }); }); diff --git a/spec/index.html b/spec/index.html index e974099..835b9e3 100644 --- a/spec/index.html +++ b/spec/index.html @@ -10,38 +10,73 @@