diff --git a/photoswipe.js b/photoswipe.js index 2fec46b..e24fd97 100644 --- a/photoswipe.js +++ b/photoswipe.js @@ -1,41 +1,8 @@ -Array.prototype.getUnique = function(){ - var u = {}, a = []; - for (var i = 0, l = this.length; i < l; ++i) { - if (this[i] in u) - continue; - a.push(this[i]); - u[this[i]] = 1; - } - return a; -} -var myArr = ['Kevin', 'Kevin', 'Shamasis', 'Kevin'].getUnique(); // must be called to prevent error! -// console.log(myArr); - - -/** - * Load PhotoSwipe once page is ready - */ -(function(window, PhotoSwipe){ - document.addEventListener('DOMContentLoaded', function(){ - // Set options - var options = photoswipeConfig; - - // Find all existing galleries - var galleries = null; - jQuery("div[class*=photoswipe-gallery-]").each(function(index) { - galleries += jQuery(this).attr('class') + ' '; - }); - - // Identify and instanciate all photoswipe galleries - var regexp = /photoswipe\-gallery\-(\S*)/g; // regexp to find all the existing galleries identifiers (starting with photoswipe-gallery-) - if (galleries != null) { // else no div found - galleries = galleries.match(regexp); - galleries = galleries.sort().getUnique(); - galleries.forEach(function(item) { - PhotoSwipe.attach( window.document.querySelectorAll('.' + item + ' a'), options ); - }); +(function ($) { + Drupal.behaviors.photoswipe = { + attach: function (context, settings) { + var myPhotoSwipe = Code.PhotoSwipe.attach( window.document.querySelectorAll('div.photoswipe-image-container a'), settings.photoswipe); } + }; - }, false); - -}(window, window.Code.PhotoSwipe)); +}(jQuery)); diff --git a/photoswipe.module b/photoswipe.module index 1c9395e..ab0e6cd 100644 --- a/photoswipe.module +++ b/photoswipe.module @@ -1,13 +1,8 @@ 'setting', 'scope' => JS_DEFAULT)); drupal_add_js('var photoswipeConfig = {' . variable_get('photoswipe_custom_config', $defaultConfigString) . '};', array('type' => 'inline', 'scope' => JS_DEFAULT)); + */ } } @@ -87,21 +84,21 @@ function photoswipe_active_url() { */ function photoswipe_active_nodetype() { $node = menu_get_object(); - $photoswipe_types = variable_get('photoswipe_nodetypes', array()); - - if (count($photoswipe_types) > 0) { - if (is_integer(max($photoswipe_types))) { // check whether any node type has been selected - return TRUE; // return true if no type has been selected + if ($node) { + $photoswipe_types = variable_get('photoswipe_nodetypes', array()); + + if (count($photoswipe_types) > 0) { + if (is_integer(max($photoswipe_types))) { // check whether any node type has been selected + return TRUE; // return true if no type has been selected + } + else { + return in_array($node->type, $photoswipe_types, $strict = TRUE); // strict is needed to compare with the values and not the keys + } } else { - return in_array($node->type, $photoswipe_types, $strict = TRUE); // strict is needed to compare with the values and not the keys + return TRUE; // return true if no type has been selected } } - else { - return TRUE; // return true if no type has been selected - } - - } /** @@ -111,7 +108,7 @@ function photoswipe_permission() { return array( 'administer photoswipe' => array( 'title' => t('administer photoswipe'), - 'description' => t('TODO Add a description for \'administer photoswipe\''), + 'description' => t('Administer photoswipe module settings.'), ), ); } @@ -195,3 +192,117 @@ function photoswipe_admin_settings($form, &$form_state) { return system_settings_form($form); } + +/** + * Implementation of hook_field_formatter_info(). + * + * Add certain photoswipe+imagecache formatters to CCK image fields if the + * imagefield.module and the imagecache.module exist. + */ +function photoswipe_field_formatter_info() { + return array( + 'photoswipe_formatter' => array( + 'label' => t('Photoswipe'), + 'field types' => array('image'), + 'settings' => array( + 'thumb_style' => 'thumbnail', + 'slide_style' => 'large', + 'custom_class' => '', + ), + ), + ); +} + +/** + * Implements hook_field_formatter_settings_form(). + */ +function photoswipe_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { + //This gets the view_mode where our settings are stored + $display = $instance['display'][$view_mode]; + //This gets the actual settings + $settings = $display['settings']; + //Initialize the element variable + $element = array(); + + // Thumbnail size + $styles = image_styles(); + foreach ($styles as $k => $s) { $styles[$k] = $k; } + $element['thumb_style'] = array( + '#type' => 'select', + '#title' => t('Thumbnail Image Style'), + '#description' => t('Select what image style to use for the image thumbnail'), + '#default_value' => $settings['thumb_style'], + '#options' => $styles, + ); + + // Slide style + $element['slide_style'] = array( + '#type' => 'select', + '#title' => t('Slide Image Style'), + '#description' => t('Select what image style to use for the image slide'), + '#default_value' => $settings['slide_style'], + '#options' => $styles, + ); + + // CSS Class + $element['custom_class'] = array( + '#type' => 'textfield', + '#title' => t('Custom Class'), + '#description' => t('Add a custom css class to the image wrapper'), + '#default_value' => $settings['custom_class'], + ); + return $element; +} + +/** + * Implements hook_field_formatter_settings_summary(). + */ +function photoswipe_field_formatter_settings_summary($field, $instance, $view_mode) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + $summary = t('Use a @thumb_style thumbnail and link to a @slide_style slide. with the tooltip of "@tooltip."', array( + '@thumb_style' => $settings['thumb_style'], + '@slide_style' => $settings['slide_style'], + )); + if (strlen($settings['custom_class'] > 0)) { + $summary .= t(' The image wrapper has a custom css class of "@class."', array( + '@class' => $settings['custom_class'], + )); + } + return $summary; +} + +function photoswipe_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $elements = array(); + + // settings + $settings = $display['settings']; + $thumb = $settings['thumb_style']; + $slide = $settings['slide_style']; + $class = $settings['custom_class']; + + // this could be expanded on - for now we'll leave it simple + $options = array('html' => true); + + // build render + foreach ($items as $delta => $item) { + $thumb_path = image_style_url($thumb, $item['uri']); + $slide_path = image_style_url($slide, $item['uri']); + $image = theme('image', array('path' => $thumb_path, 'alt' => $item['alt'], 'title' => $item['title'])); + $link = l($image, $slide_path, $options); + $elements[$delta] = array( + '#markup' => $link, + ); + $elements['#prefix'] = "
"; + $elements['#suffix'] = "
"; + } + + $ps_options = array( + 'enableMouseWheel' => false, + 'enableKeyboard' => false, + ); + + drupal_add_js(array('photoswipe' => $ps_options), 'setting'); + + return $elements; +}