From e5ba920187387e9bb382708a4ebd38482b2e8544 Mon Sep 17 00:00:00 2001 From: tDidierjean Date: Mon, 2 Apr 2012 12:46:47 +0000 Subject: [PATCH] // #PI-107 BO product page JS refactoring --- .../controllers/products/accounting.tpl | 6 - .../controllers/products/associations.tpl | 41 +----- .../products/helpers/form/form.tpl | 21 ++- controllers/admin/AdminProductsController.php | 2 +- js/admin-product-tab.js | 112 ++++++++++++++++ js/admin-products.js | 123 +++++++----------- 6 files changed, 169 insertions(+), 136 deletions(-) create mode 100644 js/admin-product-tab.js diff --git a/admin-dev/themes/default/template/controllers/products/accounting.tpl b/admin-dev/themes/default/template/controllers/products/accounting.tpl index f3a8edc84..6205a7694 100644 --- a/admin-dev/themes/default/template/controllers/products/accounting.tpl +++ b/admin-dev/themes/default/template/controllers/products/accounting.tpl @@ -24,12 +24,6 @@ * International Registered Trademark & Property of PrestaShop SA *} - - {if isset($product->id)}

{l s='Accounting'}

diff --git a/admin-dev/themes/default/template/controllers/products/associations.tpl b/admin-dev/themes/default/template/controllers/products/associations.tpl index 5915b0e31..0a8d74ccf 100644 --- a/admin-dev/themes/default/template/controllers/products/associations.tpl +++ b/admin-dev/themes/default/template/controllers/products/associations.tpl @@ -117,43 +117,4 @@ - - - \ No newline at end of file + \ No newline at end of file diff --git a/admin-dev/themes/default/template/controllers/products/helpers/form/form.tpl b/admin-dev/themes/default/template/controllers/products/helpers/form/form.tpl index eac43de1b..2f292bf6d 100644 --- a/admin-dev/themes/default/template/controllers/products/helpers/form/form.tpl +++ b/admin-dev/themes/default/template/controllers/products/helpers/form/form.tpl @@ -28,7 +28,7 @@ {block name="autoload_tinyMCE"} // Execute when tab Informations has finished loading - onTabLoad('Informations', function(){ + new ProductTab('Informations').onDisplay(function(){ // change each by click to load only on click $(".autoload_rte").each(function(e){ tinySetup({ @@ -98,12 +98,12 @@ product_type = $("input[name=type_product]:checked").val(); $('#product-tab-content-wait').show(); {if $is_pack} - onTabLoad('Informations', function(){ + new ProductTab('Informations').onDisplay(function(){ $('#pack_product').attr('checked', 'checked'); }); $('li.tab-row a[id*="VirtualProduct"]').hide(); {elseif $product->is_virtual} - onTabLoad('Informations', function(){ + new ProductTab('Informations').onDisplay(function(){ $('#virtual_product').attr('checked', 'checked'); $('#condition').attr('disabled', 'disabled'); $('#condition option[value=new]').attr('selected', 'selected'); @@ -111,7 +111,7 @@ $('li.tab-row a[id*="Pack"]').hide(); $('li.tab-row a[id*="Shipping"]').hide(); {else} - onTabLoad('Informations', function(){ + new ProductTab('Informations').onDisplay(function(){ $('#simple_product').attr('checked', 'checked'); }); $('li.tab-row a[id*="Pack"]').hide(); @@ -159,20 +159,20 @@ { $('li.tab-row a[id*="VirtualProduct"]').show().click(); - onTabLoad('VirtualProduct', function(){ + new ProductTab('VirtualProduct').onDisplay(function(){ $('#is_virtual_good').attr('checked', true); $('#virtual_good').show(); $('#is_virtual').val(1); $("#virtual_good_attributes").show(); }); - onTabLoad('Quantities', function(){ + new ProductTab('Quantities').onDisplay(function(){ $('.stockForVirtualProduct').hide(); }); $('li.tab-row a[id*="Shipping"]').hide(); - onTabLoad('Informations', function(){ + new ProductTab('Informations').onDisplay(function(){ $('#condition').attr('disabled', 'disabled'); $('#condition option[value=refurbished]').removeAttr('selected'); $('#condition option[value=used]').removeAttr('selected'); @@ -215,7 +215,7 @@ } if ($("#product-tab-content-"+id).hasClass('not-loaded') && !$("#product-tab-content-"+id).hasClass('loading')) - displayTabProductById(id, true, 0, null); + ProductTab.display(id, true, 0, null); else { $("#product-tab-content-"+id).show(0, function(){ @@ -249,8 +249,7 @@ if ($('#product-tab-content-Associations').hasClass('not-loaded')) disableSave(); - $('#product-tab-content-Associations').bind('loaded', function() - { + new ProductTab('Associations').onDisplay(function(){ handleSaveButtons(); }); @@ -276,7 +275,7 @@ {/foreach} // Recursively load tabs starting with the first element of stack - displayTabProductById(tabs_to_preload[0], false, 0, tabs_to_preload); + ProductTab.display(tabs_to_preload[0], false, 0, tabs_to_preload); $('.productTabs').show(); $('#product_form').show(); diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index e750a0799..23ee9558b 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -1895,7 +1895,6 @@ class AdminProductsControllerCore extends AdminController { if ($this->display == 'edit' || $this->display == 'add') { - $this->addJS(_PS_JS_DIR_.'admin-products.js'); $this->fields_form = array(); // Check if Module @@ -3949,6 +3948,7 @@ class AdminProductsControllerCore extends AdminController )); $this->addJS(array( + _PS_JS_DIR_.'admin-product-tab.js', _PS_JS_DIR_.'admin-products.js', _PS_JS_DIR_.'attributesBack.js', _PS_JS_DIR_.'price.js', diff --git a/js/admin-product-tab.js b/js/admin-product-tab.js new file mode 100644 index 000000000..d3c742d4a --- /dev/null +++ b/js/admin-product-tab.js @@ -0,0 +1,112 @@ +/* +* +* NOTICE OF LICENSE +* +* This source file is subject to the Open Software License (OSL 3.0) +* that is bundled with this package in the file LICENSE.txt. +* It is also available through the world-wide-web at this URL: +* http://opensource.org/licenses/osl-3.0.php +* If you did not receive a copy of the license and are unable to +* obtain it through the world-wide-web, please send an email +* to license@prestashop.com so we can send you a copy immediately. +* +* DISCLAIMER +* +* Do not edit or add to this file if you wish to upgrade PrestaShop to newer +* versions in the future. If you wish to customize PrestaShop for your +* needs please refer to http://www.prestashop.com for more information. +* +* @author PrestaShop SA +* @copyright 2007-2012 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +var ProductTab = function(name){ + this.name = name; + this.id; + this.container; + + /** + * Execute a callback function when a specific tab has finished loading or right now if the tab is already loaded + * + * @param tab_name name of the tab that is checked for loading + * @param callback_function function to call + */ + this.onDisplay = function (callback_function) + { + if (typeof(this.selector) === 'undefined') + this.container = $('#product-tab-content-' + this.name); + + if (this.container.length === 0) + throw 'Could not find container for tab name: ' + this.name; + + if (this.container.hasClass('not-loaded')) + this.container.bind('loaded', callback_function); + else + callback_function(); + } +} + +/** + * Get a single tab or recursively get tabs in stack then display them + * + * @param int id position of the tab in the product page + * @param boolean selected is the tab selected + * @param int index current index in the stack (or 0) + * @param array stack list of tab ids to load (or null) + */ +ProductTab.display = function (id, selected, index, stack) +{ + var myurl = $('#link-'+id).attr("href")+"&ajax=1"; + var tab_selector = $("#product-tab-content-"+id); + // Used to check if the tab is already in the process of being loaded + tab_selector.addClass('loading'); + + if (selected) + $('#product-tab-content-wait').show(); + + $.ajax({ + url : myurl, + async : true, + cache: false, // cache needs to be set to false or IE will cache the page with outdated product values + type: 'POST', + success : function(data) + { + tab_selector.html(data); + tab_selector.removeClass('not-loaded'); + + if (selected) + { + $("#link-"+id).addClass('selected'); + tab_selector.show(); + } + }, + complete : function(data) + { + $("#product-tab-content-"+id).removeClass('loading'); + if (selected) + { + $('#product-tab-content-wait').hide(); + tab_selector.trigger('displayed'); + } + tab_selector.trigger('loaded'); + if (stack && stack[index + 1]) + ProductTab.display(stack[index + 1], selected, index + 1, stack); + }, + beforeSend : function(data) + { + // don't display the loading notification bar + if (typeof(ajax_running_timeout) !== 'undefined') + clearTimeout(ajax_running_timeout); + } + }); +} + +//ProductTab.tabs = +// +//ProductTab.getByName(name) +//{ +// +//} \ No newline at end of file diff --git a/js/admin-products.js b/js/admin-products.js index 13496c0eb..6fbd05600 100644 --- a/js/admin-products.js +++ b/js/admin-products.js @@ -205,61 +205,6 @@ function editProductAttribute(url, parent) } /* END Combination */ -/** - * Get a single tab or recursively get tabs in stack then display them - * - * @param int id position of the tab in the product page - * @param boolean selected is the tab selected - * @param int index current index in the stack (or 0) - * @param array stack list of tab ids to load (or null) - */ -function displayTabProductById(id, selected, index, stack) -{ - var myurl = $('#link-'+id).attr("href")+"&ajax=1"; - var tab_selector = $("#product-tab-content-"+id); - // Used to check if the tab is already in the process of being loaded - tab_selector.addClass('loading'); - - if (selected) - $('#product-tab-content-wait').show(); - - $.ajax({ - url : myurl, - async : true, - cache: false, // cache needs to be set to false or IE will cache the page with outdated product values - type: 'POST', - success : function(data) - { - tab_selector.html(data); - tab_selector.removeClass('not-loaded'); - - if (selected) - { - $("#link-"+id).addClass('selected'); - tab_selector.show(); - } - }, - complete : function(data) - { - $("#product-tab-content-"+id).removeClass('loading'); - if (selected) - { - $('#product-tab-content-wait').hide(); - tab_selector.trigger('displayed'); - } - tab_selector.trigger('loaded'); - if (stack && stack[index + 1]) - displayTabProductById(stack[index + 1], selected, index + 1, stack); - }, - beforeSend : function(data) - { - // don't display the loading notification bar - if (typeof(ajax_running_timeout) !== 'undefined') - clearTimeout(ajax_running_timeout); - } - }); -} - /** * Update the manufacturer select element with the list of existing manufacturers */ @@ -445,25 +390,42 @@ function deleteSpecificPrice(url, parent) }); } -/** - * Execute a callback function when a specific tab has finished loading or right now if the tab is already loaded - * - * @param tab_name name of the tab that is checked for loading - * @param callback_function function to call - */ -function onTabLoad(tab_name, callback_function) +function initAccessoriesAutocomplete() { - var target_tab = $('#product-tab-content-' + tab_name); - if (!target_tab) - return false; - if (target_tab.hasClass('not-loaded')) - target_tab.bind('loaded', callback_function); - else - callback_function(); -} + /* function autocomplete */ + $(document).ready(function() { + $('#product_autocomplete_input') + .autocomplete('ajax_products_list.php', { + minChars: 1, + autoFill: true, + max:20, + matchContains: true, + mustMatch:true, + scroll:false, + cacheLength:0, + formatItem: function(item) { + return item[1]+' - '+item[0]; + } + }).result(addAccessory); -/* function autocomplete */ -urlToCall = null; + $('#product_autocomplete_input').setOptions({ + extraParams: { + excludeIds : getAccessorieIds() + } + }); + + + + function getAccessorieIds() + { + var ids = id_product + ','; + ids += $('#inputAccessories').val().replace(/\\-/g,',').replace(/\\,$/,''); + ids = ids.replace(/\,$/,''); + + return ids; + } + }); +} $(document).ready(function() { updateCurrentText(); @@ -491,13 +453,13 @@ $(document).ready(function() { }); // Enable writing of the product name when the friendly url field in tab SEO is loaded - onTabLoad('Seo', enableProductName); + new ProductTab('Seo').onDisplay(enableProductName); - // Bind to show/hide new specific price form - onTabLoad('Prices', toggleSpecificPrice); + new ProductTab('Prices').onDisplay(function(){ + // Bind to show/hide new specific price form + toggleSpecificPrice(); - // Bind to delete specific price link - onTabLoad('Prices', function(){ + // Bind to delete specific price link $('#specific_prices_list').delegate('a[name="delete_link"]', 'click', function(e){ e.preventDefault(); deleteSpecificPrice(this.href, $(this).parents('tr')); @@ -505,7 +467,7 @@ $(document).ready(function() { }); // Bind attribute list ajax actions (edit, default, delete) - onTabLoad('Combinations', function(){ + new ProductTab('Combinations').onDisplay(function(){ $('table[name=list_table]').delegate('a.edit', 'click', function(e){ e.preventDefault(); editProductAttribute(this.href, $(this).closest('tr')); @@ -521,4 +483,9 @@ $(document).ready(function() { defaultProductAttribute(this.href, $(this).closest('tr')); }); }); + + new ProductTab('Associations').onDisplay(function(){ + initAccessoriesAutocomplete(); + getManufacturers(); + }); });