Browse Source

Added support for d3.js in Tripal core and added a last updated note to feature view

Lacey Sanderson 10 years ago
parent
commit
aa3dadd6b1

+ 50 - 0
tripal_core/api/tripal_core.d3js.api.inc

@@ -0,0 +1,50 @@
+<?php
+/**
+ *
+ */
+
+/**
+ * Implements hook_libraries_info().
+ */
+function tripal_core_libraries_info() {
+  $libraries = array();
+  $libraries['d3'] = array(
+    'name' => 'D3.js',
+    'vendor url' => 'http://d3js.org/',
+    'download url' => 'https://github.com/mbostock/d3',
+    'version arguments' => array(
+      'file' => 'd3.js',
+      'pattern' => '/\s*version: "(\d+\.\d+\.\d+)"/',
+    ),
+    'files' => array(
+      'js' => array(
+        'd3.min.js',
+      ),
+    ),
+  );
+
+  return $libraries;
+}
+
+/**
+ * Load D3.js releated javascripts for the current page.
+ */
+function tripal_add_d3js() {
+
+  // First try to load d3.js using the libraries API.
+  // This will work if the site admin has saved d3.js in their libraries folder.
+  $library = libraries_load('d3');
+
+  // If we were not able to load d3.js using the libraries API
+  // then revert to loading the remote files manually.
+  if (!isset($library['loaded']) OR !$library['loaded']) {
+
+    // If SSL is being used then use a secure CDN for d3.js
+    if (isset($_SERVER['HTTPS'])) {
+      drupal_add_js('https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js');
+    }
+    else {
+      drupal_add_js('http://d3js.org/d3.v3.min.js');
+    }
+  }
+}

+ 1 - 1
tripal_core/tripal_core.module

@@ -34,7 +34,7 @@ require_once 'api/tripal_core.files.api.inc';
 require_once 'api/tripal_core.jobs.api.inc';
 require_once 'api/tripal_core.tripal.api.inc';
 require_once 'api/tripal_core.tripal_variables.api.inc';
-
+require_once 'api/tripal_core.d3js.api.inc';
 require_once 'api/tripal_core.DEPRECATED.inc';
 
 // INCLUDES

+ 20 - 4
tripal_feature/includes/tripal_feature.admin.inc

@@ -99,11 +99,27 @@ function tripal_feature_admin_feature_view() {
   sort($type_names);
   sort($organism_names);
 
-  drupal_add_js('http://d3js.org/d3.v3.min.js');
-  drupal_add_js('http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js');
-  drupal_add_js(drupal_get_path('module','tripal_feature') . '/theme/js/tripalFeature.adminChart.js');
-  drupal_add_js(array('tripalFeature' => array('admin' => array('summary' => $chart, 'types' => $type_names, 'organisms' => $organism_names))), 'setting');
+  // We also need to add information about the materialized views
+  // so that admin can update it and know how recent the data is.
+  $mview = db_query('
+    SELECT mview_id, name, last_update
+    FROM tripal_mviews
+    WHERE mv_table=:mv_table',
+    array(':mv_table' => 'organism_feature_count')
+  )->fetchObject();
+
+  tripal_add_d3js();
   drupal_add_css(drupal_get_path('module','tripal_feature') . '/theme/css/tripal_feature.css');
+  drupal_add_js(drupal_get_path('module','tripal_feature') . '/theme/js/tripalFeature.adminChart.js');
+  drupal_add_js(array('tripalFeature' => array('admin' => array(
+    'summary' => $chart,
+    'types' => $type_names,
+    'organisms' => $organism_names,
+    'mviewUrl' => url('admin/tripal/schema/mviews/action/update/' . $mview->mview_id),
+    'mviewUable' => $mview->name,
+    'mviewLastUpdate' => format_date($mview->last_update),
+  ))), 'setting');
+
 
   return $output;
 }

+ 16 - 2
tripal_feature/theme/js/tripalFeature.adminChart.js

@@ -111,9 +111,11 @@ Drupal.behaviors.tripalFeature_adminSummaryChart = {
           .text(function(d) { return formatNum(d.y1 - d.y0); });
 
       // Add the total to the top of the bar.
-      svg.selectAll("text.bar")
+      svg.selectAll("g.bar-totals")
         .data(data)
-      .enter().append("text")
+      .enter().append('g')
+        .classed('bar-totals', true)
+      .append("text")
         .attr("class", "bar-label")
         .attr("text-anchor", "middle")
         .attr("x", function(d) { return x0(d.name) + x0.rangeBand()/2; })
@@ -139,6 +141,18 @@ Drupal.behaviors.tripalFeature_adminSummaryChart = {
           .attr('font-style','italic')
           .text(function(d) { return d; });
 
+      // Add a small blurb mentioning this is from an mview and you should update ;).
+      var blurb = svg.append('g')
+          .classed('figure-legend', true)
+        .append('text')
+          .attr('x', width - 18)
+          .attr('y', height + 70)
+          .attr('font-style','italic')
+          .style("fill", '#7F7F7F')
+          .style("font-size","10px")
+          .style("text-anchor", "end")
+          .text('Updated on ' + Drupal.settings.tripalFeature.admin.mviewLastUpdate);
+
       function wrap(text, width) {
         text.each(function() {
           var text = d3.select(this),