Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
gwagroves committed Oct 28, 2015
1 parent abb1b57 commit 35dd8af
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bower_components
node_modules
75 changes: 75 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({

pkg: grunt.file.readJSON('package.json'),

// tasks

jshint: {
options: {
jshintrc: 'bower_components/gwa-codestyle/rc/.jshintrc'
},
src: [
'src/ShareLinks.js'
]
},

jasmine: {
require: {
options: {
vendor: [
// 'bower_components/requirejs/require.js'
],
specs: [
'tests/ShareLinks.test.js'
],
template: require('grunt-template-jasmine-requirejs'),
templateOptions: {
requireConfig: {
baseUrl: './',
paths: {
'jquery' : 'bower_components/jquery/dist/jquery',
'Gwa.ShareLinks' : 'src/ShareLinks'
}
}
}
}
}
},

copy: {
main: {
files: [
{src:'src/ShareLinks.js', dest:'dist/ShareLinks.js'}
]
}
},

uglify: {
main: {
files: {
'dist/ShareLinks.min.js': ['src/ShareLinks.js']
}
}
}
});

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-jscs');
grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');

grunt.registerTask(
'default',
[
'jshint:src',
'jasmine',
'copy',
'uglify'
]
);

};
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# Share-Links
Simple non-api share links for Facebook & Twitter

Simple non-api share links for Facebook & Twitter.

## Usage

```markup
<a href="http://linkto.share.com/" class="fb-share">Share on Facebook</a>
<a href="http://linkto.share.com/" data-content="#hashtag content" class="tw-share">Share on Twitter</a>
```

```js
gwa.ShareLinks.initFacebook($('a.fb-share'));
gwa.ShareLinks.initTwitter($('a.tw-share'));
```
28 changes: 28 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "gwa-share-Links",
"main": "",
"repository": {
"type": "git",
"url": "https://github.com/gwa/Share-Links.git"
},
"license": "MIT",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests",
"grunt",
"Gruntfile.js",
"package.json",
"src"
],
"devDependencies": {
"gwa-codestyle": "gwa/codestyle#~1.0",
"requirejs": "~2.1.20"
},
"dependencies": {
"jquery": ">1.11.0"
}
}
42 changes: 42 additions & 0 deletions dist/ShareLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* global define */
(function( root, factory ) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
root.gwa = typeof root.gwa === 'undefined' ? {} : root.gwa;
root.gwa.ShareLinks = factory(jQuery);
}
}(this, function($) {

function getContentData(jq) {
return jq.attr('data-content') ? jq.attr('data-content') : '';
}

var instance = {
initFacebook: function(jq) {
jq.on('click', function(ev) {
var permalink = $(this).attr('href');
ev.preventDefault();
instance.opener('https://www.facebook.com/sharer.php?u=' + permalink, 'facebook', 'toolbar=no,width=700,height=400');
});
},

initTwitter: function(jq) {
jq.on('click', function(ev) {
var permalink = $(this).attr('href'),
content = getContentData($(this)),
fullcontent = content ? encodeURIComponent(content + ' ' + permalink) : permalink;

ev.preventDefault();
instance.opener('https://www.twitter.com/home?status=' + fullcontent, 'twitter', 'toolbar=no,width=700,height=400');
});
},

opener: window.open
};

return instance;

}));
2 changes: 2 additions & 0 deletions dist/ShareLinks.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/ShareLinks.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Gwa-Share-Links",
"repository": {
"type": "git",
"url": "https://github.com/gwa/Share-Links"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-copy": "^0.8.2",
"grunt-contrib-jasmine": "^0.6.3",
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-uglify": "^0.10.0",
"grunt-template-jasmine-requirejs": "^0.2.0"
}
}
42 changes: 42 additions & 0 deletions src/ShareLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* global define */
(function( root, factory ) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
root.gwa = typeof root.gwa === 'undefined' ? {} : root.gwa;
root.gwa.ShareLinks = factory(jQuery);
}
}(this, function($) {

function getContentData(jq) {
return jq.attr('data-content') ? jq.attr('data-content') : '';
}

var instance = {
initFacebook: function(jq) {
jq.on('click', function(ev) {
var permalink = $(this).attr('href');
ev.preventDefault();
instance.opener('https://www.facebook.com/sharer.php?u=' + permalink, 'facebook', 'toolbar=no,width=700,height=400');
});
},

initTwitter: function(jq) {
jq.on('click', function(ev) {
var permalink = $(this).attr('href'),
content = getContentData($(this)),
fullcontent = content ? encodeURIComponent(content + ' ' + permalink) : permalink;

ev.preventDefault();
instance.opener('https://www.twitter.com/home?status=' + fullcontent, 'twitter', 'toolbar=no,width=700,height=400');
});
},

opener: window.open
};

return instance;

}));
41 changes: 41 additions & 0 deletions tests/ShareLinks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
define(['jquery', 'Gwa.ShareLinks'], function($, ShareLinks) {

describe("ShareLinks", function() {

it("has jquery as a dependency", function () {
expect($).toBeDefined();
});

it("exists as an object", function() {
expect(ShareLinks).toBeDefined();
});

it("can init a FB link", function() {
var result,
jq = $('<a href="http://www.example.com/">My link</a>');

ShareLinks.opener = function(url) {
result = url;
};

ShareLinks.initFacebook(jq);
jq.trigger('click');
expect(result).toBe('https://www.facebook.com/sharer.php?u=http://www.example.com/');
});

it("can init a Twitter link", function() {
var result,
jq = $('<a href="http://www.example.com/" data-content="#foo @bar">My link</a>');

ShareLinks.opener = function(url) {
result = url;
};

ShareLinks.initTwitter(jq);
jq.trigger('click');
expect(result).toBe('https://www.twitter.com/home?status=%23foo%20%40bar%20http%3A%2F%2Fwww.example.com%2F');
});

});

});

0 comments on commit 35dd8af

Please sign in to comment.