| 1 |
/* |
| 2 |
* Google Maps Widget |
| 3 |
* (c) Web factory Ltd, 2012 - 2015 |
| 4 |
*/ |
| 5 |
|
| 6 |
jQuery(function($) { |
| 7 |
$('.gmw-tabs').each(function(i, el) { |
| 8 |
change_pin_type(el); |
| 9 |
change_link_type(el); |
| 10 |
$('.gmw_thumb_pin_type', el).on('change', function() { change_pin_type(el) }); |
| 11 |
$('.gmw_thumb_link_type', el).on('change', function() { change_link_type(el) }); |
| 12 |
$('.gmw_thumb_color_scheme', el).on('change', function() { gmw_promo_option_change(el, '.gmw_thumb_color_scheme') }); |
| 13 |
$('.gmw_lightbox_skin', el).on('change', function() { gmw_promo_option_change(el, '.gmw_lightbox_skin') }); |
| 14 |
|
| 15 |
el_id = $(el).attr('id'); |
| 16 |
$(el).tabs({ active: get_active_tab(el_id), |
| 17 |
activate: function(event, ui) { save_active_tab(this); } |
| 18 |
}); |
| 19 |
}); |
| 20 |
|
| 21 |
// get active tab index from cookie |
| 22 |
function get_active_tab(el_id) { |
| 23 |
id = parseInt(0 + $.cookie(el_id), 10); |
| 24 |
|
| 25 |
return id; |
| 26 |
} // get_active_tab |
| 27 |
|
| 28 |
// save active tab index to cookie |
| 29 |
function save_active_tab(elem) { |
| 30 |
$.cookie($(elem).attr('id'), $(elem).tabs('option', 'active'), { expires: 30 }); |
| 31 |
} // save_active_tab |
| 32 |
|
| 33 |
// show/hide custom link field based on user's link type choice |
| 34 |
function change_link_type(widget) { |
| 35 |
if ($('.gmw_thumb_link_type', widget).val() == 'custom') { |
| 36 |
$('.gmw_thumb_link_section', widget).show(); |
| 37 |
} else { |
| 38 |
$('.gmw_thumb_link_section', widget).hide(); |
| 39 |
} |
| 40 |
} // link_type |
| 41 |
|
| 42 |
// show/hide custom pin URL field based on user's pin type choice |
| 43 |
function change_pin_type(widget) { |
| 44 |
if ($('.gmw_thumb_pin_type', widget).val() == 'custom') { |
| 45 |
$('.gmw_thumb_pin_type_custom_section', widget).show(); |
| 46 |
$('.gmw_thumb_pin_type_predefined_section', widget).hide(); |
| 47 |
} else { |
| 48 |
$('.gmw_thumb_pin_type_custom_section', widget).hide(); |
| 49 |
$('.gmw_thumb_pin_type_predefined_section', widget).show(); |
| 50 |
} |
| 51 |
} // pin_type |
| 52 |
|
| 53 |
// opens promo dialog when special value is selected in widget's options |
| 54 |
function gmw_promo_option_change(widget, el) { |
| 55 |
if (($(el, widget).val()) == '-1') { |
| 56 |
$(el, widget).find('option').attr('selected', ''); |
| 57 |
$(el, widget).find('option:first').attr('selected', 'selected'); |
| 58 |
gmw_open_promo_dialog(widget); |
| 59 |
} |
| 60 |
} // promo_option_change |
| 61 |
|
| 62 |
// open promo/activation dialog |
| 63 |
$('.open_promo_dialog').on('click', function(e) { |
| 64 |
gmw_open_promo_dialog(this); |
| 65 |
|
| 66 |
e.preventDefault(); |
| 67 |
return false; |
| 68 |
}); |
| 69 |
|
| 70 |
// button in dialog |
| 71 |
$('#gmw_already_subscribed').on('click', function(e) { |
| 72 |
$('#gmw_dialog_subscribe').hide(); |
| 73 |
$('#gmw_dialog_activate').show(); |
| 74 |
|
| 75 |
e.preventDefault(); |
| 76 |
return false; |
| 77 |
}); |
| 78 |
|
| 79 |
// button in dialog |
| 80 |
$('#gmw_subscribe').on('click', function(e) { |
| 81 |
e.preventDefault(); |
| 82 |
|
| 83 |
$err = 0; |
| 84 |
$('#gmw_promo_dialog input.error').removeClass('error'); |
| 85 |
$('#gmw_promo_dialog span.error').hide(); |
| 86 |
|
| 87 |
if ($('#gmw_name').val().length < 3) { |
| 88 |
$('#gmw_name').addClass('error'); |
| 89 |
$('#gmw_promo_dialog span.error.name').show(); |
| 90 |
$('#gmw_name').focus().select(); |
| 91 |
|
| 92 |
$err = 1; |
| 93 |
} |
| 94 |
|
| 95 |
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,}))$/; |
| 96 |
if (!re.test($('#gmw_email').val())) { |
| 97 |
$('#gmw_email').addClass('error'); |
| 98 |
$('#gmw_promo_dialog span.error.email').show(); |
| 99 |
$('#gmw_email').focus().select(); |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
if ($err) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
$.post(ajaxurl, { action: 'gmw_subscribe', 'name': $('#gmw_name').val(), 'email': $('#gmw_email').val()}, function(data) { |
| 108 |
if (data == 'ok') { |
| 109 |
$('#gmw_dialog_subscribe').hide(); |
| 110 |
$('#gmw_dialog_activate').show(); |
| 111 |
alert(gmw.subscribe_ok); |
| 112 |
} else if (data == 'duplicate') { |
| 113 |
$('#gmw_dialog_subscribe').hide(); |
| 114 |
$('#gmw_dialog_activate').show(); |
| 115 |
alert(gmw.subscribe_duplicate); |
| 116 |
} else { |
| 117 |
alert(gmw.subscribe_error); |
| 118 |
} |
| 119 |
}); |
| 120 |
|
| 121 |
return false; |
| 122 |
}); |
| 123 |
|
| 124 |
// button in dialog |
| 125 |
// check code and activate |
| 126 |
$('#gmw_activate').on('click', function(e) { |
| 127 |
e.preventDefault(); |
| 128 |
|
| 129 |
$('#gmw_promo_dialog input.error').removeClass('error'); |
| 130 |
$('#gmw_promo_dialog span.error').hide(); |
| 131 |
|
| 132 |
$.get(ajaxurl, { action: 'gmw_activate', 'code': $('#gmw_code').val()}, function(data) { |
| 133 |
if (data == '1') { |
| 134 |
alert(gmw.activate_ok); |
| 135 |
if ($('#gmw_promo_dialog').data('widget-id')) { |
| 136 |
$('#' + $('#gmw_promo_dialog').data('widget-id') + ' .widget-control-save').trigger('click'); |
| 137 |
} |
| 138 |
$('#gmw_promo_dialog').dialog('close'); |
| 139 |
} else { |
| 140 |
$('#gmw_promo_dialog span.error.gmw_code').show(); |
| 141 |
$('#gmw_code').focus().select(); |
| 142 |
} |
| 143 |
}); |
| 144 |
|
| 145 |
return false; |
| 146 |
}); |
| 147 |
|
| 148 |
// open promo/activation dialog |
| 149 |
function gmw_open_promo_dialog(widget) { |
| 150 |
$('#gmw_dialog_subscribe').show(); |
| 151 |
$('#gmw_dialog_activate').hide(); |
| 152 |
|
| 153 |
$('#gmw_promo_dialog').dialog({ |
| 154 |
'dialogClass' : 'wp-dialog gmw-dialog', |
| 155 |
'modal' : true, |
| 156 |
'width': 650, |
| 157 |
'title': gmw.dialog_title, |
| 158 |
'autoOpen': false, |
| 159 |
'closeOnEscape': true, |
| 160 |
close: function(event, ui) { $('#gmw_promo_dialog').data('widget-id', '') } |
| 161 |
}).dialog('open'); |
| 162 |
|
| 163 |
if (widget) { |
| 164 |
$('#gmw_promo_dialog').data('widget-id', $(widget).parents('div.widget').attr('id')); |
| 165 |
} |
| 166 |
} // open_promo_dialog |
| 167 |
|
| 168 |
// re-tab on GUI rebuild |
| 169 |
$('div[id*="googlemapswidget"]').ajaxSuccess(function(event, request, option) { |
| 170 |
$('.gmw-tabs').each(function(i, el) { |
| 171 |
change_pin_type(el); |
| 172 |
change_link_type(el); |
| 173 |
$('.gmw_thumb_pin_type', el).on('change', function() { change_pin_type(el) }); |
| 174 |
$('.gmw_thumb_link_type', el).on('change', function() { change_link_type(el) }); |
| 175 |
$('.gmw_thumb_color_scheme', el).on('change', function() { gmw_promo_option_change(el, '.gmw_thumb_color_scheme'); }); |
| 176 |
$('.gmw_lightbox_skin', el).on('change', function() { gmw_promo_option_change(el, '.gmw_lightbox_skin') }); |
| 177 |
|
| 178 |
el_id = $(el).attr('id'); |
| 179 |
$(el).tabs({ active: get_active_tab(el_id), |
| 180 |
activate: function(event, ui) { save_active_tab(this); } |
| 181 |
}); |
| 182 |
}); |
| 183 |
|
| 184 |
// todo fix multiple actions on single selector |
| 185 |
$('.open_promo_dialog').on('click', function(e) { |
| 186 |
gmw_open_promo_dialog(this); |
| 187 |
|
| 188 |
e.preventDefault(); |
| 189 |
return false; |
| 190 |
}); |
| 191 |
}); |
| 192 |
}); // onload |