-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddThisBar.js
More file actions
193 lines (155 loc) · 5.14 KB
/
Copy pathaddThisBar.js
File metadata and controls
193 lines (155 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*!
* addThisBar v0.1.0 (https://github.com/TechTarget/addThisBar)
* Author: Morgan Wigmanich <okize123@gmail.com> (http://github.com/okize)
* Copyright (c) 2013 | Licensed under the MIT license
* http://www.opensource.org/licenses/mit-license.php
*/
;(function (factory) {
// use AMD or browser globals to create a jQuery plugin.
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}(function ($) {
'use strict';
// default plugin options
var pluginName = 'addThisBar';
var defaults = {
addThisApiVersion: '300' // 300, 250, 200, 150
};
// plugin constructor
var Bar = function (element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.doc = $(window.document);
this.addThisScript = '//s7.addthis.com/js/' + this.options.addThisApiVersion + '/addthis_widget.js'; // url of addthis script
this.addThisConfiguration = {
pubid: 'ra-4f0c7ed813520536', // change this to whatever profile should be used
domready: true
};
this.addThisScriptCache = {};
this.init();
};
Bar.prototype = {
init: function() {
// var self = this;
// load the addthis script
$.when(this.loadAddthisScript(this.addThisScript)).then(function () {
window.addthis.bar.show({
message: 'This is whatever text I feel like adding in here',
action: {
'type': 'button',
'text': 'I am a button!',
'verb': 'link',
'url': 'https://github.com/okize'
}
});
window.addthis.bar.initialize({
'default': {
backgroundColor: '#333',
textColor: 'white'
}
});
// addthis.bar.initialize({
// default: {
// backgroundColor: '#eee',
// textColor: '#000'
// },
// rules: [{
// match: {
// 'service': 'facebook'
// },
// 'message': 'Hey Facebook user! Share my page.',
// 'action': {
// 'type': 'button',
// 'text': 'Share',
// 'verb': 'share',
// 'service': 'facebook',
// 'url': 'http://addthis.com'
// }
// }, {
// match: {
// 'service': 'twitter'
// },
// 'message': 'Hey Twitter user! Tweet my page.',
// 'action': {
// 'type': 'button',
// 'text': 'Share',
// 'verb': 'share',
// 'service': 'twitter',
// 'url': 'http://addthis.com'
// }
// }
// ]});
// addthis.bar.initialize([{
// 'match': {
// 'time': {'start':'06:00', 'end':'23:40'}
// },
// 'message': 'Wow, it's early! Have a coffee on us',
// 'action': {
// 'type': 'button',
// 'text': 'All right!',
// 'verb': 'link',
// 'url': 'http://www.starbucks.com/'
// }
// }
// ]);
});
},
isAddThisLoaded: function (bool) {
// if argument is passed then function is setter
if (arguments.length > 0 && typeof bool === 'boolean') {
this.doc.data('addThisScriptLoaded', bool);
}
// truth in dom; if data attr hasn't been set yet, set it
if (typeof this.doc.data('addThisScriptLoaded') === 'undefined') {
this.doc.data('addThisScriptLoaded', false);
return false;
}
return this.doc.data('addThisScriptLoaded');
},
setAddThisConfiguration: function() {
// addthis_config is global to the page so only set it once
if (this.isAddThisReady() === true && typeof window.addthis_config === 'undefined') {
window.addthis_config = this.addThisConfiguration;
window.addthis_share = this.addThisShareConfiguration;
}
},
loadAddthisScript: function (val) {
// if cache has been set, return promise form these
// else create new jqXHR object and store it in the cache
var promise = this.addThisScriptCache[val];
if (!promise) {
promise = $.ajax({
url: this.addThisScript,
cache: true,
dataType: 'script'
});
this.addThisScriptCache[val] = promise;
}
return promise;
},
isAddThisReady: function () {
// check for global addthis object
// doesn't seem to be a public method for getting version loaded
// otherwise there should be a check here to compare version loaded is
// the same as the version requested in the plugin init
if (typeof window.addthis === 'undefined') {
return false;
} else {
return true;
}
}
};
// lightweight wrapper around the constructor that prevents multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Bar( this, options ));
}
});
};
}));