cms/drupal/sites/all/modules/masonry/masonry.module
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 /**
       
     3  * @file
       
     4  * Provides an API for integrating the jQuery Masonry plugin with Drupal.
       
     5  */
       
     6 
       
     7 /**
       
     8  * Implements hook_libraries_info().
       
     9  */
       
    10 function masonry_libraries_info() {
       
    11   $libraries['masonry'] = array(
       
    12     'name' => 'Masonry',
       
    13     'vendor url' => 'http://masonry.desandro.com/',
       
    14     'download url' => 'http://masonry.desandro.com/',
       
    15     'version arguments' => array(
       
    16       'file' => 'masonry.pkgd.min.js',
       
    17       // Masonry 3.x
       
    18       'pattern' => '/Masonry\s+PACKAGED\s+v?([0-9\.]+)/',
       
    19       'lines' => 2,
       
    20       'cols' => 30,
       
    21     ),
       
    22     'files' => array(
       
    23       'js' => array(
       
    24         'masonry.pkgd.min.js',
       
    25       ),
       
    26     ),
       
    27   );
       
    28   $libraries['imagesloaded'] = array(
       
    29     'name' => 'Images Loaded',
       
    30     'vendor url' => 'http://desandro.github.io/imagesloaded',
       
    31     'download url' => 'http://desandro.github.io/imagesloaded/imagesloaded.pkgd.min.js',
       
    32     'version arguments' => array(
       
    33       'file' => 'imagesloaded.pkgd.min.js',
       
    34       // imagesLoaded 3.x
       
    35       'pattern' => '/imagesLoaded\s+PACKAGED\s+v?([0-9\.]+)/',
       
    36       'lines' => 2,
       
    37       'cols' => 30,
       
    38     ),
       
    39     'files' => array(
       
    40       'js' => array(
       
    41         'imagesloaded.pkgd.min.js',
       
    42       ),
       
    43     ),
       
    44   );
       
    45 
       
    46   return $libraries;
       
    47 }
       
    48 
       
    49 /**
       
    50  * Check if the Masonry and imagesLoaded libraries are installed.
       
    51  *
       
    52  * @return
       
    53  *   A boolean indicating the installed status.
       
    54  */
       
    55 function masonry_installed() {
       
    56   $masonry = libraries_detect('masonry');
       
    57   $imagesloaded = libraries_detect('imagesloaded');
       
    58   if ((!empty($masonry['installed'])) && (!empty($imagesloaded['installed']))) {
       
    59     return TRUE;
       
    60   }
       
    61   else {
       
    62     return FALSE;
       
    63   }
       
    64 }
       
    65 
       
    66 /**
       
    67  * Check if the Masonry and imagesLoaded libraries have been loaded.
       
    68  *
       
    69  * @return
       
    70  *   A boolean indicating the loaded status.
       
    71  */
       
    72 function masonry_loaded() {
       
    73   $masonry = libraries_load('masonry');
       
    74   $imagesloaded = libraries_load('imagesloaded');
       
    75   if ((!empty($masonry['loaded'])) && (!empty($imagesloaded['loaded']))) {
       
    76     return TRUE;
       
    77   }
       
    78   else {
       
    79     return FALSE;
       
    80   }
       
    81 }
       
    82 
       
    83 /**
       
    84  * Get default Masonry options.
       
    85  *
       
    86  * @return
       
    87  *   An associative array of default options for Masonry.
       
    88  *   Contains:
       
    89  *   - masonry_column_width: The width of each column (in pixels or as a
       
    90  *     percentage).
       
    91  *   - masonry_column_width_units: The units to use for the column width ('px'
       
    92  *     or '%').
       
    93  *   - masonry_gutter_width: The spacing between each column (in pixels).
       
    94  *   - masonry_resizable: Automatically rearrange items when the container is
       
    95  *     resized.
       
    96  *   - masonry_animated: Animate item rearrangements.
       
    97  *   - masonry_animation_duration: The duration of animations (in milliseconds).
       
    98  *   - masonry_fit_width: Sets the width of the container to the nearest column.
       
    99  *     Ideal for centering Masonry layouts.
       
   100  *   - masonry_rtl: Display items from right-to-left.
       
   101  *   - masonry_images_first: Load all images first before triggering Masonry.
       
   102  */
       
   103 function masonry_default_options() {
       
   104   $options = array(
       
   105     'masonry_column_width' => '',
       
   106     'masonry_column_width_units' => 'px',
       
   107     'masonry_gutter_width' => '0',
       
   108     'masonry_resizable' => 1,
       
   109     'masonry_animated' => 0,
       
   110     'masonry_animation_duration' => '500',
       
   111     'masonry_fit_width' => 0,
       
   112     'masonry_rtl' => 0,
       
   113     'masonry_images_first' => 1,
       
   114     'masonry_stamp_selector' => ''
       
   115   );
       
   116 
       
   117   // Allow other modules to alter the default options
       
   118   drupal_alter('masonry_default_options', $options);
       
   119 
       
   120   return $options;
       
   121 }
       
   122 
       
   123 /**
       
   124  * Add Masonry options to an existing form.
       
   125  *
       
   126  * @param $form
       
   127  *   A form array to add Masonry options to.
       
   128  * @param $default_values
       
   129  *   An array of default form values.
       
   130  */
       
   131 function masonry_add_options_to_form(&$form, $default_values) {
       
   132   $form['masonry_column_width'] = array(
       
   133     '#type' => 'textfield',
       
   134     '#title' => t('Column width'),
       
   135     '#description' => t("The width of each column, enter pixels, percentage, or string of css selector"),
       
   136     '#default_value' => $default_values['masonry_column_width'],
       
   137   );
       
   138   $form['masonry_column_width_units'] = array(
       
   139     '#type' => 'radios',
       
   140     '#title' => t('Column width units'),
       
   141     '#description' => t("The units to use for the column width."),
       
   142     '#options' => array(
       
   143       'px' => t("Pixels"),
       
   144       '%' => t("Percentage (of container's width)"),
       
   145       'css' => t("CSS selector (you must configure your css to set widths for .masonry-item)"),
       
   146     ),
       
   147     '#default_value' => $default_values['masonry_column_width_units'],
       
   148   );
       
   149   $form['masonry_gutter_width'] = array(
       
   150     '#type' => 'textfield',
       
   151     '#title' => t('Gutter width'),
       
   152     '#description' => t("The spacing between each column."),
       
   153     '#default_value' => $default_values['masonry_gutter_width'],
       
   154     '#size' => 4,
       
   155     '#maxlength' => 3,
       
   156     '#field_suffix' => t('px'),
       
   157   );
       
   158   $form['masonry_stamp_selector'] = array(
       
   159     '#type' => 'textfield',
       
   160     '#title' => t('Stamp Selector'),
       
   161     '#description' => t("Specifies which elements are stamped within the layout using css selector"),
       
   162     '#default_value' => $default_values['masonry_stamp_selector']
       
   163   );
       
   164   $form['masonry_resizable'] = array(
       
   165     '#type' => 'checkbox',
       
   166     '#title' => t('Resizable'),
       
   167     '#description' => t("Automatically rearrange items when the container is resized."),
       
   168     '#default_value' => $default_values['masonry_resizable'],
       
   169   );
       
   170   $form['masonry_animated'] = array(
       
   171     '#type' => 'checkbox',
       
   172     '#title' => t('Animated'),
       
   173     '#description' => t("Animate item rearrangements."),
       
   174     '#default_value' => $default_values['masonry_animated'],
       
   175     '#states' => array(
       
   176       'visible' => array(
       
   177         'input.form-checkbox[name$="[masonry_resizable]"]' => array('checked' => TRUE),
       
   178       ),
       
   179     ),
       
   180   );
       
   181   $form['masonry_animation_duration'] = array(
       
   182     '#type' => 'textfield',
       
   183     '#title' => t('Animation duration'),
       
   184     '#description' => t("The duration of animations (1000 ms = 1 sec)."),
       
   185     '#default_value' => $default_values['masonry_animation_duration'],
       
   186     '#size' => 5,
       
   187     '#maxlength' => 4,
       
   188     '#field_suffix' => t('ms'),
       
   189     '#states' => array(
       
   190       'visible' => array(
       
   191         'input.form-checkbox[name$="[masonry_resizable]"]' => array('checked' => TRUE),
       
   192         'input.form-checkbox[name$="[masonry_animated]"]' => array('checked' => TRUE),
       
   193       ),
       
   194     ),
       
   195   );
       
   196   $form['masonry_fit_width'] = array(
       
   197     '#type' => 'checkbox',
       
   198     '#title' => t('Fit width'),
       
   199     '#description' => t("Sets the width of the container to the nearest column. Ideal for centering Masonry layouts. See the <a href='http://masonry.desandro.com/demos/centered.html'>'Centered' demo</a> for more information."),
       
   200     '#default_value' => $default_values['masonry_fit_width'],
       
   201   );
       
   202   $form['masonry_rtl'] = array(
       
   203     '#type' => 'checkbox',
       
   204     '#title' => t('RTL layout'),
       
   205     '#description' => t("Display items from right-to-left."),
       
   206     '#default_value' => $default_values['masonry_rtl'],
       
   207   );
       
   208   $form['masonry_images_first'] = array(
       
   209     '#type' => 'checkbox',
       
   210     '#title' => t('Load images first'),
       
   211     '#description' => t("Load all images first before triggering Masonry."),
       
   212     '#default_value' => $default_values['masonry_images_first'],
       
   213   );
       
   214 
       
   215   // Allow other modules to alter the form
       
   216   drupal_alter('masonry_options_form', $form, $default_values);
       
   217 }
       
   218 
       
   219 /**
       
   220  * Apply Masonry to a container.
       
   221  *
       
   222  * @param $container
       
   223  *   The CSS selector of the container element to apply Masonry to.
       
   224  * @param $options
       
   225  *   An associative array of Masonry options.
       
   226  *   Contains:
       
   227  *   - masonry_item_selector: The CSS selector of the items within the
       
   228  *     container.
       
   229  *   - masonry_column_width: The width of each column (in pixels or as a
       
   230  *     percentage).
       
   231  *   - masonry_column_width_units: The units to use for the column width ('px'
       
   232  *     or '%').
       
   233  *   - masonry_gutter_width: The spacing between each column (in pixels).
       
   234  *   - masonry_resizable: Automatically rearrange items when the container is
       
   235  *     resized.
       
   236  *   - masonry_animated: Animate item rearrangements.
       
   237  *   - masonry_animation_duration: The duration of animations (in milliseconds).
       
   238  *   - masonry_fit_width: Sets the width of the container to the nearest column.
       
   239  *     Ideal for centering Masonry layouts.
       
   240  *   - masonry_rtl: Display items from right-to-left.
       
   241  *   - masonry_images_first: Load all images first before triggering Masonry.
       
   242  */
       
   243 function masonry_apply($container, $options = array()) {
       
   244   if (masonry_loaded() && !empty($container)) {
       
   245     // For any options not specified, use default options
       
   246     $options += masonry_default_options();
       
   247     if (!isset($options['masonry_item_selector'])) {
       
   248       $options['masonry_item_selector'] = '';
       
   249     }
       
   250 
       
   251     // Setup Masonry script
       
   252     $masonry = array(
       
   253       'masonry' => array(
       
   254         $container => array(
       
   255           'item_selector' => $options['masonry_item_selector'],
       
   256           'column_width' => $options['masonry_column_width'],
       
   257           'column_width_units' => $options['masonry_column_width_units'],
       
   258           'gutter_width' => (int) $options['masonry_gutter_width'],
       
   259           'resizable' => (bool) $options['masonry_resizable'],
       
   260           'animated' => (bool) $options['masonry_animated'],
       
   261           'animation_duration' => (int) $options['masonry_animation_duration'],
       
   262           'fit_width' => (bool) $options['masonry_fit_width'],
       
   263           'rtl' => (bool) $options['masonry_rtl'],
       
   264           'images_first' => (bool) $options['masonry_images_first'],
       
   265           'stamp' => $options['masonry_stamp_selector']
       
   266         ),
       
   267       ),
       
   268     );
       
   269     $script_file = drupal_get_path('module', 'masonry') . '/masonry.js';
       
   270 
       
   271     // Allow other modules to alter the Masonry script
       
   272     $context = array(
       
   273       'container' => $container,
       
   274       'options' => $options,
       
   275     );
       
   276     drupal_alter('masonry_script', $masonry, $script_file, $context);
       
   277 
       
   278     // Apply the script
       
   279     drupal_add_js($masonry, 'setting');
       
   280     drupal_add_js($script_file);
       
   281   }
       
   282 }