Skip to content

Commit

Permalink
fixed #145
Browse files Browse the repository at this point in the history
  • Loading branch information
edsilv committed Sep 17, 2015
1 parent 7509659 commit 1e3abbb
Show file tree
Hide file tree
Showing 17 changed files with 757 additions and 34 deletions.
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ module.exports = function (grunt) {
flatten: true,
src: [
'es6-promise/promise.min.js',
'exjs/dist/ex.min.js',
'extensions/dist/extensions.js',
'http-status-codes/dist/http-status-codes.js',
'jquery-plugins/dist/jquery-plugins.js',
Expand All @@ -225,6 +226,7 @@ module.exports = function (grunt) {
expand: true,
flatten: true,
src: [
'exjs/dist/ex.d.ts',
'extensions/typings/extensions.d.ts',
'http-status-codes/dist/http-status-codes.d.ts',
'jquery-plugins/typings/jquery-plugins.d.ts',
Expand Down
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"r.js": "~2.1.18",
"utils": "edsilv/utils#~0.0.23",
"uv-cy-GB-theme": "universalviewer/uv-cy-GB-theme",
"uv-en-GB-theme": "universalviewer/uv-en-GB-theme"
"uv-en-GB-theme": "universalviewer/uv-en-GB-theme",
"exjs": "~0.2.15"
}
}
}
2 changes: 1 addition & 1 deletion examples
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require.config({
paths: {
'browserdetect': 'lib/browserdetect',
'promise': 'lib/promise.min',
'ex': 'lib/ex.min',
'ext': 'lib/extensions',
'httpstatuscodes': 'lib/http-status-codes',
'jquery': 'lib/jquery-1.10.2.min',
Expand Down Expand Up @@ -50,6 +51,7 @@ require([
'extensions/uv-seadragon-extension/Provider',
'manifesto',
'browserdetect',
'ex',
'ext',
'httpstatuscodes',
'jquery',
Expand Down
4 changes: 3 additions & 1 deletion src/extensions/uv-seadragon-extension/ISeadragonProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import IProvider = require("../../modules/uv-shared-module/IProvider");
import ExternalResource = require("../../modules/uv-shared-module/ExternalResource");
import IProvider = require("../../modules/uv-shared-module/IProvider");
import SearchResult = require("./SearchResult");
import TreeSortType = require("./TreeSortType");

interface ISeadragonProvider extends IProvider{
getAutoCompleteService(): Manifesto.IService;
Expand All @@ -13,6 +14,7 @@ interface ISeadragonProvider extends IProvider{
getSearchResultByCanvasIndex(index: number): SearchResult;
getSearchWithinService(): Manifesto.IService;
getSearchWithinServiceUri(): string;
getSortedTree(sortType: TreeSortType): Manifesto.TreeNode;
isSearchWithinEnabled(): boolean;
parseSearchWithinResults(results: any);
searchResults: SearchResult[];
Expand Down
227 changes: 225 additions & 2 deletions src/extensions/uv-seadragon-extension/Provider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import BootStrapper = require("../../Bootstrapper");
import BaseProvider = require("../../modules/uv-shared-module/BaseProvider");
import BootStrapper = require("../../Bootstrapper");
import ExternalResource = require("../../modules/uv-shared-module/ExternalResource");
import ISeadragonProvider = require("./ISeadragonProvider");
import SearchResult = require("./SearchResult");
import SearchResultRect = require("./SearchResultRect");
import ExternalResource = require("../../modules/uv-shared-module/ExternalResource");
import TreeSortType = require("./TreeSortType");

class Provider extends BaseProvider implements ISeadragonProvider{

Expand Down Expand Up @@ -247,6 +248,228 @@ class Provider extends BaseProvider implements ISeadragonProvider{
}
return null;
}

// returns a list of treenodes for each decade.
// expanding a decade generates a list of years
// expanding a year gives a list of months containing issues
// expanding a month gives a list of issues.
getSortedTree(sortType: TreeSortType): Manifesto.TreeNode {

var tree = this.iiifResource.getTree();
var sortedTree = manifesto.getTreeNode();

if (sortType === TreeSortType.date){
this.getSortedTreeNodesByDate(sortedTree, tree);
} else if (sortType === TreeSortType.none) {
sortedTree = tree;
}

return sortedTree;
}

getSortedTreeNodesByDate(sortedTree: Manifesto.TreeNode, tree: Manifesto.TreeNode): void{

var all: Manifesto.TreeNode[] = tree.nodes.en().traverseUnique(node => node.nodes)
.where((n) => n.data.type === manifesto.TreeNodeType.collection().toString() ||
n.data.type === manifesto.TreeNodeType.manifest().toString()).toArray();

//var collections: Manifesto.TreeNode[] = tree.nodes.en().traverseUnique(n => n.nodes)
// .where((n) => n.data.type === manifesto.TreeNodeType.collection().toString()).toArray();

var manifests: Manifesto.TreeNode[] = tree.nodes.en().traverseUnique(n => n.nodes)
.where((n) => n.data.type === manifesto.TreeNodeType.manifest().toString()).toArray();

this.createDecadeNodes(sortedTree, all);
this.sortDecadeNodes(sortedTree);
this.createYearNodes(sortedTree, all);
this.sortYearNodes(sortedTree);
this.createMonthNodes(sortedTree, manifests);
this.sortMonthNodes(sortedTree);
this.createDateNodes(sortedTree, manifests);

this.pruneDecadeNodes(sortedTree);
}

createDecadeNodes(rootNode: Manifesto.TreeNode, nodes: Manifesto.TreeNode[]): void{
var decadeNode: Manifesto.TreeNode;

for (var i = 0; i < nodes.length; i++) {
var node: Manifesto.TreeNode = nodes[i];
var year = this.getNodeYear(node);
var decade = Number(year.toString().substr(2, 1));
var endYear = Number(year.toString().substr(0, 3) + "9");

if(!this.getDecadeNode(rootNode, year)){
decadeNode = manifesto.getTreeNode();
decadeNode.label = year + " - " + endYear;
decadeNode.navDate = node.navDate;
decadeNode.data.startYear = year;
decadeNode.data.endYear = endYear;
rootNode.addNode(decadeNode);
}
}
}

// delete any empty decades
pruneDecadeNodes(rootNode: Manifesto.TreeNode): void {
var pruned: Manifesto.TreeNode[] = [];

for (var i = 0; i < rootNode.nodes.length; i++){
var n = rootNode.nodes[i];
if (!n.nodes.length){
pruned.push(n);
}
}

for (var j = 0; j < pruned.length; j++){
var p = pruned[j];

rootNode.nodes.remove(p);
}
}

sortDecadeNodes(rootNode: Manifesto.TreeNode): void {
rootNode.nodes = rootNode.nodes.sort(function(a, b) {
return a.data.startYear - b.data.startYear;
});
}

getDecadeNode(rootNode: Manifesto.TreeNode, year: number): Manifesto.TreeNode{
for (var i = 0; i < rootNode.nodes.length; i++){
var n = rootNode.nodes[i];
if (year >= n.data.startYear && year <= n.data.endYear) return n;
}

return null;
}

createYearNodes(rootNode: Manifesto.TreeNode, nodes: Manifesto.TreeNode[]): void{
var yearNode: Manifesto.TreeNode;

for (var i = 0; i < nodes.length; i++) {
var node: Manifesto.TreeNode = nodes[i];
var year = this.getNodeYear(node);
var decadeNode = this.getDecadeNode(rootNode, year);

if(decadeNode && !this.getYearNode(decadeNode, year)){
yearNode = manifesto.getTreeNode();
yearNode.label = year.toString();
yearNode.navDate = node.navDate;
yearNode.data.year = year;

decadeNode.addNode(yearNode);
}
}
}

sortYearNodes(rootNode: Manifesto.TreeNode): void {
for (var i = 0; i < rootNode.nodes.length; i++){
var decadeNode = rootNode.nodes[i];

decadeNode.nodes = decadeNode.nodes.sort((a: Manifesto.TreeNode, b: Manifesto.TreeNode) => {
return (this.getNodeYear(a) - this.getNodeYear(b));
});
}
}

getYearNode(decadeNode: Manifesto.TreeNode, year: Number): Manifesto.TreeNode{
for (var i = 0; i < decadeNode.nodes.length; i++){
var n = decadeNode.nodes[i];
if (year === this.getNodeYear(n)) return n;
}

return null;
}

createMonthNodes(rootNode: Manifesto.TreeNode, nodes: Manifesto.TreeNode[]): void{
var monthNode: Manifesto.TreeNode;

for (var i = 0; i < nodes.length; i++) {
var node: Manifesto.TreeNode = nodes[i];
var year = this.getNodeYear(node);
var month = this.getNodeMonth(node);
var decadeNode = this.getDecadeNode(rootNode, year);
var yearNode = this.getYearNode(decadeNode, year);

if (decadeNode && yearNode && !this.getMonthNode(yearNode, month)){
monthNode = manifesto.getTreeNode();
monthNode.label = this.getNodeDisplayMonth(node);
monthNode.navDate = node.navDate;
monthNode.data.year = year;
monthNode.data.month = month;
yearNode.addNode(monthNode);
}
}
}

sortMonthNodes(rootNode: Manifesto.TreeNode): void {
for (var i = 0; i < rootNode.nodes.length; i++){
var decadeNode = rootNode.nodes[i];

for (var j = 0; j < decadeNode.nodes.length; j++){
var monthNode = decadeNode.nodes[j];

monthNode.nodes = monthNode.nodes.sort((a: Manifesto.TreeNode, b: Manifesto.TreeNode) => {
return this.getNodeMonth(a) - this.getNodeMonth(b);
});
}
}
}

getMonthNode(yearNode: Manifesto.TreeNode, month: Number): Manifesto.TreeNode{
for (var i = 0; i < yearNode.nodes.length; i++){
var n = yearNode.nodes[i];
if (month === this.getNodeMonth(n)) return n;
}

return null;
}

createDateNodes(rootNode: Manifesto.TreeNode, nodes: Manifesto.TreeNode[]): void{
for (var i = 0; i < nodes.length; i++) {
var node: Manifesto.TreeNode = nodes[i];
var year = this.getNodeYear(node);
var month = this.getNodeMonth(node);

var dateNode = manifesto.getTreeNode();
dateNode.label = this.getNodeDisplayDate(node);
dateNode.data = node.data;
dateNode.data.type = 'manifest';
dateNode.data.year = year;
dateNode.data.month = month;

var decadeNode = this.getDecadeNode(rootNode, year);

if (decadeNode) {
var yearNode = this.getYearNode(decadeNode, year);

if (yearNode){
var monthNode = this.getMonthNode(yearNode, month);

if (monthNode){
monthNode.addNode(dateNode);
}
}
}
}
}

getNodeYear(node: Manifesto.TreeNode): number{
return node.navDate.getFullYear();
}

getNodeMonth(node: Manifesto.TreeNode): number{
return node.navDate.getMonth();
}

getNodeDisplayMonth(node: Manifesto.TreeNode): string{
var months: string[] = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
return months[node.navDate.getMonth()];
}

getNodeDisplayDate(node: Manifesto.TreeNode): string{
return node.navDate.toDateString();
}
}

export = Provider;
6 changes: 6 additions & 0 deletions src/extensions/uv-seadragon-extension/TreeSortType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum TreeSortType {
date,
none
}

export = TreeSortType;
5 changes: 4 additions & 1 deletion src/extensions/uv-seadragon-extension/l10n/cy-GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@
"content": {
"collapse": "Cuddio'r Cynnwys",
"collapseFull": "Cuddio'r Galeri",
"date": "dyddiad",
"expand": "Ehangu'r Cynnwys",
"expandFull": "Ehangu'r Galeri",
"index": "Mynegai",
"sortBy": "trefnu yn ôl",
"thumbnails": "Bodluniau",
"title": "Cynnwys"
"title": "Cynnwys",
"volume": "cyfaint"
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/extensions/uv-seadragon-extension/l10n/en-GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,14 @@
"content": {
"collapse": "Collapse Contents",
"collapseFull": "Collapse Gallery",
"date": "date",
"expand": "Expand Contents",
"expandFull": "Expand Gallery",
"index": "Index",
"sortBy": "Sort By:",
"thumbnails": "Thumbnails",
"title": "Contents"
"title": "Contents",
"volume": "volume"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/ex.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 1e3abbb

Please sign in to comment.