From 273f499cc2452d0e87b1382f0dccbf32cdc8a797 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 31 Mar 2014 15:22:35 -0400 Subject: [PATCH] moved the duplicated sharing code to requirejs module --- public/src/forum/category.js | 30 ++-------------- public/src/forum/topic/postTools.js | 39 ++------------------ public/src/modules/share.js | 56 +++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 64 deletions(-) create mode 100644 public/src/modules/share.js diff --git a/public/src/forum/category.js b/public/src/forum/category.js index eef2a04207..ee19ccb7f5 100644 --- a/public/src/forum/category.js +++ b/public/src/forum/category.js @@ -1,7 +1,7 @@ "use strict"; /* global define, config, templates, app, ajaxify, socket, translator */ -define(['composer', 'forum/pagination'], function(composer, pagination) { +define(['composer', 'forum/pagination', 'share'], function(composer, pagination, share) { var Category = {}, loadingMoreTopics = false; @@ -10,33 +10,7 @@ define(['composer', 'forum/pagination'], function(composer, pagination) { app.enterRoom('category_' + cid); - $('.twitter-share').on('click', function () { - window.open('https://twitter.com/intent/tweet?url=' + encodeURIComponent(window.location.href) + '&text=' + encodeURIComponent(ajaxify.variables.get('category_name')), '_blank', 'width=550,height=420,scrollbars=no,status=no'); - return false; - }); - - $('.facebook-share').on('click', function () { - window.open('https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(window.location.href), '_blank', 'width=626,height=436,scrollbars=no,status=no'); - return false; - }); - - $('.google-share').on('click', function () { - window.open('https://plus.google.com/share?url=' + encodeURIComponent(window.location.href), '_blank', 'width=500,height=570,scrollbars=no,status=no'); - return false; - }); - - $('.share-dropdown').on('shown.bs.dropdown', function() { - $('#category-link').val(window.location.protocol + '//' + window.location.host + window.location.pathname); - // without the setTimeout can't select the text in the input - setTimeout(function() { - $('#category-link').putCursorAtEnd().select(); - }, 50); - }); - - $('.post-link').on('click', function(e) { - e.preventDefault(); - return false; - }); + share.addShareHandlers(ajaxify.variables.get('category_name')); $('#new_post').on('click', function () { composer.newTopic(cid); diff --git a/public/src/forum/topic/postTools.js b/public/src/forum/topic/postTools.js index 4b355b2dbc..26cbeb14c8 100644 --- a/public/src/forum/topic/postTools.js +++ b/public/src/forum/topic/postTools.js @@ -2,7 +2,7 @@ /* globals define, app, translator, ajaxify, socket, bootbox */ -define(['composer'], function(composer) { +define(['composer', 'share'], function(composer, share) { var PostTools = {}, topicName; @@ -12,7 +12,7 @@ define(['composer'], function(composer) { addPostHandlers(tid, threadState); - addShareHandlers(); + share.addShareHandlers(topicName); }; function addPostHandlers(tid, threadState) { @@ -21,6 +21,7 @@ define(['composer'], function(composer) { onReplyClicked($(this), tid, topicName); } }); + var postContainer = $('#post-container'); postContainer.on('click', '.quote', function() { @@ -228,39 +229,5 @@ define(['composer'], function(composer) { return false; } - function addShareHandlers() { - - function openShare(url, pid, width, height) { - window.open(url + encodeURIComponent(window.location.protocol + '//' + window.location.host + window.location.pathname + '#' + pid), '_blank', 'width=' + width + ',height=' + height + ',scrollbars=no,status=no'); - return false; - } - - $('#post-container').on('shown.bs.dropdown', '.share-dropdown', function() { - var pid = getPid($(this)); - $('#post_' + pid + '_link').val(window.location.protocol + '//' + window.location.host + window.location.pathname + '#' + pid); - // without the setTimeout can't select the text in the input - setTimeout(function() { - $('#post_' + pid + '_link').putCursorAtEnd().select(); - }, 50); - }); - - $('#post-container').on('click', '.post-link', function(e) { - e.preventDefault(); - return false; - }); - - $('#post-container').on('click', '.twitter-share', function () { - return openShare('https://twitter.com/intent/tweet?text=' + topicName + '&url=', getPid($(this)), 550, 420); - }); - - $('#post-container').on('click', '.facebook-share', function () { - return openShare('https://www.facebook.com/sharer/sharer.php?u=', getPid($(this)), 626, 436); - }); - - $('#post-container').on('click', '.google-share', function () { - return openShare('https://plus.google.com/share?url=', getPid($(this)), 500, 570); - }); - } - return PostTools; }); \ No newline at end of file diff --git a/public/src/modules/share.js b/public/src/modules/share.js new file mode 100644 index 0000000000..61da02d7ac --- /dev/null +++ b/public/src/modules/share.js @@ -0,0 +1,56 @@ +'use strict'; + +/* globals define, app, translator, ajaxify, socket, bootbox */ + +define(function() { + + var module = {}; + + module.addShareHandlers = function(name) { + + var baseUrl = window.location.protocol + '//' + window.location.host + window.location.pathname; + + function openShare(url, hash, width, height) { + window.open(url + encodeURIComponent(baseUrl + hash), '_blank', 'width=' + width + ',height=' + height + ',scrollbars=no,status=no'); + return false; + } + + $('#content').on('shown.bs.dropdown', '.share-dropdown', function() { + + var postLink = $(this).find('.post-link'); + postLink.val(baseUrl + getPostHash($(this))); + + // without the setTimeout can't select the text in the input + setTimeout(function() { + postLink.putCursorAtEnd().select(); + }, 50); + }); + + $('#content').on('click', '.post-link', function(e) { + e.preventDefault(); + return false; + }); + + $('#content').on('click', '.twitter-share', function () { + return openShare('https://twitter.com/intent/tweet?text=' + name + '&url=', getPostHash($(this)), 550, 420); + }); + + $('#content').on('click', '.facebook-share', function () { + return openShare('https://www.facebook.com/sharer/sharer.php?u=', getPostHash($(this)), 626, 436); + }); + + $('#content').on('click', '.google-share', function () { + return openShare('https://plus.google.com/share?url=', getPostHash($(this)), 500, 570); + }); + }; + + function getPostHash(clickedElement) { + var pid = clickedElement.parents('.post-row').attr('data-pid'); + if (pid) { + return '#' + pid; + } + return ''; + } + + return module; +}); \ No newline at end of file