Instantly share code, notes, and snippets.
Created
March 7, 2012 21:35
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save Kalyse/1996387 to your computer and use it in GitHub Desktop.
Lazy Loading Social Buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require([ | |
"dojo/_base/declare", "dojo/dom-construct", "dojo/parser", "dojo/ready", | |
"dijit/_WidgetBase", "dijit/_TemplatedMixin", "dijit/_CssStateMixin" | |
], function(declare, domConstruct, parser, ready,_WidgetBase, _TemplatedMixin, _CssStateMixin){ | |
declare("my.diskit.Social", [_WidgetBase, _TemplatedMixin, _CssStateMixin], { | |
// counter | |
baseClass: 'diskitSocial', | |
fb : { | |
dataLayout : "button_count", | |
dataSend: false, | |
dataShowFaces : false, | |
dataAction : "like", | |
dataFont : "verdana", | |
className : "fb-like" | |
}, | |
tw : { | |
className : "twitter-share-button", | |
dataSize : "small", | |
dataCount : "true" | |
}, | |
dataUrl : false, | |
templateString: | |
"<div class='${baseClass}'>" + | |
"<span data-dojo-attach-point='twitter' class='${baseClass}Twitter ${baseClass}Lazy'></span>" + | |
"<span data-dojo-attach-point='facebook' class='${baseClass}Facebook ${baseClass}Lazy'></span>" + | |
"</div>", | |
constructor : function(args){ | |
if(typeof args.facebook != "undefined"){ | |
dojo.mixin( this.fb, args.facebook ); | |
} | |
}, | |
postCreate: function(){ | |
this._loadHandle = this.connect(this.domNode, "onmouseover", "_loadSocial"); | |
this.inherited(arguments); | |
}, | |
_loadSocial: function(){ | |
dojo.disconnect( this._loadHandle ); | |
dojo.empty(this.domNode); | |
var twitter = domConstruct.create("a", { | |
"href": "https://twitter.com/share", | |
'class': this.tw.className, | |
"data-size": this.tw.dataSize, | |
"data-count": this.tw.dataCount, | |
"data-url" : this.dataUrl | |
}, this.domNode); | |
var facebook = domConstruct.create("div", { | |
"class" : this.fb.className, | |
"data-send" : this.fb.dataSend, | |
"data-layout" : this.fb.dataLayout, | |
"data-show-faces" : this.fb.dataShowFaces, | |
"data-action" : this.fb.dataAction, | |
"data-font" : this.fb.dataFont, | |
"data-href" : this.dataUrl | |
}, this.domNode); | |
domConstruct.create("div", { | |
"class" : 'clear' | |
}, this.domNode); | |
this._loadFacebook(); | |
this._loadTwitter(); | |
}, | |
_loadFacebook : function(){ | |
(function(d, s, id) { | |
var js, fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) return; | |
domConstruct.create("div", { | |
id: "fb-root" | |
}, this.domNode); | |
js = d.createElement(s); | |
js.id = id; | |
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=314159575308890"; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, 'script', 'facebook-jssdk')); | |
if(typeof FB != "undefined" ){ | |
FB.init({ | |
status : true, // check login status | |
cookie : true, // enable cookies to allow the server to access the session | |
xfbml : true // parse XFBML | |
}); | |
} | |
}, | |
_loadTwitter: function(){ | |
!function(d,s,id){ | |
var js,fjs=d.getElementsByTagName(s)[0]; | |
if(!d.getElementById(id)){ | |
js=d.createElement(s); | |
js.id=id; | |
js.src="//platform.twitter.com/widgets.js"; | |
fjs.parentNode.insertBefore(js,fjs); | |
} | |
}(document,"script","twitter-wjs"); | |
if(typeof twttr != "undefined" ){ | |
twttr.widgets.load(); | |
} | |
} | |
}); | |
ready(function(){ | |
// Call the parser manually so it runs after our widget is defined, and page has finished loading | |
parser.parse(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment