From 35dd8af51f5bf13db4763ba0e578a64ac4426a19 Mon Sep 17 00:00:00 2001 From: Timothy Groves Date: Wed, 28 Oct 2015 13:40:10 +0100 Subject: [PATCH] Initial release --- .gitignore | 2 + Gruntfile.js | 75 ++++++++++++++++++++++++++++++++++++++ README.md | 15 +++++++- bower.json | 28 ++++++++++++++ dist/ShareLinks.js | 42 +++++++++++++++++++++ dist/ShareLinks.min.js | 2 + dist/ShareLinks.min.js.map | 1 + package.json | 15 ++++++++ src/ShareLinks.js | 42 +++++++++++++++++++++ tests/ShareLinks.test.js | 41 +++++++++++++++++++++ 10 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Gruntfile.js create mode 100644 bower.json create mode 100644 dist/ShareLinks.js create mode 100644 dist/ShareLinks.min.js create mode 100644 dist/ShareLinks.min.js.map create mode 100644 package.json create mode 100644 src/ShareLinks.js create mode 100644 tests/ShareLinks.test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7bf6eb1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bower_components +node_modules diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..adae592 --- /dev/null +++ b/Gruntfile.js @@ -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' + ] + ); + +}; diff --git a/README.md b/README.md index 1de3e68..9ab03d1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # Share-Links -Simple non-api share links for Facebook & Twitter + +Simple non-api share links for Facebook & Twitter. + +## Usage + +```markup +Share on Facebook +Share on Twitter +``` + +```js +gwa.ShareLinks.initFacebook($('a.fb-share')); +gwa.ShareLinks.initTwitter($('a.tw-share')); +``` diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..1029f8d --- /dev/null +++ b/bower.json @@ -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" + } +} diff --git a/dist/ShareLinks.js b/dist/ShareLinks.js new file mode 100644 index 0000000..379270b --- /dev/null +++ b/dist/ShareLinks.js @@ -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; + +})); diff --git a/dist/ShareLinks.min.js b/dist/ShareLinks.min.js new file mode 100644 index 0000000..61672d7 --- /dev/null +++ b/dist/ShareLinks.min.js @@ -0,0 +1,2 @@ +!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],b):(a.gwa="undefined"==typeof a.gwa?{}:a.gwa,a.gwa.ShareLinks=b(jQuery))}(this,function(a){function b(a){return a.attr("data-content")?a.attr("data-content"):""}var c={initFacebook:function(b){b.on("click",function(b){var d=a(this).attr("href");b.preventDefault(),c.opener("https://www.facebook.com/sharer.php?u="+d,"facebook","toolbar=no,width=700,height=400")})},initTwitter:function(d){d.on("click",function(d){var e=a(this).attr("href"),f=b(a(this)),g=f?encodeURIComponent(f+" "+e):e;d.preventDefault(),c.opener("https://www.twitter.com/home?status="+g,"twitter","toolbar=no,width=700,height=400")})},opener:window.open};return c}); +//# sourceMappingURL=ShareLinks.min.js.map \ No newline at end of file diff --git a/dist/ShareLinks.min.js.map b/dist/ShareLinks.min.js.map new file mode 100644 index 0000000..43fbb1f --- /dev/null +++ b/dist/ShareLinks.min.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../src/ShareLinks.js"],"names":["root","factory","define","amd","gwa","ShareLinks","jQuery","this","$","getContentData","jq","attr","instance","initFacebook","on","ev","permalink","preventDefault","opener","initTwitter","content","fullcontent","encodeURIComponent","window","open"],"mappings":"CACC,SAAUA,EAAMC,GACM,kBAAXC,SAAyBA,OAAOC,IAE1CD,QAAQ,UAAWD,IAGnBD,EAAKI,IAA0B,mBAAbJ,GAAKI,OAA2BJ,EAAKI,IACvDJ,EAAKI,IAAIC,WAAaJ,EAAQK,UAE9BC,KAAM,SAASC,GAEhB,QAASC,GAAeC,GACvB,MAAOA,GAAGC,KAAK,gBAAkBD,EAAGC,KAAK,gBAAkB,GAG5D,GAAIC,IACHC,aAAc,SAASH,GACtBA,EAAGI,GAAG,QAAS,SAASC,GACvB,GAAIC,GAAYR,EAAED,MAAMI,KAAK,OAC7BI,GAAGE,iBACHL,EAASM,OAAO,yCAA2CF,EAAW,WAAY,sCAIpFG,YAAa,SAAST,GACrBA,EAAGI,GAAG,QAAS,SAASC,GACvB,GAAIC,GAAYR,EAAED,MAAMI,KAAK,QAC7BS,EAAUX,EAAeD,EAAED,OAC3Bc,EAAcD,EAAUE,mBAAmBF,EAAU,IAAMJ,GAAaA,CAExED,GAAGE,iBACHL,EAASM,OAAO,uCAAyCG,EAAa,UAAW,sCAInFH,OAAQK,OAAOC,KAGhB,OAAOZ","file":"ShareLinks.min.js"} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..b430541 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/src/ShareLinks.js b/src/ShareLinks.js new file mode 100644 index 0000000..379270b --- /dev/null +++ b/src/ShareLinks.js @@ -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; + +})); diff --git a/tests/ShareLinks.test.js b/tests/ShareLinks.test.js new file mode 100644 index 0000000..aa84b34 --- /dev/null +++ b/tests/ShareLinks.test.js @@ -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 = $('My link'); + + 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 = $('My link'); + + 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'); + }); + + }); + +});