From 425a9d56b73ecd279aca5257c9ecc53830b44b80 Mon Sep 17 00:00:00 2001 From: Zach Tirrell Date: Wed, 2 Apr 2014 08:00:57 -0400 Subject: [PATCH] Add efficiency to resize These elements do not need to be done on each iteration of the loop over all the area tags, better to do them outside the each - calculating the rescale ratio - adjusting the image size - triggering the change handler on the target --- lib/jquery.timezone-picker.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/jquery.timezone-picker.js b/lib/jquery.timezone-picker.js index 4fe6301..8b29c94 100644 --- a/lib/jquery.timezone-picker.js +++ b/lib/jquery.timezone-picker.js @@ -19,7 +19,7 @@ methods.init = function(initOpts) { // Set the instance options. opts = $.extend({}, $.fn.timezonePicker.defaults, initOpts); selectedTimzone = opts.timezone; - + changeHandler = opts.changeHandler; return $origCall.each(function(index, item) { @@ -56,7 +56,7 @@ methods.init = function(initOpts) { top: (pinCoords[1] - pinHeight) + 'px' }); } - + var timezoneName = $(areaElement).attr('data-timezone'); var countryName = $(areaElement).attr('data-country'); var offset = $(areaElement).attr('data-offset'); @@ -65,7 +65,7 @@ methods.init = function(initOpts) { if (typeof changeHandler === 'function') { changeHandler(timezoneName, countryName, offset); } - + // Update the target select list. if (opts.target) { if (timezoneName) $(opts.target).val(timezoneName); @@ -244,6 +244,13 @@ methods.detectLocation = function(detectOpts) { * the current dimensions of the image. */ methods.resize = function() { + var rescale = imgElement.width/imgElement.getAttribute('width'); + + // Adjust the image size. + $(imgElement).parent().css({ + width: $(imgElement).width() + 'px' + }); + $(mapElement).find('area').each(function(m, areaElement) { // Save the original coordinates for further resizing. if (!areaElement.originalCoords) { @@ -254,12 +261,6 @@ methods.resize = function() { pin: areaElement.getAttribute('data-pin') }; } - var rescale = imgElement.width/imgElement.getAttribute('width'); - - // Adjust the image size. - $(imgElement).parent().css({ - width: $(imgElement).width() + 'px' - }); // Adjust the coords attribute. var originalCoords = areaElement.originalCoords.coords.split(','); @@ -274,14 +275,13 @@ methods.resize = function() { pinCoords[0] = Math.round(parseInt(pinCoords[0]) * rescale); pinCoords[1] = Math.round(parseInt(pinCoords[1]) * rescale); areaElement.setAttribute('data-pin', pinCoords.join(',')); - - // Fire the change handler on the target. - if (opts.target) { - $(opts.target).triggerHandler('change'); - } - }); + // Fire the change handler on the target. + if (opts.target) { + $(opts.target).triggerHandler('change'); + } + return this; }; @@ -295,7 +295,7 @@ $.fn.timezonePicker = function(method) { } else { $.error('Method ' + method + ' does not exist on jQuery.timezonePicker'); - } + } }; $.fn.timezonePicker.defaults = {