| 1 |
/* |
| 2 |
* Google Maps Widget |
| 3 |
* (c) Web factory Ltd, 2012 - 2015 |
| 4 |
*/ |
| 5 |
|
| 6 |
|
| 7 |
jQuery(function($) { |
| 8 |
// init JS for each active widget |
| 9 |
$(".widget-liquid-right [id*='" + gmw.id_base + "'].widget, .inactive-sidebar [id*='" + gmw.id_base + "'].widget").each(function (i, widget) { |
| 10 |
gmw_init_widget_ui(widget); |
| 11 |
}); // foreach GMW active widget |
| 12 |
|
| 13 |
// re-init JS on widget update and add |
| 14 |
$(document).on('widget-updated', function(event, widget) { |
| 15 |
gmw_init_widget_ui(widget); |
| 16 |
}); |
| 17 |
$(document).on('widget-added', function(event, widget) { |
| 18 |
gmw_init_widget_ui(widget); |
| 19 |
}); |
| 20 |
|
| 21 |
|
| 22 |
// init JS UI for an individual GMW |
| 23 |
function gmw_init_widget_ui(widget) { |
| 24 |
gmw_change_pin_type(widget); |
| 25 |
gmw_change_link_type(widget); |
| 26 |
|
| 27 |
// handle dropdown fields that have dependant fields |
| 28 |
$('.gmw_thumb_pin_type', widget).on('change', function(e) { |
| 29 |
gmw_change_pin_type(widget); |
| 30 |
}); |
| 31 |
$('.gmw_thumb_link_type', widget).on('change', function(e) { |
| 32 |
gmw_change_link_type(widget); |
| 33 |
}); |
| 34 |
$('.gmw_thumb_color_scheme', widget).on('change', function(e) { |
| 35 |
gmw_promo_option_change(widget, '.gmw_thumb_color_scheme'); |
| 36 |
}); |
| 37 |
$('.gmw_lightbox_skin', widget).on('change', function(e) { |
| 38 |
gmw_promo_option_change(widget, '.gmw_lightbox_skin'); |
| 39 |
}); |
| 40 |
|
| 41 |
// open promo/activation dialog |
| 42 |
$('.open_promo_dialog', widget).on('click', function(e) { |
| 43 |
e.preventDefault(); |
| 44 |
gmw_open_promo_dialog(this); |
| 45 |
|
| 46 |
return false; |
| 47 |
}); |
| 48 |
|
| 49 |
// init tabs |
| 50 |
$('.gmw-tabs', widget).tabs({ active: gmw_get_active_tab($('.gmw-tabs', widget).attr('id')), |
| 51 |
activate: function(event, ui) { gmw_save_active_tab(this); } |
| 52 |
}); |
| 53 |
} // gmw_init_widget_ui |
| 54 |
|
| 55 |
|
| 56 |
// get active tab index from cookie |
| 57 |
function gmw_get_active_tab(el_id) { |
| 58 |
id = parseInt(0 + $.cookie(el_id), 10); |
| 59 |
if (isNaN(id) === true) { |
| 60 |
id = 0; |
| 61 |
} |
| 62 |
|
| 63 |
return id; |
| 64 |
} // get_active_tab |
| 65 |
|
| 66 |
|
| 67 |
// save active tab index to cookie |
| 68 |
function gmw_save_active_tab(elem) { |
| 69 |
$.cookie($(elem).attr('id'), $(elem).tabs('option', 'active'), { expires: 30 }); |
| 70 |
} // save_active_tab |
| 71 |
|
| 72 |
|
| 73 |
// show/hide custom link field based on user's link type choice |
| 74 |
function gmw_change_link_type(widget) { |
| 75 |
if ($('.gmw_thumb_link_type', widget).val() == 'custom') { |
| 76 |
$('.gmw_thumb_link_section', widget).show(); |
| 77 |
} else { |
| 78 |
$('.gmw_thumb_link_section', widget).hide(); |
| 79 |
} |
| 80 |
} // link_type |
| 81 |
|
| 82 |
|
| 83 |
// show/hide custom pin URL field based on user's pin type choice |
| 84 |
function gmw_change_pin_type(widget) { |
| 85 |
if ($('.gmw_thumb_pin_type', widget).val() == 'custom') { |
| 86 |
$('.gmw_thumb_pin_type_custom_section', widget).show(); |
| 87 |
$('.gmw_thumb_pin_type_predefined_section', widget).hide(); |
| 88 |
} else { |
| 89 |
$('.gmw_thumb_pin_type_custom_section', widget).hide(); |
| 90 |
$('.gmw_thumb_pin_type_predefined_section', widget).show(); |
| 91 |
} |
| 92 |
} // pin_type |
| 93 |
|
| 94 |
|
| 95 |
// extra features related functions |
| 96 |
// open promo dialog on load |
| 97 |
if (window.location.search.search('gmw_open_promo_dialog') != -1) { |
| 98 |
gmw_open_promo_dialog(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
// opens promo dialog when special value is selected in widget's options |
| 103 |
function gmw_promo_option_change(widget, el) { |
| 104 |
if (($(el, widget).val()) == '-1') { |
| 105 |
$(el, widget).find('option').attr('selected', ''); |
| 106 |
$(el, widget).find('option:first').attr('selected', 'selected'); |
| 107 |
gmw_open_promo_dialog(widget); |
| 108 |
} |
| 109 |
} // promo_option_change |
| 110 |
|
| 111 |
|
| 112 |
// already subscribed button in dialog |
| 113 |
$('#gmw_already_subscribed').on('click', function(e) { |
| 114 |
e.preventDefault(); |
| 115 |
$('#gmw_dialog_subscribe').hide(); |
| 116 |
$('#gmw_dialog_activate').show(); |
| 117 |
|
| 118 |
return false; |
| 119 |
}); // already subscribed click |
| 120 |
|
| 121 |
|
| 122 |
// subscribe button in dialog |
| 123 |
$('#gmw_subscribe').on('click', function(e) { |
| 124 |
e.preventDefault(); |
| 125 |
|
| 126 |
err = false; |
| 127 |
$('#gmw_promo_dialog input.error').removeClass('error'); |
| 128 |
$('#gmw_promo_dialog span.error').hide(); |
| 129 |
|
| 130 |
if ($('#gmw_name').val().length < 3) { |
| 131 |
$('#gmw_name').addClass('error'); |
| 132 |
$('#gmw_promo_dialog span.error.name').show(); |
| 133 |
$('#gmw_name').focus().select(); |
| 134 |
|
| 135 |
err = true; |
| 136 |
} // check name |
| 137 |
|
| 138 |
re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; |
| 139 |
if (!re.test($('#gmw_email').val())) { |
| 140 |
$('#gmw_email').addClass('error'); |
| 141 |
$('#gmw_promo_dialog span.error.email').show(); |
| 142 |
$('#gmw_email').focus().select(); |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
if (err) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
$.post(ajaxurl, { action: 'gmw_subscribe', 'name': $('#gmw_name').val(), 'email': $('#gmw_email').val()}, function(data) { |
| 151 |
if (data && data.success == true) { |
| 152 |
$('#gmw_dialog_subscribe').hide(); |
| 153 |
$('#gmw_dialog_activate').show(); |
| 154 |
alert(gmw.subscribe_ok); |
| 155 |
} else if (data && data.success == false && data.data == 'duplicate') { |
| 156 |
$('#gmw_dialog_subscribe').hide(); |
| 157 |
$('#gmw_dialog_activate').show(); |
| 158 |
alert(gmw.subscribe_duplicate); |
| 159 |
} else { |
| 160 |
alert(gmw.subscribe_error); |
| 161 |
} |
| 162 |
}, 'json').fail(function() { |
| 163 |
alert(gmw.undocumented_error); |
| 164 |
}); |
| 165 |
|
| 166 |
return false; |
| 167 |
}); // subscribe click |
| 168 |
|
| 169 |
|
| 170 |
// check code and activate button in dialog |
| 171 |
$('#gmw_activate').on('click', function(e) { |
| 172 |
e.preventDefault(); |
| 173 |
|
| 174 |
$('#gmw_promo_dialog input.error').removeClass('error'); |
| 175 |
$('#gmw_promo_dialog span.error').hide(); |
| 176 |
|
| 177 |
$.post(ajaxurl, { action: 'gmw_activate', 'code': $('#gmw_code').val()}, function(data) { |
| 178 |
if (data && data.success == true) { |
| 179 |
alert(gmw.activate_ok); |
| 180 |
if ($('#gmw_promo_dialog').data('widget-id')) { |
| 181 |
$('#' + $('#gmw_promo_dialog').data('widget-id') + ' .widget-control-save').trigger('click'); |
| 182 |
$('#gmw_activate_notice').hide(); |
| 183 |
$('#gmw_promo_dialog').dialog('close'); |
| 184 |
} else { |
| 185 |
window.location = 'widgets.php'; |
| 186 |
} |
| 187 |
} else { |
| 188 |
$('#gmw_promo_dialog span.error.gmw_code').show(); |
| 189 |
$('#gmw_code').focus().select(); |
| 190 |
} |
| 191 |
}, 'json').fail(function() { |
| 192 |
alert(gmw.undocumented_error); |
| 193 |
}); |
| 194 |
|
| 195 |
return false; |
| 196 |
}); // activate button click |
| 197 |
|
| 198 |
|
| 199 |
// open promo/activation dialog |
| 200 |
function gmw_open_promo_dialog(widget) { |
| 201 |
$('#gmw_dialog_subscribe').show(); |
| 202 |
$('#gmw_dialog_activate').hide(); |
| 203 |
|
| 204 |
$('#gmw_promo_dialog').dialog({ |
| 205 |
'dialogClass' : 'wp-dialog gmw-dialog', |
| 206 |
'modal' : true, |
| 207 |
'width': 650, |
| 208 |
'title': gmw.dialog_title, |
| 209 |
'autoOpen': false, |
| 210 |
'closeOnEscape': true, |
| 211 |
close: function(event, ui) { $('#gmw_promo_dialog').data('widget-id', ''); } |
| 212 |
}).dialog('open'); |
| 213 |
|
| 214 |
if (widget) { |
| 215 |
$('#gmw_promo_dialog').data('widget-id', $(widget).parents('div.widget').attr('id')); |
| 216 |
} |
| 217 |
} // open_promo_dialog |
| 218 |
}); // onload |