Skip to content

Commit b5de285

Browse files
committed
refactor: plugin based architecture
1 parent 526519b commit b5de285

19 files changed

+501
-209
lines changed

Gruntfile.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ module.exports = function(grunt) {
1313
sourceMap: false
1414
},
1515
dist: {
16-
src: 'lib/built.js',
17-
dest: 'lib/<%= pkg.name %>.min.js'
16+
src: 'dist/built.js',
17+
dest: 'dist/<%= pkg.name %>.min.js'
1818
}
1919
},
2020
concat: {
2121
options: {
2222
separator: ';',
2323
},
2424
dist: {
25-
src: ['src/qrcode.min.js', 'src/social-share.js'],
26-
dest: 'lib/built.js',
25+
src: ['lib/qrcode.min.js', 'src/social-share.js', 'src/plugins/*.js'],
26+
dest: 'dist/built.js',
2727
},
2828
},
2929
cssmin: {
@@ -32,11 +32,11 @@ module.exports = function(grunt) {
3232
banner: '/*! bbbb */'
3333
},
3434
files: {
35-
'lib/<%= pkg.name %>.min.css': 'src/social-share.css'
35+
'dist/<%= pkg.name %>.min.css': 'src/social-share.css'
3636
}
3737
}
3838
},
39-
clean: ["lib/built.js"]
39+
clean: ["dist/built.js"]
4040
});
4141

4242
// Load the plugin that provides the "uglify" task.

README.md

+241-44
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
Social share widget supporting: wechat, weibo, linkedin, github, google+, rss, twitter, facebook and more.
44

5-
Demo: http://harttle.com/social-share/
5+
Live Demo: http://harttle.com/social-share/
66

7-
Dependencies [Fontawesome][font], [jQuery][jq].
7+
Dependencies: [Fontawesome][font]
88

9-
## Usage
9+
## Installation
1010

11-
### Import
12-
13-
Import jQuery and Fontawesome:
11+
Import Fontawesome:
1412

1513
```html
1614
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
17-
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
1815
```
1916

2017
Import Social Share:
@@ -24,54 +21,114 @@ Import Social Share:
2421
<script src="path/to/social-share.min.js"></script>
2522
```
2623

27-
### Mininal Usage
24+
## Mininal Usage
2825

29-
```javascript
30-
$('div').socialShare({
31-
facebook: 'https://www.facebook.com/harttle',
32-
wechat: location.href,
33-
rss: 'http://harttle.com/feed.xml'
34-
});
26+
```html
27+
<div id="share-area"></div>
3528
```
3629

37-
### Ordered Links
30+
```javascript
31+
var el = document.getElementById('share-area');
32+
var links = [{
33+
url: 'http://harttle.com',
34+
target: '_qrcode'
35+
}, {
36+
plugin: 'github',
37+
url: 'http://github.com/harttle'
38+
}, {
39+
plugin: 'github',
40+
args: {
41+
id: 'harttle'
42+
}
43+
}];
44+
window.socialShare(el, links);
45+
```
3846

39-
In case you want these links be ordered, put them into an array:
47+
## Full Usage
4048

4149
```javascript
4250
var links = [{
43-
style: 'facebook',
44-
url: 'https://www.facebook.com/harttle'
45-
},{
46-
style: 'google-plus',
47-
url: 'https://plus.google.com/+杨珺',
48-
},{
49-
style: 'weibo',
50-
url: 'http://v.t.sina.com.cn/share/share.php?url=xxx&title=xxx&appid=xxx'
51+
url: 'http://harttle.com',
52+
target: '_qrcode',
53+
color: '#fff',
54+
background: '#b5b5b5',
55+
icon: 'fa-code-fork',
56+
plugin: 'github',
57+
args: {
58+
id
59+
}
5160
}];
52-
53-
$('div').socialShare(links);
61+
var options = {
62+
size: 'md'
63+
};
64+
window.socialShare(el, links, options);
5465
```
5566

56-
### Available Styles
67+
## Options
68+
69+
### links.url
5770

58-
These are available styles:
71+
Type: `String`
5972

60-
`qrcode`, `facebook`, `google-plus`, `weibo`, `wechat`, `linkedin`, `rss`, `github`, `twitter`.
73+
Default: `location.href`
6174

62-
> Feel free to contribute yours!
75+
The url of this icon. Typically, `links.url` will be set to the `href` attribute
76+
of the corresponding anchor.
6377

64-
## Options
78+
### links.target
6579

66-
### $.fn.socialShare(links, options)
80+
Type: `String`
81+
82+
Default: `""`
83+
84+
This will be set to the `target` attribute of the anchor.
85+
Available targets: `"_self"`, `"_parent"`, `"_blank"`, `"_top"`, `"_qrcode"`
86+
If set to `_qrcode`, the `links.url` will be opened as a qrcode image within a modal.
87+
88+
### links.icon
89+
90+
Type: `String`
91+
92+
Default: `'fa-code-fork'`
93+
94+
The Fontawesome icon class for the share button.
95+
96+
### links.color
97+
98+
Type: `String`
6799

68-
### links
100+
Defalt: `'#fff'`
101+
102+
The color of the Fontawesome icon.
103+
104+
### links.background
105+
106+
Type: `String`
107+
108+
Default: `'#b5b5b5'`
109+
110+
The background of the Fontawesome icon.
111+
112+
### links.plugin
113+
114+
Type: `String`
115+
116+
Default: `undefined`
117+
118+
The plugin to use. Typically, a plugin is used to generage
119+
the above settings, according to the arguments set by `links.args`.
120+
121+
Note: Settings within `links` will override the settings returned by a plugin.
122+
For example, `github` plugin responds with the url `//foo`,
123+
while `links.url` is set to `//bar`. The result url will be `//bar`.
124+
125+
### links.args
69126

70127
Type: `Object`
71128

72129
Default: `{}`
73130

74-
Share buttons are set in `links` object.
131+
The arguments passed to the plugin, which is specified by `links.plugin`.
75132

76133
### options.size
77134

@@ -86,25 +143,165 @@ Size of the buttons, available values:
86143
* `"sm"`(small)
87144
* `"xs"`(exteme small)
88145

89-
### options.blank
146+
## Plugin List
90147

91-
Type: `Boolean`
148+
### Weibo(微博)
92149

93-
Default: `true`
150+
All args will be append to URL query string.
94151

95-
Should the links open in new window? If set `true`, `target="_blank"` will be set.
152+
```javascript
153+
var link = {
154+
plugin: 'weibo',
155+
args: {
156+
appid: '<your App ID>', // Default: ''
157+
title: 'About Harttle', // Default: document.title
158+
url: '//harttle.com/about.html' // Default: location.href
159+
source: 'http://harttle.com' // Any other query string you need...
160+
}
161+
};
162+
```
96163

97-
## links.url
164+
> `appid`是微博认证的App ID,便于微博跟踪。`title``url`用于微博分享内容和参考链接。
98165
99-
Type: `String`
166+
### Wechat(微信)
100167

101-
The `href` attribute for the the share button. For `"wechat"`, `links.url` is the url encoded within the qrcode img.
168+
```javascript
169+
var link = {
170+
plugin: 'wechat'
171+
};
172+
```
173+
174+
`wechat` plugin accept no arguments, while you can still set `links` properties:
175+
176+
```javascript
177+
var link = {
178+
plugin: 'wechat',
179+
url: '//yet.another.url',
180+
color: 'yellow'
181+
};
182+
```
183+
184+
### QR Code(二维码)
185+
186+
```javascript
187+
var link = {
188+
plugin: 'qrcode'
189+
};
190+
```
191+
192+
Just like `wechat` plugin, with different background and icon.
193+
194+
### RSS
195+
196+
```javascript
197+
var link = {
198+
plugin: 'rss'
199+
};
200+
```
201+
202+
### Github
203+
204+
```javascript
205+
var link = {
206+
plugin: 'github',
207+
args: {
208+
id: 'harttle' // Your Github ID
209+
}
210+
};
211+
```
212+
213+
### Linkedin
214+
215+
```javascript
216+
var link = {
217+
plugin: 'linkedin',
218+
args: {
219+
id: 'harttle' // Your linkedin ID
220+
}
221+
};
222+
```
223+
224+
### Google Plus
225+
226+
```javascript
227+
var link = {
228+
plugin: 'google-plus',
229+
args: {
230+
id: 'harttle' // Your Google+ ID
231+
}
232+
};
233+
```
234+
235+
### Twitter
236+
237+
```javascript
238+
var link = {
239+
plugin: 'twitter',
240+
args: {
241+
id: 'harttleharttle' // Your twitter ID
242+
}
243+
};
244+
```
245+
246+
### Facebook
247+
248+
```javascript
249+
var link = {
250+
plugin: 'facebook',
251+
args: {
252+
id: 'harttle' // Your facebook ID
253+
}
254+
}
255+
```
256+
257+
## How to Write Plugins
258+
259+
Plugins are used to generate a `link` Object according to the `links.args`.
260+
For example, the `github` plugin:
261+
262+
```javascript
263+
(function(socialShare) {
264+
socialShare.plugin('github', function(args) {
265+
return {
266+
url: 'https://github.com/' + args.id,
267+
background: '#b5b5b5',
268+
icon: 'fa-github'
269+
};
270+
});
271+
})(window.socialShare);
272+
```
273+
274+
To use this plugin, simply set `plugin` to `"github"`, and specify the args:
275+
276+
```js
277+
var links = [{
278+
plugin: 'github',
279+
args: {
280+
id: 'harttle'
281+
}
282+
}];
283+
```
284+
285+
Which is equavalent to:
286+
287+
```js
288+
var links = [{
289+
url: 'https://github.com/harttle',
290+
background: '#b5b5b5',
291+
icon: 'fa-github'
292+
}];
293+
```
102294

103-
## links.index
295+
## Contribution Guideline
104296

105-
Type: `Number`
297+
It's wellcome to make contributions by any means.
298+
While we suggest the following guide lines:
106299

107-
Optional, share buttons are sorted by `links.index`.
300+
1. Fork this repo.
301+
2. Add your plugin within `src/plugins/`.
302+
3. Run `grunt` to build the `dist/` files.
303+
4. Test your plugin in `demo/index.js`
304+
4. Commit and make a pull request.
108305

109306
[font]: http://fontawesome.io/
110307
[jq]: http://jquery.com/

0 commit comments

Comments
 (0)