Mercurial > defr > drupal > popups
diff popups.module @ 1:4215c43e74eb
Popups: Mise à jour en version alpha6
author | Franck Deroche <franck@defr.org> |
---|---|
date | Fri, 31 Dec 2010 13:44:00 +0100 |
parents | 76f9b43738f2 |
children | 5efa741592f9 |
line wrap: on
line diff
--- a/popups.module Fri Dec 31 13:41:08 2010 +0100 +++ b/popups.module Fri Dec 31 13:44:00 2010 +0100 @@ -1,9 +1,9 @@ <?php -// $Id: popups.module,v 1.11.8.10 2009/03/21 00:57:15 starbow Exp $ +// $Id: popups.module,v 1.11.8.14 2010/12/10 19:58:10 drewish Exp $ /** * @file - * This module provides a hook_popups for links to be openned in an Ajax Popup Modal Dialog. + * This module provides a hook_popups for links to be openned in an Ajax Popup Modal Dialog. */ @@ -16,8 +16,8 @@ * * @return array of new menu items. */ -function popups_menu() { - +function popups_menu() { + // Admin Settings. $items['admin/settings/popups'] = array( 'page callback' => 'drupal_get_form', @@ -25,17 +25,17 @@ 'title' => 'Popups', 'access arguments' => array('administer site configuration'), 'description' => 'Configure the page-in-a-dialog behavior.', - ); - + ); + return $items; } /** * Implementation of hook_init(). - * + * * Look at the page path and see if popup behavior has been requested for any links in this page. */ -function popups_init() { +function popups_init() { $popups = popups_get_popups(); if (variable_get('popups_always_scan', 0)) { @@ -49,13 +49,13 @@ elseif (strpos($path, '*') !== FALSE && drupal_match_path($_GET['q'], $path)) { popups_add_popups($popup_config); } - } - + } + $render_mode = ''; if (isset($_SERVER['HTTP_X_DRUPAL_RENDER_MODE'])) { $render_mode = $_SERVER['HTTP_X_DRUPAL_RENDER_MODE']; } - + // Check and see if the page_override param is in the URL. // Note - the magic happens here. // Need to cache the page_override flag in the session, so it will effect @@ -68,39 +68,38 @@ if (isset($_SESSION['page_override'])) { // This call will not return on form submission. $content = menu_execute_active_handler(); - - // The call did return, so it wasn't a form request, + + // The call did return, so it wasn't a form request, // so we are returning a result, so clear the session flag. $override = $_SESSION['page_override']; unset($_SESSION['page_override']); - + // Menu status constants are integers; page content is a string. if (isset($content) && !is_int($content) && isset($override)) { - print popups_render_as_json($content); + print popups_render_as_json($content); exit; // Do not continue processing request in index.html. - } + } } - + } /** * Implementation of hook_form_alter(). - * + * * Look at the form_id and see if popup behavior has been requested for any links in this form. * * @param form_array $form * @param array $form_state - * @param str $form_id: + * @param str $form_id: */ function popups_form_alter(&$form, $form_state, $form_id) { // Add popup behavior to the form if requested. -// dsm($form_id); $popups = popups_get_popups(); if (isset($popups[$form_id])) { popups_add_popups($popups[$form_id]); - } + } - // Alter the theme configuration pages, to add a per-theme-content selector. + // Alter the theme configuration pages, to add a per-theme-content selector. $theme = arg(4); if ($form_id == 'system_theme_settings' && $theme) { $form['popups'] = array( @@ -113,11 +112,11 @@ '#title' => t('Content Selector'), '#default_value' => variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector()), '#description' => t("jQuery selector to define the page's content area on this theme."), - ); + ); $form['popups']['popups_theme'] = array( '#type' => 'hidden', '#value' => $theme, - ); + ); $form['#submit'][] = 'popups_theme_settings_form_submit'; } } @@ -132,7 +131,11 @@ * @param $content: themed html. * @return $content in a json wrapper with metadata. */ -function popups_render_as_json($content) { +function popups_render_as_json($content) { + // Call theme_page so modules like jquery_update can do their thing. We don't + // really care about the mark up though. + $ignore = theme('page', $content); + $path = $_GET['q']; // Get current path from params. return drupal_json(array( 'title' => drupal_get_title(), @@ -140,7 +143,7 @@ 'path' => $path, 'content' => $content, 'js' => popups_get_js(), - 'css' => popups_get_css(), + 'css' => popups_get_css(), )); } @@ -150,7 +153,7 @@ function popups_get_js() { $js = array_merge_recursive(drupal_add_js(), drupal_add_js(NULL, NULL, 'footer')); $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1); - + $popup_js = array(); foreach ($js as $type => $data) { @@ -173,9 +176,21 @@ } } - // A special exception, never include the popups settings in the JS array. - // ??? -// unset($popup_js['setting']['popups']); + unset($popup_js['core']['misc/jquery.js']); + unset($popup_js['core']['misc/drupal.js']); + + if (module_exists('jquery_update')) { + foreach (jquery_update_get_replacements() as $type => $replacements) { + foreach ($replacements as $find => $replace) { + if (isset($popup_js[$type][$find])) { + // Create a new entry for the replacement file, and unset the original one. + $replace = JQUERY_UPDATE_REPLACE_PATH .'/'. $replace; + //$popup_js[$type][$replace] = str_replace($find, $replace, $popup_js[$type][$find]); + unset($popup_js[$type][$find]); + } + } + } + } return $popup_js; } @@ -233,12 +248,12 @@ /** * Define hook_popups(). * Build the list of popup rules from all modules that implement hook_popups. - * + * * Retrieves the list of popup rules from all modules that implement hook_popups. * * @param $reset * (optional) If set to TRUE, forces the popup rule cache to reset. - * + * */ function popups_get_popups($reset = FALSE) { static $popups = NULL; @@ -247,37 +262,37 @@ if (!$reset && ($cache = cache_get('popups:popups')) && !empty($cache->data)) { $popups = $cache->data; } - else { + else { // Call all hook_popups and cache results. $popups = module_invoke_all('popups'); - + // Invoke hook_popups_alter() to allow altering the default popups registry. drupal_alter('popups', $popups); - // Save the popup registry in the cache. + // Save the popup registry in the cache. cache_set('popups:popups', $popups); - } + } } return $popups; } /** * Attach the popup behavior to the page. - * - * The default behavoir of a popup is to open a form that will modify the original page. - * The popup submits the form and reloads the original page with the resulting new content. + * + * The default behavoir of a popup is to open a form that will modify the original page. + * The popup submits the form and reloads the original page with the resulting new content. * The popup then replaces the original page's content area with the new copy of that content area. * * @param array $rules: Array of rules to apply to the page or form, keyed by jQuery link selector. - * See README.txt for a listing of the options, and popups_admin.module for examples. + * See README.txt for a listing of the options, and popups_admin.module for examples. */ -function popups_add_popups($rules=NULL) { +function popups_add_popups($rules=NULL) { static $added = FALSE; $settings = array('popups' => array()); - + if (is_array($rules)) { $settings['popups']['links'] = array(); - foreach ($rules as $popup_selector => $options) { + foreach ($rules as $popup_selector => $options) { if (is_array($options)) { $settings['popups']['links'][$popup_selector] = $options; } @@ -296,7 +311,7 @@ if (!$theme) { $theme = variable_get('theme_default','none'); } - + drupal_add_js('misc/jquery.form.js'); drupal_add_css(drupal_get_path('module', 'popups') .'/popups.css'); drupal_add_js(drupal_get_path('module', 'popups') .'/popups.js'); @@ -312,15 +327,14 @@ drupal_add_css($skins[$skin]['css']); if (isset($skins[$skin]['js'])) { drupal_add_js($skins[$skin]['js']); - } + } } - + $default_target_selector = variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector()); - + $settings['popups']['originalPath'] = $_GET['q']; $settings['popups']['defaultTargetSelector'] = $default_target_selector; $settings['popups']['modulePath'] = drupal_get_path('module', 'popups'); -// $settings['popups']['popupFinalMessage'] = variable_get('popups_popup_final_message', 1); $settings['popups']['autoCloseFinalMessage'] = variable_get('popups_autoclose_final_message', 1); drupal_add_js( $settings, 'setting' ); $added = TRUE; @@ -352,14 +366,14 @@ /** * Implementation of hook_popups_skins. - * + * * This hook allows other modules to create additional custom skins for the * popups module. - * + * * @return array * An array of key => value pairs suitable for inclusion as the #options in a - * select or radios form element. Each key must be the location of at least a - * css file for a popups skin. Optionally can have a js index as well. Each + * select or radios form element. Each key must be the location of at least a + * css file for a popups skin. Optionally can have a js index as well. Each * value should be the name of the skin. */ function popups_popups_skins() { @@ -381,7 +395,7 @@ /** * Returns the default jQuery content selector as a string. - * Currently uses the selector for Garland. + * Currently uses the selector for Garland. * Sometime in the future I will change this to '#content' or '#content-content'. */ function _popups_default_content_selector() { @@ -398,7 +412,6 @@ */ function popups_admin_settings() { popups_add_popups(); -// drupal_add_css(drupal_get_path('module', 'popups'). '/skins/blue/blue.css'); // temp drupal_set_title("Popups Settings"); $form = array(); @@ -416,7 +429,7 @@ // Retrieve all available skins, forcing the registry to refresh. $skins['Unskinned'] = array(); $skins += popups_skins(TRUE); - + $skin_options = drupal_map_assoc(array_keys($skins)); $form['popups_skins'] = array( '#type' => 'fieldset', @@ -432,13 +445,13 @@ '#options' => $skin_options, ); - + return system_settings_form($form); } /** * popups_form_alter adds this submit handler to the theme pages. - * Set a per-theme jQuery content selector that gets passed into the js settings. + * Set a per-theme jQuery content selector that gets passed into the js settings. * * @param $form * @param $form_state @@ -449,3 +462,39 @@ variable_set('popups_'. $theme .'_content_selector', $content_selector); } +/** + * Implementation of hook_preprocess_hook(). + * + * When CSS or JS aggregation is enabled make a list of the CSS/JS incorporated + * in it so we don't re-add it when loading the popup content. + */ +function popups_preprocess_page() { + $base_path = base_path(); + + if (variable_get('preprocess_css', 0)) { + $css_on_page = array(); + foreach (popups_get_css() as $type => $files) { + foreach ($files as $path => $html) { + $css_on_page[$base_path . $path] = 1; + } + } + $settings['popups']['originalCSS'] = $css_on_page; + } + + if (variable_get('preprocess_js', 0)) { + $js_on_page = array(); + $js = popups_get_js(); + // We don't care about settings or inline css. + unset($js['inline'], $js['setting']); + foreach ($js as $type => $files) { + foreach ($files as $path => $html) { + $js_on_page[$base_path . $path] = 1; + } + } + $settings['popups']['originalJS'] = $js_on_page; + } + + if (isset($settings)) { + drupal_add_js($settings, 'setting'); + } +}