/*
Copyright 2016-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the GNU General Public License as published by the Free Software Foundation,
Version 2.0 (the "License"). You may not use this file except in compliance with the License.
A copy of the License is located in the "license" file accompanying this file.
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.
*/
//ToDo: Create the JSON in contexts in PHP instead of creating here and pass directly to Handlebars
//ToDO: Deep-dive to find out if event bubbling with single target is a better option and instead of having a common parent for event delgation with capturing
var aalb_admin_object = (function( $ ) {
var SELECT_DROPDOWN_VALUE = "no-selection";
var SINGLE_ASIN_TEMPLATE = {
PriceLink : 'true',
ProductAd : 'true',
ProductLink: 'true'
};
var AALB_SHORTCODE_AMAZON_LINK = api_pref.AALB_SHORTCODE_AMAZON_LINK;
var AALB_SHORTCODE_AMAZON_TEXT = api_pref.AALB_SHORTCODE_AMAZON_TEXT;
var IS_PAAPI_CREDENTIALS_NOT_SET = api_pref.IS_PAAPI_CREDENTIALS_NOT_SET;
var IS_STORE_ID_CREDENTIALS_NOT_SET = api_pref.IS_STORE_ID_CREDENTIALS_NOT_SET;
var MAX_ALLOWED_ASINS_IN_SELECTION = 10;
var ENTER_KEY_CODE = 13;
var tb_remove = "";
var template;
var link_id = "";
var marketplace_store_id_mapping = $.parseJSON( api_pref.marketplace_store_id_map );
var default_marketplace = api_pref.default_marketplace;
var default_store_id_list = (marketplace_store_id_mapping && marketplace_store_id_mapping[ default_marketplace ]) ? marketplace_store_id_mapping[ default_marketplace ] : [];
var default_store_id = (default_store_id_list.length !== 0) ? default_store_id_list[ 0 ] : "";
var tabs = "";
//tab_counter will be appended to the new tab's id(#aalb_tab2) and will be incremented in code on every new tab addition. 1 is already assigned to defaxlt marketplace tab
var tab_counter = 2;
var marketplace_pop_up_json = [];
var keyword_for_search = "";
var gb_props;
var meta_box_tab_context = {
"searchbox_placeholder" : aalb_strings.searchbox_placeholder,
"search_button_label" : aalb_strings.search_button_label,
"associate_id_label" : aalb_strings.associate_id_label,
"select_associate_id_label" : aalb_strings.select_associate_id_label,
"marketplace_label" : aalb_strings.marketplace_label,
"search_keyword_label" : aalb_strings.search_keyword_label,
"select_marketplace_label" : aalb_strings.select_marketplace_label,
"selected_products_list_label" : aalb_strings.selected_products_list_label,
"click_to_select_products_label" : aalb_strings.click_to_select_products_label,
"text_shown_during_shortcode_creation": aalb_strings.text_shown_during_shortcode_creation,
"marketplace_help_content" : aalb_strings.marketplace_help_content,
"tracking_id_help_content" : aalb_strings.tracking_id_help_content,
"searched_products_box_placeholder" : aalb_strings.searched_products_box_placeholder,
"selected_products_box_placeholder" : aalb_strings.selected_products_box_placeholder,
"marketplace_list" : marketplace_store_id_mapping ? Object.keys( marketplace_store_id_mapping ) : "",
"default_marketplace" : default_marketplace,
"default_store_id_list" : default_store_id_list,
"default_store_id" : default_store_id
};
var search_pop_up_context = {
"meta_box_tab_context" : meta_box_tab_context,
"add_shortcode_button_label": aalb_strings.add_shortcode_button_label,
"ad_template_label" : aalb_strings.ad_template_label,
"templates_help_content" : aalb_strings.templates_help_content,
"templates_list" : $.parseJSON( api_pref.templates_list ),
"default_template" : api_pref.default_template
};
var admin_pop_up_content_context = {
"text_shown_during_search" : aalb_strings.text_shown_during_search,
"check_more_on_amazon_text": aalb_strings.check_more_on_amazon_text
};
$( function() {
//Load the search result template
var hbs_admin_items_search_source = $( "#aalb-hbs-admin-items-search" ).html();
if( hbs_admin_items_search_source != null ) {
template = Handlebars.compile( hbs_admin_items_search_source );
}
//Resize thickbox on window resize
$( window ).on( 'resize', resize_thickbox );
//Storing the tb_remove function of Thickbox.js
var old_tb_remove = window.tb_remove;
//Custom tb_remove function
tb_remove = function() {
reset_add_short_button_and_error_warnings();
//call actual tb_remove
old_tb_remove();
//Emptying the array
marketplace_pop_up_json = [];
tab_counter = 2;
};
/**
* Bind template change using delegated events so that the binding remains when complete pop-up is removed(childs of #aalb-admin-pop-up) & added again
*
**/
$( '#aalb-admin-pop-up' ).on( 'change', '#aalb_template_names_list', function() {
var aalb_add_short_code_button = $( '#aalb-add-shortcode-button' );
//checking for user selected template and number of products selected by user
if( does_any_marketplace_contains_multiple_asin() && SINGLE_ASIN_TEMPLATE[ get_selected_template() ] ) {
$( '#aalb-add-template-asin-error' ).text( aalb_strings.template_asin_error );
aalb_add_short_code_button.prop( 'disabled', true );
} else {
aalb_add_short_code_button.prop( 'disabled', false );
$( '#aalb-add-template-asin-error' ).text( '' );
}
} );
//Bind focus event with dropdown of marketplaces
$( "#aalb-admin-pop-up" ).on( 'focus', '.aalb-marketplace-names-list', function() {
$( this ).data( 'prev-val', $( this ).val() );
} );
/**
* To fill the store-ids as per markeplace in Associate Id section on changing marketplace
**/
$( '#aalb-admin-pop-up' ).on( 'change', '.aalb-marketplace-names-list', function() {
var prev_marketplace = $( this ).data( 'prev-val' );
var new_marketplace = $( this ).val();
if( !marketplace_pop_up_json[ new_marketplace ] ) {
var pop_up_container = $( this ).closest( '.aalb-pop-up-container' );
var store_id_dropdown = pop_up_container.find( '.aalb-admin-popup-store-id' );
reset_store_id_list( store_id_dropdown, marketplace_store_id_mapping[ new_marketplace ] );
if( prev_marketplace !== null ) {
delete marketplace_pop_up_json[ prev_marketplace ];
pop_up_container.find( '.aalb-selected-item' ).remove();
} else {
add_close_button( pop_up_container );
add_tab();
}
add_entry_in_marketplace_json( new_marketplace, marketplace_store_id_mapping[ new_marketplace ][ 0 ] );
change_header_of_tab( pop_up_container, new_marketplace );
admin_popup_search_items( pop_up_container );
} else {
$( this ).val( prev_marketplace || SELECT_DROPDOWN_VALUE );
}
$( this ).blur();
} );
// Close icon: removing the tab on click
$( '#aalb-admin-pop-up' ).on( "click", "span.ui-icon-close", function() {
var marketplace = $( this ).siblings( "a" ).text();
var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
$( "#" + panelId ).remove();
delete marketplace_pop_up_json[ marketplace ];
tabs.tabs( "refresh" );
} );
//Binding on change event of store-id list
$( '#aalb-admin-pop-up' ).on( 'change', '.aalb-admin-popup-store-id', function() {
var pop_up_container = $( this ).closest( '.aalb-pop-up-container' );
marketplace_pop_up_json[ pop_up_container.find( '.aalb-marketplace-names-list' ).val() ].store_id = $( this ).val();
} );
//Binding click event with Search button in search pop-up
$( '#aalb-admin-pop-up' ).on( 'click', '.aalb-admin-popup-search-button', function() {
admin_popup_search_items( $( this ).closest( '.aalb-pop-up-container' ) );
} );
//Binding click event with ASIN removal from selcted item in search pop-up
$( '#aalb-admin-pop-up' ).on( 'click', '.aalb-selected-item', function() {
var aalb_selected_box = $( this ).closest( '.aalb-selected' );
remove_asin( this );
if( aalb_selected_box.find( '.aalb-selected-item' ).length === 0 ) {
aalb_selected_box.find( '.aalb-admin-popup-placeholder' ).show();
}
} );
//Binding click event with ASIN addition from search item in search pop-up
$( '#aalb-admin-pop-up' ).on( "click", '.aalb-admin-item-search-items-item', function() {
var data_asin = $( this ).attr( "data-asin" );
var marketplace = $( this ).closest( '.aalb-pop-up-container' ).find( '.aalb-marketplace-names-list' ).val();
if( !validate_asin_addition( data_asin, marketplace ) ) {
return;
}
marketplace_pop_up_json[ marketplace ].selected_asin.push( data_asin );
var aalb_selected_box = $( this ).closest( '.aalb-pop-up-container' ).find( '.aalb-selected' );
aalb_selected_box.find( '.aalb-admin-popup-placeholder' ).hide();
aalb_selected_box.append( create_selected_asin_html( data_asin, this ) );
} );
//Binding Enter event with Search button in editor search box
$( '#aalb-admin-pop-up' ).on( 'keypress', '.aalb-admin-popup-input-search', function( event ) {
if( event.keyCode === ENTER_KEY_CODE ) {
event.preventDefault();
admin_popup_search_items( $( this ).closest( '.aalb-pop-up-container' ) );
}
} );
if( IS_PAAPI_CREDENTIALS_NOT_SET ) {
disable_editor_search( aalb_strings.paapi_credentials_not_set );
} else if( IS_STORE_ID_CREDENTIALS_NOT_SET ) {
disable_editor_search( aalb_strings.store_id_credentials_not_set );
}
} );
/**
* onKeyPress event handler for editor seach box
*
* @param HTML_DOM_EVENT event OnKeyPress event
* @param HTMLElement caller_element caller of this function
*
* @since 1.5.3
*/
function editor_searchbox_keypress_event_handler( event, caller_element ) {
if( event.keyCode === ENTER_KEY_CODE ) {
event.preventDefault();
admin_show_create_shortcode_popup( $( caller_element ).siblings( '.aalb-admin-button-create-amazon-shortcode' ) );
}
}
/**
* onKeyPress event handler for editor search box for gutenberg editor.
*
* @param HTML_DOM_EVENT event OnKeyPress event
* @param HTMLElement caller_element caller of this function
*
* @since 1.9.0
*/
function gutenberg_editor_onkeypress(event, props) {
if (event.keyCode === ENTER_KEY_CODE) {
event.preventDefault();
admin_show_create_shortcode_popup_gutenberg(props);
}
}
/**
* Returns elements not present in second array but in first
*
* @param Array arr1
* @param Array arr2
*
* @since 1.5.0
*
* @return Array difference between two arrays
*/
function get_diff_between_two_arrays( arr1, arr2 ) {
return arr1.filter( function( a ) {
return arr2.indexOf( a ) == -1;
} );
}
/**
* Resets marketplace drop down with the values provided in new_store_id_list
*
* @param HTMLElement marketplace_dropdown
* @param Array marketplace_list
* @param String selected_marketplace
*
* @since 1.5.0
*/
function reset_marketplace_dropwdown( marketplace_dropdown, new_marketplace_list, selected_marketplace ) {
marketplace_dropdown.empty();
marketplace_dropdown.append( '' );
if( selected_marketplace ) {
marketplace_dropdown.append( '' );
}
$.each( new_marketplace_list, function( key, marketplace ) {
marketplace_dropdown.append( '' );
} );
marketplace_dropdown.val( selected_marketplace || SELECT_DROPDOWN_VALUE );
}
/**
* Adds a close button to the tab
*
* @param jQueryObject pop_up_container The pop up container in which all content in a tab resides
*
* @since 1.5.0
*
*/
function add_close_button( pop_up_container ) {
var close_icon = "";
$( '#aalb-tabs ul li' ).last().append( close_icon );
}
/**
* Change header of tab with marketplace name
*
* @param jQueryObject pop_up_container The pop up container in which all content in a tab resides
* @param String marketplace
*
* @since 1.5.0
*
*/
function change_header_of_tab( pop_up_container, marketplace ) {
var url = '#' + pop_up_container.parent().closest( 'div' ).attr( "id" );
//Why double Quotes & single quote around url :https://stackoverflow.com/questions/31197452/syntax-error-unrecognized-expression-for-href/31197472
$( 'a[href="' + url + '"]' ).text( marketplace );
}
/**
* Checks if any marketplace contains more than one asin selected
*
* @return bool does any marketplace contains multiple asins
*
* @since 1.5.0
*/
function does_any_marketplace_contains_multiple_asin() {
return Object.keys( marketplace_pop_up_json ).some( function( marketplace ) {
return marketplace_pop_up_json[ marketplace ].selected_asin.length > 1;
} );
}
/**
* Checks if any marketplace exists for which no asin is selected
*
* @param String marketplace
*
* @return Array marketplaces list for which no asin is selected
*
* @since 1.5.0
*/
function get_marketplaces_containing_no_asin_selected() {
return Object.keys( marketplace_pop_up_json ).filter( function( marketplace ) {
return marketplace_pop_up_json[ marketplace ].selected_asin.length === 0;
} );
}
/**
* Creates HTML for the selected ASIN from search results
*
* @param String data_asin ASIN to be added
* @param jQueryObject element The search item clicked in search results to be added to selected ASIN list
*
* @return HTMLElement HTML of selected ASIN
*
* @since 1.5.0
*/
function create_selected_asin_html( data_asin, element ) {
var productImage = $( element ).find( "img" ).attr( "src" );
var productTitle = $( element ).find( "div.aalb-admin-item-search-items-item-title" ).text();
var productPrice = $( element ).find( "div.aalb-admin-item-search-items-item-price" ).text();
//ToDO: See if handlebars can be leveraged here like in credentials.js
var selectedAsinHTML = '
';
selectedAsinHTML += '
×';
selectedAsinHTML += '
';
selectedAsinHTML += '
' + productTitle + '
';
selectedAsinHTML += '
' + productPrice + '
';
return selectedAsinHTML;
}
/**
* Add json object with marketplace as key
*
* @param String marketplace
* @param String store_id
*
*
* @since 1.5.0
*/
function add_entry_in_marketplace_json( marketplace, store_id ) {
marketplace_pop_up_json[ marketplace ] = {
"store_id" : store_id,
"selected_asin": []
};
}
/**
* Resets store-id drop down with the values provided in new_store_id_list
*
* @param HTMLElement store_id_dropdown
* @param Array new_store_id_list
*
* @since 1.5.0
*/
function reset_store_id_list( store_id_dropdown, new_store_id_list ) {
store_id_dropdown.empty();
store_id_dropdown.append( '' );
$.each( new_store_id_list, function( key, store_id ) {
store_id_dropdown.append( '' );
} );
store_id_dropdown.val( new_store_id_list[ 0 ] );
}
/**
* Insert Loading search results spinner and content
*
* @param jQueryObject pop_up_container The pop up container in which all content in a tab resides
*
* @since 1.5.0
*/
function insert_search_loading_box( pop_up_container ) {
delete_stale_pop_up_content( pop_up_container );
var admin_pop_up_content_hbs = $( "#aalb-admin-pop-up-content-hbs" ).html();
if( admin_pop_up_content_hbs != null ) {
var admin_pop_up_content_template = Handlebars.compile( admin_pop_up_content_hbs );
var admin_pop_up_content_html = admin_pop_up_content_template( admin_pop_up_content_context );
pop_up_container.find( ".aalb-admin-popup-search-result .aalb-admin-popup-placeholder" ).remove();
pop_up_container.find( ".aalb-admin-popup-search-result" ).append( admin_pop_up_content_html );
}
}
/**
* Delete existing content in pop-up container
*
* @param jQueryObject pop_up_container The pop up container in which all content in a tab resides
*
* @since 1.5.0
*/
function delete_stale_pop_up_content( pop_up_container ) {
var pop_up_content = pop_up_container.find( '.aalb-admin-popup-content' );
if( pop_up_content.length !== 0 ) {
pop_up_content.remove();
}
}
/**
* Adds a new jQuery tab
*
* @since 1.5.0
*/
function add_tab() {
var id = "aalb_tab" + tab_counter++;
var tab_template = "