Skip to content

Commit

Permalink
use _data not options.data
Browse files Browse the repository at this point in the history
  • Loading branch information
edsilv committed Oct 12, 2018
1 parent 7267d65 commit 322e37a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
// https://iiif-commons.github.io/iiif-av-component/examples/data/iiif/lunchroom-manners.json

Manifold.loadManifest({
iiifResourceUri: 'http://wellcomelibrary.org/iiif/collection/b19974760',
iiifResourceUri: 'https://iiif-commons.github.io/iiif-av-component/examples/data/iiif/lunchroom-manners.json',
collectionIndex: 0,
manifestIndex: 0,
sequenceIndex: 0,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iiif/iiif-tree-component",
"version": "1.1.11",
"version": "1.1.12",
"description": "",
"main": "./dist/TreeComponent.js",
"types": "./dist/TreeComponent.d.ts",
Expand Down
48 changes: 32 additions & 16 deletions src/TreeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ namespace IIIFComponents {
public options: _Components.IBaseComponentOptions;
private _$tree: JQuery;
private _allNodes: Manifold.ITreeNode[] | null; // cache
private _data: ITreeComponentData = this.data();
private _multiSelectableNodes: Manifold.ITreeNode[] | null; // cache
private _selectedNode: Manifold.ITreeNode;
private _rootNode: Manifold.ITreeNode;
private _selectedNode: Manifold.ITreeNode;

constructor(options: _Components.IBaseComponentOptions) {
super(options);
this._data = this.options.data;
this._init();
}

Expand Down Expand Up @@ -77,7 +79,11 @@ namespace IIIFComponents {
that._setNodeMultiSelected(node, !!!node.multiSelected);

if (node.isRange()) {
that._getMultiSelectState().selectRange(<Manifold.IRange>node.data, node.multiSelected);
const multiSelectState: Manifold.MultiSelectState | null = that._getMultiSelectState();

if (multiSelectState) {
multiSelectState.selectRange(<Manifold.IRange>node.data, node.multiSelected);
}
}

that.fire(TreeComponent.Events.TREE_NODE_MULTISELECTED, node);
Expand All @@ -97,7 +103,7 @@ namespace IIIFComponents {

const node: Manifold.ITreeNode = self.data;

if (node.nodes.length && that.options.data.branchNodesExpandOnClick) {
if (node.nodes.length && that._data.branchNodesExpandOnClick) {
self.toggleExpanded();
}

Expand All @@ -107,7 +113,7 @@ namespace IIIFComponents {
if (!node.nodes.length) {
that.fire(TreeComponent.Events.TREE_NODE_SELECTED, node);
that.selectNode(node);
} else if (that.options.data.branchNodesSelectable) {
} else if (that._data.branchNodesSelectable) {
that.fire(TreeComponent.Events.TREE_NODE_SELECTED, node);
that.selectNode(node);
}
Expand All @@ -125,25 +131,31 @@ namespace IIIFComponents {

public set(data: ITreeComponentData): void {

this.options.data = data;
this._data = Object.assign(this._data, data);

if (!this._data.helper) {
return;
}

this._rootNode = this.options.data.helper.getTree(this.options.data.topRangeIndex, this.options.data.treeSortType);
this._rootNode = this._data.helper.getTree(this._data.topRangeIndex, this._data.treeSortType) as Manifold.ITreeNode;
this._allNodes = null; // delete cache
this._multiSelectableNodes = null; // delete cache
this._$tree.link($.templates.pageTemplate, this._rootNode);

const multiSelectState: Manifold.MultiSelectState = this._getMultiSelectState();
const multiSelectState: Manifold.MultiSelectState | null = this._getMultiSelectState();

for (let i = 0; i < multiSelectState.ranges.length; i++) {
const range: Manifold.IRange = multiSelectState.ranges[i];
const node: Manifold.ITreeNode = this._getMultiSelectableNodes().en().where(n => n.data.id === range.id).first();
if (node){
this._setNodeMultiSelectEnabled(node, (<Manifold.IMultiSelectable>range).multiSelectEnabled);
this._setNodeMultiSelected(node, range.multiSelected);
if (multiSelectState) {
for (let i = 0; i < multiSelectState.ranges.length; i++) {
const range: Manifold.IRange = multiSelectState.ranges[i];
const node: Manifold.ITreeNode = this._getMultiSelectableNodes().en().where(n => n.data.id === range.id).first();
if (node){
this._setNodeMultiSelectEnabled(node, (<Manifold.IMultiSelectable>range).multiSelectEnabled);
this._setNodeMultiSelected(node, range.multiSelected);
}
}
}

if (this.options.data.autoExpand) {
if (this._data.autoExpand) {
const allNodes: Manifesto.ITreeNode[] = this._getAllNodes();

allNodes.forEach((node: Manifold.ITreeNode, index: number) => {
Expand All @@ -154,8 +166,12 @@ namespace IIIFComponents {
}
}

private _getMultiSelectState(): Manifold.MultiSelectState {
return this.options.data.helper.getMultiSelectState();
private _getMultiSelectState(): Manifold.MultiSelectState | null {

if (this._data.helper) {
return this._data.helper.getMultiSelectState();
}
return null;
}

public data(): ITreeComponentData {
Expand Down

0 comments on commit 322e37a

Please sign in to comment.