Skip to content

Commit

Permalink
#47: Port export configuration diff view.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfranco committed Dec 19, 2024
1 parent a0afb05 commit 775bf21
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 114 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@
[submodule "library/lazy_sessions"]
path = library/lazy_sessions
url = https://github.com/middlebury/lazy_sessions.git
[submodule "library/jsdifflib"]
path = library/jsdifflib
url = https://github.com/cemerick/jsdifflib.git
branch = master
33 changes: 0 additions & 33 deletions application/views/scripts/export/revisiondiff.phtml

This file was deleted.

34 changes: 0 additions & 34 deletions application/views/scripts/export/revisionhistory.phtml

This file was deleted.

38 changes: 38 additions & 0 deletions assets/export_revision_diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'diff2html/bundles/css/diff2html.min.css';
import './styles/export.css';
import $ from 'jquery';
import 'jquery-ui';
// import { Diff2Html } from 'diff2html';
import Diff2HtmlUI from 'diff2html/bundles/js/diff2html-ui.min.js';
import * as Diff from 'diff';

function buildDiffView(text1, text2, label1, label2) {
var changes = Diff.diffLines(text1, text2);
if (changes.length > 1) {
const diff = Diff.createPatch('', text1, text2, label1, label2, { options: { context: 5} });
const configuration = {
drawFileList: true,
matching: 'lines',
outputFormat: 'side-by-side',
fileListToggle: false,
fileListStartVisible: false,
fileContentToggle: false,
container: document.createElement('div'),
diff2htmlUi:null,
highlight: true
};
var diff2htmlUi = new Diff2HtmlUI.Diff2HtmlUI($('#diff').get(0), diff, configuration);
diff2htmlUi.draw();
} else {
$('#diff').append("<div style='border:1px solid gray;padding:10px'><p>No difference in JSON data between these revisions</p></div>");
}
}

$(document).ready(function() {
buildDiffView(
$("#rev1").val(),
$("#rev2").val(),
"Revision #" + $("#rev1").data('rev-id') + " (" + $("#rev1").data('rev-date') + ")",
"Revision #" + $("#rev2").data('rev-id') + " (" + $("#rev2").data('rev-date') + ")",
);
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import './styles/export.css';
import $ from 'jquery';
import 'jquery-ui';

var selected = [];

Expand All @@ -21,7 +24,7 @@ function compare(url) {
var rev1 = $($('#' + selected[0]).parents('tr').find('.revId')[0]).val();
var rev2 = $($('#' + selected[1]).parents('tr').find('.revId')[0]).val();
if(!rev1 || !rev2) return;
var win = window.open(url + '/' + rev1 + '/' + rev2, '_blank');
var win = window.open(url.replace(/-rev1-/, rev1).replace(/-rev2-/, rev2), '_blank');
win.focus();
}

Expand Down Expand Up @@ -52,4 +55,8 @@ $(document).ready(function() {
if (selected.length > 2) selected.shift();
renderSelected();
});

$('.compare-revisions-button').on('click', function() {
compare($(this).data('url'));
});
});
5 changes: 5 additions & 0 deletions assets/styles/export.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* Hide the file list and renamed tag since I'm using the labels as virtual
file names. */
.d2h-file-list-wrapper, .d2h-moved-tag {
display:none;
}

.added {
background: #9fff99;
Expand Down
1 change: 0 additions & 1 deletion docroot/javascript/jsdifflib

This file was deleted.

24 changes: 24 additions & 0 deletions importmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
'path' => 'export.js',
'entrypoint' => true,
],
'export_revision_diff' => [
'path' => 'export_revision_diff.js',
'entrypoint' => true,
],
'export_revision_history' => [
'path' => 'export_revision_history.js',
'entrypoint' => true,
],
'offering_search' => [
'path' => 'offering_search.js',
'entrypoint' => true,
Expand Down Expand Up @@ -53,4 +61,20 @@
'version' => '2.1.3',
'type' => 'css',
],
'diff' => [
'version' => '5.1.0',
],
'hogan.js' => [
'version' => '3.0.2',
],
'diff2html' => [
'version' => '3.4.48',
],
'diff2html/bundles/css/diff2html.min.css' => [
'version' => '3.4.48',
'type' => 'css',
],
'diff2html/bundles/js/diff2html-ui.min.js' => [
'version' => '3.4.48',
],
];
1 change: 0 additions & 1 deletion library/jsdifflib
Submodule jsdifflib deleted from f728d4
72 changes: 33 additions & 39 deletions src/Controller/AdminExports.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,22 @@ public function latestrevisionAction(int $exportId)
#[Route('/admin/exports/{exportId}/revisions', name: 'admin_exports_config_revisions')]
public function revisionhistoryAction(int $exportId)
{
$request = $this->getRequest();
if (!$request->getParam('config') || -1 === $request->getParam('config')) {
header('HTTP/1.1 400 Bad Request');
echo 'A config ID must be specified.';
exit;
}
$this->view->configId = filter_var($request->getParam('config'), \FILTER_SANITIZE_NUMBER_INT);
$db = Zend_Registry::get('db');
$data['configId'] = $exportId;
$db = $this->entityManager->getConnection();

$query = 'SELECT label FROM archive_configurations WHERE id = ?';
$stmt = $db->prepare($query);
$stmt->execute([$this->view->configId]);
$this->view->configLabel = $stmt->fetch()['label'];
$stmt->bindValue(1, $exportId);
$result = $stmt->executeQuery();
$data['configLabel'] = $result->fetchAssociative()['label'];

$query = 'SELECT * FROM archive_configuration_revisions WHERE arch_conf_id = ? ORDER BY last_saved DESC';
$stmt = $db->prepare($query);
$stmt->execute([$this->view->configId]);
$this->view->revisions = $stmt->fetchAll();
$stmt->bindValue(1, $exportId);
$result = $stmt->executeQuery();
$data['revisions'] = $result->fetchAllAssociative();

return $this->render('admin/export/revisionhistory.html.twig', $data);
}

/**
Expand All @@ -107,27 +106,21 @@ public function revisionhistoryAction(int $exportId)
*
* @since 1/25/18
*/
public function revisiondiffAction()
#[Route('/admin/exports/revisiondiff/{rev1}/{rev2}', name: 'admin_exports_config_revision_diff')]
public function revisiondiffAction(int $rev1, int $rev2)
{
if (!$this->_getParam('rev1') || !$this->_getParam('rev2')) {
header('HTTP/1.1 400 Bad Request');
echo 'This route requires two revision IDs';
exit;
} else {
$db = Zend_Registry::get('db');
$query = 'SELECT * FROM archive_configuration_revisions WHERE id = ?';
$stmt = $db->prepare($query);
$stmt->execute([filter_var($this->_getParam('rev1'), \FILTER_SANITIZE_NUMBER_INT)]);
$this->rev1 = $stmt->fetch();
$stmt = $db->prepare($query);
$stmt->execute([filter_var($this->_getParam('rev2'), \FILTER_SANITIZE_NUMBER_INT)]);
$this->rev2 = $stmt->fetch();
$db = $this->entityManager->getConnection();
$query = 'SELECT * FROM archive_configuration_revisions WHERE id = ?';
$stmt = $db->prepare($query);
$stmt->bindValue(1, $rev1);
$result = $stmt->executeQuery();
$data['rev1'] = $result->fetchAssociative();

$this->view->text1 = $this->rev1['json_data'];
$this->view->text2 = $this->rev2['json_data'];
$this->view->time1 = $this->rev1['last_saved'];
$this->view->time2 = $this->rev2['last_saved'];
}
$stmt->bindValue(1, $rev2);
$result = $stmt->executeQuery();
$data['rev2'] = $result->fetchAssociative();

return $this->render('admin/export/revisiondiff.html.twig', $data);
}

/**
Expand All @@ -137,7 +130,8 @@ public function revisiondiffAction()
*
* @since 1/25/18
*/
public function viewjsonAction()
#[Route('/admin/exports/revision/{revisionId}.json', name: 'admin_exports_config_revision_json')]
public function viewjsonAction(int $revisionId)
{
$request = $this->getRequest();
if (!$request->getParam('revision') || -1 === $request->getParam('revision')) {
Expand All @@ -149,8 +143,8 @@ public function viewjsonAction()
$db = Zend_Registry::get('db');
$query = 'SELECT * FROM archive_configuration_revisions WHERE id = ?';
$stmt = $db->prepare($query);
$stmt->execute([filter_var($this->_getParam('revision'), \FILTER_SANITIZE_NUMBER_INT)]);
$this->view->revision = $stmt->fetch();
$stmt->execute([filter_var($request->get('revision'), \FILTER_SANITIZE_NUMBER_INT)]);
$data['revision'] = $stmt->fetch();
}

/**
Expand All @@ -164,7 +158,7 @@ public function viewjsonAction()
public function newconfigAction()
{
$lookupSession = $this->osidRuntime->getCourseManager()->getCourseCatalogLookupSession();
$this->view->catalogs = $lookupSession->getCourseCatalogs();
$data['catalogs'] = $lookupSession->getCourseCatalogs();
}

/**
Expand Down Expand Up @@ -252,13 +246,13 @@ public function newjobAction()
$request = $this->getRequest();

$db = Zend_Registry::get('db');
$this->view->configs = $db->query('SELECT * FROM archive_configurations')->fetchAll();
$data['configs'] = $db->query('SELECT * FROM archive_configurations')->fetchAll();

$this->view->config = null;
$data['config'] = null;
if ($request->getParam('config')) {
foreach ($this->view->configs as $config) {
foreach ($data['configs'] as $config) {
if ($config['label'] === $request->getParam('config')) {
$this->view->config = $config;
$data['config'] = $config;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions templates/admin/export/revisiondiff.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'base.html.twig' %}

{% block importmap %}
{# do NOT call parent() #}
{{ importmap(['app', 'export_revision_diff']) }}
{% endblock %}

{% block body %}

<div id='diff'></div>
<textarea id="rev1" data-rev-id="{{ rev1.id }}" data-rev-date="{{ rev1.last_saved }}" style="display:none">{{ rev1.json_data }}</textarea>
<textarea id="rev2" data-rev-id="{{ rev2.id }}" data-rev-date="{{ rev2.last_saved }}" style="display:none">{{ rev2.json_data }}</textarea>

{% endblock %}
47 changes: 47 additions & 0 deletions templates/admin/export/revisionhistory.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% extends 'base.html.twig' %}

{% block importmap %}
{# do NOT call parent() #}
{{ importmap(['app', 'export_revision_history']) }}
{% endblock %}

{% block body %}
<h1>Revision history for {{ configLabel }}</h1>
<div class='admin-menu'><a href="{{ url('admin_exports_config', {exportId: configId}) }}">&laquo; Back to catalog export configuration</a></div>

<div class='config-menu'>
<button class="compare-revisions-button" data-url="{{ url('admin_exports_config_revision_diff', {rev1: '-rev1-', rev2: '-rev2-'}) }}">Compare selected revisions</button>
</div>

{% if revisions | length %}
<table id='revisions-table'>
<tr>
<th>Compare</th>
<th>Date</th>
<th>Author</th>
<th>Note</th>
<th>JSON</th>
<th>Actions</th>
</tr>
{% for revision in revisions %}
<tr>
<td>
<input id='radio{{ revision.id }}' type='radio'/>
<input class='revId' type='hidden' value='{{ revision.id }}' />
</td>
<td class='timestamp'>{{ revision.last_saved }}</td>
<td>{{ revision.user_disp_name }}</td>
<td width='200'>{{ revision.note }}</td>
<td>
<a class='link-button' href='{{ url('admin_exports_config_revision_json', {revisionId: revision.id}) }}' target="_blank">View JSON</a>
</td>
<td>
<button onclick="revertTo($revision['id'])">Revert to this revision</button>
</td>
</tr>
{% endfor %}
</table>
{% else %}
No revisions in history for config: {{ configLabel }}
{% endif %}
{% endblock %}
2 changes: 1 addition & 1 deletion templates/admin/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ul>
<li><a href="{{ url("admin_terms_list") }}">Manage Term Visibility</a></li>
<li><a href="{{ url("list_antirequisites") }}">Manage Anti-Requisites</a></li>
<li><a href="{{ url("admin_exports_list_configs") }}">Manage Catalog Archive Configurations</a></li>
<li><a href="{{ url("admin_exports_config") }}">Manage Catalog Archive Configurations</a></li>
<li><a href="{{ url("admin_exports_list_schedules") }}">Manage Catalog Archive Scheduling</a></li>
<li><a href="{{ url("masquerade") }}">Masquerade</a></li>
<li><a href="{{ url("markup") }}">View Catalog Markup Example</a></li>
Expand Down

0 comments on commit 775bf21

Please sign in to comment.