-
Notifications
You must be signed in to change notification settings - Fork 0
/
LensController.js
executable file
·113 lines (93 loc) · 3.08 KB
/
LensController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
var extend = require('lodash/object/extend');
var $ = require('substance/util/jquery');
var TwoPanelController = require('substance/ui/TwoPanelController');
var CrossrefSearch = require('./packages/bibliography/CrossrefSearch');
var I18n = require('substance/ui/i18n');
I18n.instance.load(require('./i18n/en'));
function LensController() {
LensController.super.apply(this, arguments);
this.handleActions({
'toggleBibItem': this.toggleBibItem,
});
}
LensController.Prototype = function() {
var _super = Object.getPrototypeOf(this);
this.getContentPanel = function() {
return this.refs.contentPanel;
};
// Action used by BibItemComponent when clicked on focus
this.toggleBibItem = function(bibItem) {
if (this.state.bibItemId === bibItem.id) {
this.setState({
contextId: 'bib-items'
});
} else {
this.setState({
contextId: 'bib-items',
bibItemId: bibItem.id
});
}
};
// Some things should go into controller
this.getChildContext = function() {
var childContext = _super.getChildContext.call(this);
return extend(childContext, {
bibSearchEngines: [new CrossrefSearch()],
// Used for turning embed urls to HTML content
embedResolver: function(srcUrl, cb) {
$.get('http://iframe.ly/api/iframely?url='+encodeURIComponent(srcUrl)+'&api_key=712fe98e864c79e054e2da')
// $.get('http://iframely.coko.foundation/iframely?url='+encodeURIComponent(srcUrl)+'')
.success(function(res) {
cb(null, res.html);
})
.error(function(err) {
cb(err);
});
}
});
};
// Action handlers
// ---------------
// Hande Writer state change updates
// --------------
//
// Here we update highlights
this.handleStateUpdate = function(newState) {
var doc = this.getDocument();
function getFigureHighlights(state) {
if (state.citationId) {
var citation = doc.get(state.citationId);
if (citation && ['image-figure', 'table-figure'].indexOf(citation.getItemType()) >= 0) {
return [ state.citationId ].concat(citation.targets);
}
}
return [];
}
function getBibItemHighlights(state) {
if (state.bibItemId) {
// Get citations for a given target
var citations = Object.keys(doc.citationsIndex.get(state.bibItemId));
return citations;
} else if (state.citationId) {
var citation = doc.get(state.citationId);
if (citation && citation.getItemType() === 'bib-item') {
return [ state.citationId ].concat(citation.targets);
}
}
return [];
}
var bibItemHighlights = getBibItemHighlights(newState);
var figureHighlights = getFigureHighlights(newState);
// HACK: updates the highlights when state
// transition has finished
setTimeout(function() {
this.contentHighlights.set({
'bib-item': bibItemHighlights,
'figure': figureHighlights
});
}.bind(this), 0);
};
};
TwoPanelController.extend(LensController);
module.exports = LensController;