| 1 |
'use strict'; |
| 2 |
|
| 3 |
jQuery(document).ready(function ($) { |
| 4 |
|
| 5 |
const selectors = { |
| 6 |
settings: '.wpie-settings__main', |
| 7 |
items_list: '.wpie-items__list', |
| 8 |
full_editor: '.wpie-fulleditor', |
| 9 |
text_editor: '.wpie-texteditor', |
| 10 |
color_picker: '.wpie-color', |
| 11 |
icon_picker: '[data-field="menu_1-item_icon"]', |
| 12 |
item_type: '[data-field="menu_1-item_type"]', |
| 13 |
checkbox: '.wpie-field input[type="checkbox"]', |
| 14 |
add_item: '.wpie-add-button', |
| 15 |
custom_icon: '[data-field="menu_1-item_custom"]', |
| 16 |
text_icon: '[data-field="menu_1-item_custom_text_check"]', |
| 17 |
enable_tracking: '[data-field="menu_1-enable_tracking"]', |
| 18 |
item: '#wpie-items-list .wpie-item', |
| 19 |
item_remove: '.wpie-item_heading .wpie_icon-trash', |
| 20 |
item_heading: '.wpie-item .wpie-item_heading', |
| 21 |
delete_link: '.wpie-link-delete, .delete a', |
| 22 |
}; |
| 23 |
|
| 24 |
$(selectors.full_editor).wowFullEditor(); |
| 25 |
|
| 26 |
function set_up() { |
| 27 |
|
| 28 |
$(selectors.text_editor).wowTextEditor(); |
| 29 |
$(selectors.icon_picker).wowIconPicker(); |
| 30 |
$(selectors.color_picker).wpColorPicker({ |
| 31 |
change: function(event, ui){ $(selectors.item).wowFloatMenuLiveBuilder(); }, |
| 32 |
}); |
| 33 |
$(selectors.item).wowFloatMenuLiveBuilder(); |
| 34 |
$(selectors.items_list).sortable({ |
| 35 |
items: '> .wpie-item', |
| 36 |
placeholder: "wpie-item ui-state-highlight", |
| 37 |
}); |
| 38 |
$(selectors.items_list).disableSelection(); |
| 39 |
|
| 40 |
$(selectors.checkbox).each(set_checkbox); |
| 41 |
$(selectors.item_type).each(item_type); |
| 42 |
|
| 43 |
$(selectors.custom_icon).each(custom_icon); |
| 44 |
$(selectors.text_icon).each(custom_icon); |
| 45 |
$(selectors.enable_tracking).each(enable_tracking); |
| 46 |
} |
| 47 |
|
| 48 |
function initialize_events() { |
| 49 |
$(selectors.settings).on('change', selectors.checkbox, set_checkbox); |
| 50 |
$(selectors.settings).on('click', selectors.add_item, clone_button); |
| 51 |
$(selectors.settings).on('change', selectors.item_type, item_type); |
| 52 |
$(selectors.settings).on('change', selectors.custom_icon, custom_icon); |
| 53 |
$(selectors.settings).on('change', selectors.text_icon, custom_icon); |
| 54 |
$(selectors.settings).on('change', selectors.enable_tracking, enable_tracking); |
| 55 |
$(selectors.settings).on('click', selectors.item_remove, item_remove); |
| 56 |
$(selectors.settings).on('click', selectors.item_heading, item_toggle); |
| 57 |
$(document).on('click', selectors.delete_link, delete_menu); |
| 58 |
|
| 59 |
$(selectors.settings).on('change click keyup', selectors.item, function (){ |
| 60 |
$(selectors.item).wowFloatMenuLiveBuilder(); |
| 61 |
}); |
| 62 |
} |
| 63 |
|
| 64 |
function initialize() { |
| 65 |
set_up(); |
| 66 |
initialize_events(); |
| 67 |
} |
| 68 |
|
| 69 |
// Set the checkboxes |
| 70 |
function set_checkbox() { |
| 71 |
const next = $(this).next('input[type="hidden"]'); |
| 72 |
if ($(this).is(':checked')) { |
| 73 |
next.val('1'); |
| 74 |
} else { |
| 75 |
next.val('0'); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// Clone menu item |
| 80 |
function clone_button(e) { |
| 81 |
e.preventDefault(); |
| 82 |
const parent = get_parent_fields($(this), '.wpie-items__list'); |
| 83 |
const selector = $(parent).find('.wpie-buttons__hr'); |
| 84 |
const template = $('#template-button').clone().html(); |
| 85 |
|
| 86 |
$(template).insertBefore($(selector)); |
| 87 |
|
| 88 |
set_up(); |
| 89 |
} |
| 90 |
|
| 91 |
// Change the button Type |
| 92 |
function item_type() { |
| 93 |
const parent = get_parent_fields($(this)); |
| 94 |
const box = get_field_box($(this)); |
| 95 |
const type = $(this).val(); |
| 96 |
const fields = parent.find('[data-field-box]').not(box); |
| 97 |
const parentTab = get_parent_fields($(this), '.wpie-tabs-wrapper'); |
| 98 |
|
| 99 |
parentTab.find('.wpie-tab__type-menu').addClass('is-hidden'); |
| 100 |
fields.addClass('is-hidden'); |
| 101 |
|
| 102 |
const linkText = parent.find('[data-field-box="menu_1-item_link"] .wpie-field__title'); |
| 103 |
linkText.text('Link'); |
| 104 |
|
| 105 |
// Mapping menu types to the respective field boxes. |
| 106 |
const typeFieldMapping = { |
| 107 |
link: ['menu_1-item_link', 'menu_1-new_tab'], |
| 108 |
share: ['menu_1-item_share'], |
| 109 |
translate: ['menu_1-gtranslate'], |
| 110 |
smoothscroll: ['menu_1-item_link'], |
| 111 |
login: ['menu_1-item_link'], |
| 112 |
logout: ['menu_1-item_link'], |
| 113 |
lostpassword: ['menu_1-item_link'], |
| 114 |
email: ['menu_1-item_link'], |
| 115 |
telephone: ['menu_1-item_link'], |
| 116 |
id: ['menu_1-item_modal'], |
| 117 |
class: ['menu_1-item_modal'], |
| 118 |
text: [] // Empty array here because text case has specific behavior. |
| 119 |
}; |
| 120 |
|
| 121 |
// Customize the link text for certain types |
| 122 |
const linkTextMapping = { |
| 123 |
login: 'Redirect URL', |
| 124 |
logout: 'Redirect URL', |
| 125 |
lostpassword: 'Redirect URL', |
| 126 |
email: 'Email', |
| 127 |
telephone: 'Telephone' |
| 128 |
}; |
| 129 |
|
| 130 |
if (type === 'text') |
| 131 |
parentTab.find('.wpie-tab__type-menu').removeClass('is-hidden'); |
| 132 |
|
| 133 |
else if (typeFieldMapping[type]) { |
| 134 |
const fieldsToShow = typeFieldMapping[type]; |
| 135 |
fieldsToShow.forEach(field => { |
| 136 |
parent.find(`[data-field-box="${field}"]`).removeClass('is-hidden'); |
| 137 |
}); |
| 138 |
|
| 139 |
if (linkTextMapping[type]) |
| 140 |
linkText.text(linkTextMapping[type]); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
function custom_icon() { |
| 145 |
const fieldset = get_parent_fields($(this), '.wpie-fieldset'); |
| 146 |
const parent_fields = get_parent_fields($(this)); |
| 147 |
const neighborhood = fieldset.find('.wpie-fields').not(parent_fields).find('input[type="checkbox"]'); |
| 148 |
const box = get_field_box($(this)); |
| 149 |
const fields = parent_fields.find('[data-field-box]').not(box); |
| 150 |
fields.addClass('is-hidden'); |
| 151 |
if ($(this).is(':checked')) { |
| 152 |
fields.removeClass('is-hidden'); |
| 153 |
$(neighborhood).attr('disabled', 'disabled'); |
| 154 |
} else { |
| 155 |
$(neighborhood).removeAttr('disabled'); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// Enable Event Tracking |
| 160 |
function enable_tracking() { |
| 161 |
const fieldset = get_parent_fields($(this), '.wpie-fieldset'); |
| 162 |
const tracking_field = fieldset.find('.wpie-event-tracking'); |
| 163 |
tracking_field.addClass('is-hidden'); |
| 164 |
if ($(this).is(':checked')) { |
| 165 |
tracking_field.removeClass('is-hidden'); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
function item_remove() { |
| 170 |
const userConfirmed = confirm("Are you sure you want to remove this element?"); |
| 171 |
if (userConfirmed) { |
| 172 |
const parent = $(this).closest('.wpie-item'); |
| 173 |
$(parent).remove(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
function item_toggle() { |
| 178 |
const parent = get_parent_fields($(this), '.wpie-item'); |
| 179 |
const val = $(parent).attr('open') ? '0' : '1'; |
| 180 |
$(parent).find('.wpie-item__toggle').val(val); |
| 181 |
} |
| 182 |
|
| 183 |
function delete_menu(e) { |
| 184 |
const proceed = confirm("Are you sure want to Delete Menu?"); |
| 185 |
if(!proceed) { |
| 186 |
e.preventDefault(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
function get_parent_fields($el, $class = '.wpie-fields') { |
| 191 |
return $el.closest($class); |
| 192 |
} |
| 193 |
|
| 194 |
function get_field_box($el, $class = '.wpie-field') { |
| 195 |
return $el.closest($class); |
| 196 |
} |
| 197 |
|
| 198 |
initialize(); |
| 199 |
}); |