Skip to content

Commit

Permalink
Add in methods for adding and removing flapper digits on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremysawesome committed Aug 5, 2015
1 parent 8fc8d3f commit 4c9cdba
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/jquery.flapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(function($) {

var prependToId = 'Flap', flappers = {};

var Flapper = function($ele, options) {
var _this = this;
this.id = Math.floor(Math.random() * 1000) + 1;
Expand All @@ -17,6 +19,10 @@
_this.update();
});

var flapperId = this.$ele[0].id || prependToId + this.id;
this.$ele.attr('id', flapperId);
flappers[flapperId] = this;

this.init();
}

Expand Down Expand Up @@ -107,6 +113,33 @@
}

return digits;
},

addDigit: function(){
var flapDigit = new FlapDigit(null, this.options);
if (this.options.align === 'left') {
this.digits.push(flapDigit);
this.$div.append(flapDigit.$ele);
}
else{
this.digits.unshift(flapDigit);
this.$div.prepend(flapDigit.$ele);
}
this.options.width = this.digits.length;
return flapDigit;
},

removeDigit: function(){
var flapDigit = this.digits.shift();
flapDigit.$ele.remove();
this.options.width = this.digits.length;
},

performAction: function(action){
switch(action){
case 'add-digit': this.addDigit(); break;
case 'remove-digit': this.removeDigit(); break;
}
}
}

Expand Down Expand Up @@ -253,9 +286,13 @@
}
};

$.fn.flapper = function(options) {
$.fn.flapper = function(arg) {
this.each(function(){
new Flapper($(this), options);
if(!(typeof arg === 'string' || arg instanceof String)) return new Flapper($(this), arg);

if(this.id && flappers.hasOwnProperty(this.id)){
flappers[this.id].performAction(arg);
}
});

return this;
Expand Down

0 comments on commit 4c9cdba

Please sign in to comment.