From 9112c7aa418b222afa41bba93c0dd2df299b25d5 Mon Sep 17 00:00:00 2001 From: Alexander Burtsev Date: Fri, 1 Nov 2013 18:00:21 +0300 Subject: [PATCH 1/4] JavaScript options --- Readme.md | 11 ++++++++++- src/demo.html | 17 ++++++++++++++++- src/social-likes.js | 26 ++++++++++++++------------ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Readme.md b/Readme.md index b94dfbf..0799429 100644 --- a/Readme.md +++ b/Readme.md @@ -68,7 +68,7 @@ If you want to remove button titles add `social-likes_notext` class to make it l ### Options -Options define via HTML data attributes. +Options define via HTML data attributes or JavaScript parameters object. `url` @@ -104,6 +104,15 @@ Examples: ``` +```js +$('.social-likes').socialLikes({ + url: 'https://github.com/sapegin/social-likes/', + title: 'Beautiful “like” buttons with counters for popular social networks', + html: 'Share with us!', + counters: 'yes' +}); +``` + ### Services specific options #### Twitter diff --git a/src/demo.html b/src/demo.html index b94fb7d..c8585dc 100644 --- a/src/demo.html +++ b/src/demo.html @@ -4,7 +4,7 @@ Social Likes - + @@ -211,6 +211,21 @@ sl.on('counter.social-likes', function(event, service, number) { console.log('Counter', service, number); }); sl.socialLikes(); + + +

Manual initialization with JavaScript options

+ + +

User button & custom count

diff --git a/src/social-likes.js b/src/social-likes.js index 3cc9796..64664d5 100644 --- a/src/social-likes.js +++ b/src/social-likes.js @@ -165,16 +165,16 @@ var counters = { /** * jQuery plugin */ -$.fn.socialLikes = function() { +$.fn.socialLikes = function(options) { return this.each(function() { - new SocialLikes($(this)); + new SocialLikes($(this), options); }); }; -function SocialLikes(container) { +function SocialLikes(container, options) { this.container = container; - this.init(); + this.init(options || {}); } SocialLikes.prototype = { @@ -202,17 +202,17 @@ SocialLikes.prototype = { convert: function(value) { return value === 'yes'; } } }, - init: function() { + init: function(options) { // Add class in case of manual initialization this.container.addClass(prefix); - this.readOptions(); + this.readOptions(options); this.single = this.container.hasClass(prefix + '_single'); this.initUserButtons(); if (this.single) { - this.makeSingleButton(); + this.makeSingleButton(options); } var options = this.options; @@ -220,11 +220,13 @@ SocialLikes.prototype = { new Button($(this), options); }); }, - readOptions: function() { + readOptions: function(options) { this.options = {}; - for (var key in this.optionsMap) { + for (var key in this.optionsMap) { var option = this.optionsMap[key]; - var value = this.container.data(option.attr); + var paramOption = options[option.attr]; + var value = paramOption || this.container.data(option.attr); + if (!value) { if ($.isFunction(option.defaultValue)) { value = $.proxy(option.defaultValue, this)(); @@ -245,7 +247,7 @@ SocialLikes.prototype = { } this.userButtonInited = true; }, - makeSingleButton: function() { + makeSingleButton: function(options) { var container = this.container; container.addClass(prefix + '_vertical'); container.wrap($('
', {'class': prefix + '_single-w'})); @@ -256,7 +258,7 @@ SocialLikes.prototype = { var button = $('
', { 'class': getElementClassNames('button', 'single'), - 'text': container.data('single-title') || 'Share' + 'text': options['single-title'] || container.data('single-title') || 'Share' }); button.prepend($('', {'class': getElementClassNames('icon', 'single')})); wrapper.append(button); From 602345abbfbf23b7dc9ef39fed43cbb6957aabe4 Mon Sep 17 00:00:00 2001 From: Alexander Burtsev Date: Thu, 5 Dec 2013 15:21:06 +0300 Subject: [PATCH 2/4] JavaScript options: small refactoring --- src/social-likes.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/social-likes.js b/src/social-likes.js index 64664d5..d63118b 100644 --- a/src/social-likes.js +++ b/src/social-likes.js @@ -165,16 +165,16 @@ var counters = { /** * jQuery plugin */ -$.fn.socialLikes = function(options) { +$.fn.socialLikes = function(opts) { return this.each(function() { - new SocialLikes($(this), options); + new SocialLikes($(this), opts); }); }; -function SocialLikes(container, options) { +function SocialLikes(container, opts) { this.container = container; - this.init(options || {}); + this.init(opts); } SocialLikes.prototype = { @@ -202,17 +202,17 @@ SocialLikes.prototype = { convert: function(value) { return value === 'yes'; } } }, - init: function(options) { + init: function(opts) { // Add class in case of manual initialization this.container.addClass(prefix); - this.readOptions(options); + this.readOptions(opts); this.single = this.container.hasClass(prefix + '_single'); this.initUserButtons(); if (this.single) { - this.makeSingleButton(options); + this.makeSingleButton(); } var options = this.options; @@ -220,12 +220,13 @@ SocialLikes.prototype = { new Button($(this), options); }); }, - readOptions: function(options) { + readOptions: function(opts) { + opts = opts || {}; this.options = {}; + for (var key in this.optionsMap) { var option = this.optionsMap[key]; - var paramOption = options[option.attr]; - var value = paramOption || this.container.data(option.attr); + var value = opts[option.attr] || this.container.data(option.attr); if (!value) { if ($.isFunction(option.defaultValue)) { @@ -247,7 +248,7 @@ SocialLikes.prototype = { } this.userButtonInited = true; }, - makeSingleButton: function(options) { + makeSingleButton: function() { var container = this.container; container.addClass(prefix + '_vertical'); container.wrap($('
', {'class': prefix + '_single-w'})); @@ -258,7 +259,7 @@ SocialLikes.prototype = { var button = $('
', { 'class': getElementClassNames('button', 'single'), - 'text': options['single-title'] || container.data('single-title') || 'Share' + 'text': this.options['single-title'] || container.data('single-title') || 'Share' }); button.prepend($('', {'class': getElementClassNames('icon', 'single')})); wrapper.append(button); From 69e58d0ee62dc19dd3ed2848d506c84af28bb37f Mon Sep 17 00:00:00 2001 From: Alexander Burtsev Date: Thu, 5 Dec 2013 16:08:30 +0300 Subject: [PATCH 3/4] JavaScript options: yes/true fixes --- Readme.md | 3 +-- src/demo.html | 7 +++---- src/social-likes.js | 18 +++++++++++------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Readme.md b/Readme.md index 0799429..80b8d3e 100644 --- a/Readme.md +++ b/Readme.md @@ -108,8 +108,7 @@ Examples: $('.social-likes').socialLikes({ url: 'https://github.com/sapegin/social-likes/', title: 'Beautiful “like” buttons with counters for popular social networks', - html: 'Share with us!', - counters: 'yes' + counters: true }); ``` diff --git a/src/demo.html b/src/demo.html index c8585dc..68571a2 100644 --- a/src/demo.html +++ b/src/demo.html @@ -132,7 +132,7 @@ body {
  • Google+
  • Pinterest
  • -