From 4a6a65c779a4de11f79fe871193188df6fd3f833 Mon Sep 17 00:00:00 2001 From: Fabio Teixeira Date: Wed, 4 Mar 2015 17:15:21 -0300 Subject: [PATCH] Execute download block javascript only when needed --- lib/software_communities_plugin.rb | 2 +- public/blocks/software-download.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ public/initializer.js | 49 ++++++++++++++++++++++++++----------------------- public/mpog-download-block.js | 38 -------------------------------------- 4 files changed, 73 insertions(+), 62 deletions(-) create mode 100644 public/blocks/software-download.js delete mode 100644 public/mpog-download-block.js diff --git a/lib/software_communities_plugin.rb b/lib/software_communities_plugin.rb index b1f2853..8a8cb8c 100644 --- a/lib/software_communities_plugin.rb +++ b/lib/software_communities_plugin.rb @@ -126,9 +126,9 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin views/create-institution.js views/complete-registration.js views/search-software-catalog.js + blocks/software-download.js initializer.js app.js - mpog-download-block.js ) end diff --git a/public/blocks/software-download.js b/public/blocks/software-download.js new file mode 100644 index 0000000..d4f7da0 --- /dev/null +++ b/public/blocks/software-download.js @@ -0,0 +1,46 @@ +modulejs.define('SoftwareDownload', ['jquery'], function($) { + 'use strict'; + + function add_new_link(){ + var newDownload = $(window.download_list_template); + newDownload.attr('data-counter-id', 1); + $("#droppable-list-downloads").append(newDownload); + } + + + function delete_link(add_element){ + var deleteDownload = $(add_element).parent().parent().parent(); + deleteDownload.remove(); + } + + + function get_download_list_template() { + var blockTemplate = sessionStorage.getItem('download_list_block_template'); + + if(blockTemplate && blockTemplate.length > 0) { + window.download_list_template = blockTemplate; + } else { + $.get('/plugin/software_communities/get_block_template', function(response) { + window.download_list_template = response; + sessionStorage.setItem('download_list_block_template', response); + }); + } + } + + + return { + isCurrentPage: function() { + return $('.download-block').length !== 0; + }, + + + init: function() { + window.add_new_link = add_new_link; + window.delete_link = delete_link; + + if( window.download_list_template === undefined ) { + get_download_list_template(); + } + } + } +}); diff --git a/public/initializer.js b/public/initializer.js index e2ff8ee..8662c27 100644 --- a/public/initializer.js +++ b/public/initializer.js @@ -1,32 +1,35 @@ -var dependencies = [ - 'ControlPanel', - 'EditSoftware', - 'NewSoftware', - 'UserEditProfile', - 'CreateInstitution', - 'CompleteRegistration', - 'SearchSoftwareCatalog' -]; +(function() { + 'use strict'; + var dependencies = [ + 'ControlPanel', + 'EditSoftware', + 'NewSoftware', + 'UserEditProfile', + 'CreateInstitution', + 'CompleteRegistration', + 'SearchSoftwareCatalog', + 'SoftwareDownload' + ]; -modulejs.define('Initializer', dependencies, function() { - 'use strict'; - var __dependencies = arguments; + modulejs.define('Initializer', dependencies, function() { + var __dependencies = arguments; - function call_dependency(dependency) { - if( dependency.isCurrentPage() ) { - dependency.init(); + function call_dependency(dependency) { + if( dependency.isCurrentPage() ) { + dependency.init(); + } } - } - return { - init: function() { - for(var i=0, len = __dependencies.length; i < len; i++) { - call_dependency(__dependencies[i]); + return { + init: function() { + for(var i=0, len = __dependencies.length; i < len; i++) { + call_dependency(__dependencies[i]); + } } - } - }; -}); + }; + }); +})(); diff --git a/public/mpog-download-block.js b/public/mpog-download-block.js deleted file mode 100644 index 5a891ba..0000000 --- a/public/mpog-download-block.js +++ /dev/null @@ -1,38 +0,0 @@ -(function($) { - 'use strict'; - - function add_new_link(){ - var newDownload = $(window.download_list_template); - newDownload.attr('data-counter-id', 1); - $("#droppable-list-downloads").append(newDownload); - } - - - function delete_link(add_element){ - var deleteDownload = $(add_element).parent().parent().parent(); - deleteDownload.remove(); - } - - - function get_download_list_template() { - var blockTemplate = sessionStorage.getItem('download_list_block_template'); - - if(blockTemplate && blockTemplate.length > 0) { - window.download_list_template = blockTemplate; - } else { - $.get('/plugin/software_communities/get_block_template', function(response) { - window.download_list_template = response; - sessionStorage.setItem('download_list_block_template', response); - }); - } - } - - $(document).ready(function() { - window.add_new_link = add_new_link; - window.delete_link = delete_link; - - if( window.download_list_template === undefined ) { - get_download_list_template(); - } - }); -})(jQuery); -- libgit2 0.21.2