diff --git a/uc_option_image.js b/uc_option_image.js index a7a6bdb..3eab747 100644 --- a/uc_option_image.js +++ b/uc_option_image.js @@ -16,19 +16,20 @@ UCOI.init = function(context) { this.noimage = Drupal.settings.UCOI.noimage; this.attributes = Drupal.settings.UCOI.attributes; this.defaultSize = Drupal.settings.UCOI.default_size; - this.nodeid = Drupal.settings.UCOI.nodeid; // Selects $('.attributes select.form-select', context).change(function(){ + nid = $('input[name=ucoi_nid]', this.form).val(); if (aid = UCOI.getAID(this)){ - UCOI.switchImage(aid, this, size); + UCOI.switchImage(nid, aid, this, size); } }); // Radios $('.attributes .form-radios input', context).click(function(){ + nid = $('input[name=ucoi_nid]', this.form).val(); if (aid = UCOI.getAID(this)){ - UCOI.switchImage(aid, this, size); + UCOI.switchImage(nid, aid, this, size); } }); }; @@ -36,10 +37,9 @@ UCOI.init = function(context) { /** * Switch an option image. */ -UCOI.switchImage = function(aid, input, size) { - var nid = this.nodeid; +UCOI.switchImage = function(nid, aid, input, size) { var oid = $(input).val(); - var image = $(':not(.uc-option-image-preloaded) > div.uc-option-image-block').children('img.uc-option-image'); + var image = $(':not(.uc-option-image-preloaded) > div#uc-option-image-block-'+nid).children('img.uc-option-image'); // Make sure we have permission to switch this attribute if (this.attributes[aid] == 0){ @@ -47,7 +47,7 @@ UCOI.switchImage = function(aid, input, size) { } try { - var images = this.images[nid][aid]; + var images = this.images['node-'+nid][aid]; if ((oid=="" || images[oid].derivative=="") && image) { parentImage = image[0].parentNode; @@ -55,7 +55,7 @@ UCOI.switchImage = function(aid, input, size) { } else if (image[0] && images[oid].derivative) { this.switchImageEffect(image, images[oid]); } else if (image[0] == null) { - parentImage = $(':not(.uc-option-image-preloaded) > div.uc-option-image-block'); + parentImage = $(':not(.uc-option-image-preloaded) > div#uc-option-image-block-'+nid); parentImage[0].innerHTML = ''; } } @@ -71,7 +71,7 @@ UCOI.switchImageEffect = function(image, imageproperty) { switch(this.effect){ case 'fade': - $(image).fadeOut(200, function(){ + $(image).fadeOut(200, function(){ $(this).attr('src', imageproperty.derivative).fadeIn(200); }); break; diff --git a/uc_option_image.module b/uc_option_image.module index 6f31e82..3a083b2 100644 --- a/uc_option_image.module +++ b/uc_option_image.module @@ -73,9 +73,7 @@ function uc_option_image_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { $size = $a4 ? $page_size : $teaser_size; // Pass attributes to uc_option_image to populate JS settings - if ($a4) { - uc_option_image($node, $attributes, $size); - } + uc_option_image($node, $attributes, $size); // Determine if we have a default option using // the first attribute's default option @@ -89,7 +87,7 @@ function uc_option_image_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { // Display the image based on teaser/page view // Ensure that original file exists if ($file->filepath && file_exists($file->filepath)) { - $image = theme('uc_option_image', $first_attribute->aid, $file, $size); + $image = theme('uc_option_image', $first_attribute->aid, $file, $size, $node); } else { $image = theme('uc_option_image_no_image', $node, $size); @@ -116,6 +114,14 @@ function uc_option_image_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { * Implements hook_form_alter(). */ function uc_option_image_form_alter(&$form, &$form_state, $form_id) { + if (strstr($form_id, 'uc_product_add_to_cart_form')) { + // Add the products nid to the form, to avoid parsing formid's for an NID that may not exist. + $form['ucoi_nid'] = array( + '#type' => 'hidden', + '#value' => $form['nid']['#value'], + ); + } + switch ($form_id) { // Attribute options form case 'uc_object_options_form': @@ -143,7 +149,7 @@ function uc_option_image_form_alter(&$form, &$form_state, $form_id) { if (!empty($file)) { $form['attributes'][$aid]['options'][$oid]['option_image_preview'] = array( '#type' => 'markup', - '#value' => filter_xss(theme('uc_option_image', $aid, $file, variable_get('uc_option_image_preview_size', '_original'))), + '#value' => filter_xss(theme('uc_option_image', $aid, $file, variable_get('uc_option_image_preview_size', '_original'), $node)), ); $form['attributes'][$aid]['options'][$oid][uc_option_image_id($nid, $aid, $oid)] = array( '#type' => 'file', @@ -334,7 +340,7 @@ function uc_option_image_form_uc_cart_view_form_alter(&$form, &$form_state) { * @todo: abstract out the drupal_add_js so the object can be returned via js http request */ function uc_option_image($node, $attributes, $size = '_original') { - static $prep, $data; + static $prep, $settings; // Check if this feature is enabled if (!variable_get('uc_option_image_js', TRUE)) { @@ -347,16 +353,19 @@ function uc_option_image($node, $attributes, $size = '_original') { } // Static prep + $data = array(); if (!$prep) { drupal_add_js(drupal_get_path('module', 'uc_option_image') . '/uc_option_image.js'); - $data = array(); $data['size'] = $size; $data['effect'] = variable_get('uc_option_image_effect', 'fade'); $data['noimage'] = theme('uc_option_image_no_image_path', $node, $size); $data['attributes'] = variable_get('uc_option_image_attributes', ''); - $data['nodeid'] = $node->nid; - + // Add the settings, then clear out these values from $data settings so + // they don't get merged many times + drupal_add_js(array('UCOI' => $data), 'setting'); + $data = array(); + $prep = TRUE; } @@ -373,7 +382,9 @@ function uc_option_image($node, $attributes, $size = '_original') { $file = uc_option_image_load($node->nid, $aid, $oid); if ($file->filepath) { - $data['images'][$node->nid][$aid][$oid] = array( + // Use strings as the keys to avoid the array_merge_recursive problem. + //$data['images']["node-" . $node->nid]["aid-" . $aid]["oid-" . $oid] = array( + $data['images']["node-" . $node->nid][$aid][$oid] = array( 'nid' => $node->nid, 'aid' => $aid, 'oid' => $oid, @@ -546,6 +557,7 @@ function uc_option_image_theme() { 'arguments' => array( 'file' => NULL, 'size' => NULL, + 'node' => NULL, ), ), 'uc_option_image_preloaded' => array( @@ -583,7 +595,7 @@ function uc_option_image_theme() { * @return string * Markup. */ -function theme_uc_option_image($aid, $file, $size = '_original') { +function theme_uc_option_image($aid, $file, $size = '_original', $node) { $shownoimage = TRUE; if (!variable_get('uc_option_image_show_noimage', FALSE) && $file->filename == 'option_image_0_0_0') { @@ -606,7 +618,11 @@ function theme_uc_option_image($aid, $file, $size = '_original') { } } - $option_image = '
' . $imagecache_image . '
'; + $option_image = '
nid . '"'; + } + $option_image .= '>' . $imagecache_image . '
'; return $option_image; } @@ -627,7 +643,7 @@ function theme_uc_option_image_preloaded($aid, $node, $size = '_original') { $output = '