Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in methods for adding and removing flapper digits on the fly #13

Merged
merged 1 commit into from
Aug 17, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.options.align === 'left') ? this.digits.pop() : 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