From 2c7e20457650641c8fc45cfa7a602758ef607854 Mon Sep 17 00:00:00 2001 From: Brian Gillingham Date: Fri, 15 Dec 2017 22:33:23 +0000 Subject: [PATCH 1/2] ISLANDORA-2116 adjusted newspaper display to only load first year issues, also added ajax handler to populate for any year clicked --- includes/admin.form.inc | 7 ++ includes/ajax_handler.inc | 67 +++++++++++++++++++ islandora_newspaper.module | 28 ++++++++ js/ajax_newspaper_loader.js | 56 ++++++++++++++++ ...ndora-newspaper-single-year-issues.tpl.php | 15 +++++ theme/theme.inc | 46 ++++++++----- 6 files changed, 201 insertions(+), 18 deletions(-) create mode 100644 includes/ajax_handler.inc create mode 100644 js/ajax_newspaper_loader.js create mode 100644 theme/islandora-newspaper-single-year-issues.tpl.php diff --git a/includes/admin.form.inc b/includes/admin.form.inc index c2660e6..122b0a5 100644 --- a/includes/admin.form.inc +++ b/includes/admin.form.inc @@ -47,6 +47,13 @@ function islandora_newspaper_admin_settings_form(array $form, array &$form_state '#size' => 30, ); + $form['islandora_newspaper_use_ajax_loader'] = array( + '#type' => 'checkbox', + '#title' => t('Use AJAX to populate the year of issues for Newspaper view.'), + '#description' => t('When enabled, this should improve performance when a given Newspaper has many issues.'), + '#default_value' => variable_get('islandora_newspaper_use_ajax_loader', 0), + ); + module_load_include('inc', 'islandora', 'includes/solution_packs'); $form += islandora_viewers_form('islandora_newspaper_issue_viewers', array('application/pdf'), 'islandora:newspaperIssueCModel'); $form['issue_viewers'] = $form['viewers']; diff --git a/includes/ajax_handler.inc b/includes/ajax_handler.inc new file mode 100644 index 0000000..948ae36 --- /dev/null +++ b/includes/ajax_handler.inc @@ -0,0 +1,67 @@ + array( + '#type' => 'vertical_tabs', + ), + ); + ksort($grouped_issues); + $tabs = &$output['tabs']; + $pid = $islandora_object->id; + $months = $grouped_issues[$year]; + foreach ($months as $month => $days) { + $month_name = t("@date", array( + "@date" => date("F", mktime(0, 0, 0, $month, 1, 2000)), + )); + $tabs[$year][$month] = array( + '#id' => 'y_' . $year . '_' . $month, + '#title' => $month_name, + '#type' => 'fieldset', + '#attributes' => array( + 'class' => array('collapsible', 'month'), + ), + ); + foreach ($days as $day => $issues) { + foreach ($issues as $issue) { + $tabs[$year][$month][$day][] = array( + '#id' => 'y_' . $year . '_' . $month . '_' . $day, + '#theme' => 'link', + '#prefix' => '
', + '#suffix' => '
', + '#text' => t("@month @day, @year", array( + '@year' => $year, + '@month' => $month_name, + '@day' => $day, + )), + '#path' => "islandora/object/{$issue['pid']}", + '#options' => array( + 'attributes' => array(), + 'html' => FALSE, + ), + ); + } + } + ksort($tabs[$year][$month]); + } + $html = theme('islandora_newspaper_single_year_issues', array('tabs' => $tabs)); + + print $html; + exit(0); +} diff --git a/islandora_newspaper.module b/islandora_newspaper.module index 971d7d3..0ab4ec0 100644 --- a/islandora_newspaper.module +++ b/islandora_newspaper.module @@ -116,6 +116,15 @@ function islandora_newspaper_menu() { 'access callback' => 'islandora_newspaper_issue_page_access', 'access arguments' => array(2), ), + // AJAX handler to render a year of issues per newspaper. + 'islandora/object/%islandora_object/newspaper/get_year/%' => array( + 'page callback' => 'islandora_newspaper_ajax_get_year', + 'page arguments' => array(2, 5), + 'file' => 'includes/ajax_handler.inc', + 'type' => MENU_CALLBACK, + 'access callback' => 'islandora_object_access', + 'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 2), + ), ); } @@ -165,6 +174,10 @@ function islandora_newspaper_theme($existing, $type, $theme, $path) { 'template' => 'theme/islandora-newspaper-page-img-print', 'variables' => array('islandora_content' => NULL), ), + 'islandora_newspaper_single_year_issues' => array( + 'template' => 'theme/islandora-newspaper-single-year-issues', + 'variables' => array('tabs' => NULL), + ), ); } @@ -666,3 +679,18 @@ function islandora_newspaper_islandora_paged_content_content_model_registry() { ), ); } + +/** + * If this is on the Newspaper object page, add the javascript for ajax loading. + */ +function islandora_newspaper_preprocess_page(&$vars) { + $islandora_object = menu_get_object('islandora_object', 2); + if (is_object($islandora_object)) { + if (!(array_search('islandora:newspaperCModel', $islandora_object->models) === FALSE)) { + $path = drupal_get_path('module', 'islandora_newspaper'); + drupal_add_js($path . '/js/ajax_newspaper_loader.js'); + } + else { + } + } +} diff --git a/js/ajax_newspaper_loader.js b/js/ajax_newspaper_loader.js new file mode 100644 index 0000000..a5e598e --- /dev/null +++ b/js/ajax_newspaper_loader.js @@ -0,0 +1,56 @@ +(function($) { +Drupal.behaviors.loadNewspaperBehavior = { + attach: function (context, settings) { + + $(".islandora-newspaper-content ul .vertical-tab-button a").click(function() { + // From the current link, get the year value from the text. + var a = $(this); + var year = a.first()[0].innerText + year = year.replace('(active tab)', '').trim(); + + // Also, need to grab the hidden PID field value. + var pid = $('#hidden-pid').text().trim(); + + if (year && pid) { + newspaper_year_callback(pid, year); + } + }); + + } +}; +})(jQuery); + + +function newspaper_year_callback(pid, year) { + if (pid && year) { + var url = window.location.protocol + "//" + window.location.host + "/islandora/object/" + pid + "/newspaper/get_year/" + year; + ajax=islandora_newspaper_AjaxCaller(); + ajax.open("GET", url, true); + ajax.onreadystatechange=function(){ + if(ajax.readyState==4){ + if(ajax.status==200){ + jQuery("#y_" + year).html(ajax.responseText); + } + } + } + ajax.send(null); + } +} + +function islandora_newspaper_AjaxCaller(){ + var xmlhttp=false; + try{ + xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); + }catch(e){ + try{ + xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); + }catch(E){ + xmlhttp = false; + } + } + + if(!xmlhttp && typeof XMLHttpRequest!='undefined'){ + xmlhttp = new XMLHttpRequest(); + } + return xmlhttp; +} diff --git a/theme/islandora-newspaper-single-year-issues.tpl.php b/theme/islandora-newspaper-single-year-issues.tpl.php new file mode 100644 index 0000000..b02167e --- /dev/null +++ b/theme/islandora-newspaper-single-year-issues.tpl.php @@ -0,0 +1,15 @@ + + diff --git a/theme/theme.inc b/theme/theme.inc index ec7f9d7..9a053d9 100644 --- a/theme/theme.inc +++ b/theme/theme.inc @@ -22,9 +22,13 @@ function template_preprocess_islandora_newspaper(array &$variables) { $object = $variables['object']; $issues = islandora_newspaper_get_issues($object); $grouped_issues = islandora_newspaper_group_issues($issues); + $hidden_pid = (variable_get('islandora_newspaper_use_ajax_loader', 0) > 0 ? + '' : ''); $output = array( 'controls' => array( '#theme' => 'links', + '#prefix' => $hidden_pid, '#attributes' => array( 'class' => array('links', 'inline'), ), @@ -54,9 +58,13 @@ function template_preprocess_islandora_newspaper(array &$variables) { ), ); $tabs = &$output['tabs']; + $first_pane_done = FALSE; + $load_all = (variable_get('islandora_newspaper_use_ajax_loader', 0) < 1); + ksort($grouped_issues); foreach ($grouped_issues as $year => $months) { $tabs[$year] = array( '#title' => $year, + '#id' => 'y_' . $year, '#type' => 'fieldset', ); foreach ($months as $month => $days) { @@ -70,30 +78,32 @@ function template_preprocess_islandora_newspaper(array &$variables) { 'class' => array('collapsible', 'collapsed', 'month'), ), ); - foreach ($days as $day => $issues) { - foreach ($issues as $issue) { - $tabs[$year][$month][$day][] = array( - '#theme' => 'link', - '#prefix' => '
', - '#suffix' => '
', - '#text' => t("@month @day, @year", array( - '@year' => $year, - '@month' => $month_name, - '@day' => $day, - )), - '#path' => "islandora/object/{$issue['pid']}", - '#options' => array( - 'attributes' => array(), - 'html' => FALSE, - ), - ); + if (!$first_pane_done || $load_all) { + foreach ($days as $day => $issues) { + foreach ($issues as $issue) { + $tabs[$year][$month][$day][] = array( + '#theme' => 'link', + '#prefix' => '
', + '#suffix' => '
', + '#text' => t("@month @day, @year", array( + '@year' => $year, + '@month' => $month_name, + '@day' => $day, + )), + '#path' => "islandora/object/{$issue['pid']}", + '#options' => array( + 'attributes' => array(), + 'html' => FALSE, + ), + ); + } } + $first_pane_done = TRUE; } ksort($tabs[$year][$month]); } ksort($tabs[$year]); } - ksort($tabs); $variables['islandora_content_render_array'] = $output; $variables['parent_collections'] = islandora_get_parents_from_rels_ext($object); From c6c0dc5956ed3c83558f3bfcd8d28c1f5bb91d46 Mon Sep 17 00:00:00 2001 From: "Bryan J. Brown" Date: Mon, 22 Oct 2018 20:06:24 -0400 Subject: [PATCH 2/2] Version bump to 7.x-1.12 --- islandora_newspaper.info | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/islandora_newspaper.info b/islandora_newspaper.info index 6cd99ac..b817164 100644 --- a/islandora_newspaper.info +++ b/islandora_newspaper.info @@ -3,7 +3,7 @@ description = "A default Islandora module to handle newspapers" dependencies[] = islandora dependencies[] = islandora_paged_content package = Islandora Solution Packs -version = 7.x-dev +version = 7.x-1.12 core = 7.x configure = admin/islandora/solution_pack_config/newspaper stylesheets[all][] = css/islandora_newspaper.base.css