| 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
114
115
116
117
118
119
120
121 | 2×
2×
2×
2×
2×
2×
2×
2×
89×
89×
89×
89×
89×
89×
2×
149×
149×
106×
106×
106×
339×
339×
149×
149×
149×
149×
149×
149×
106×
21×
106×
21×
106×
48×
21×
21×
21×
48×
15×
1×
89×
89×
445×
89×
178×
2×
2×
| var LINKS_BEGIN, LINKS_CONTAIN, MEDIA_LINK_EXCLUDES, MediaConfig, _, actions, buildAllowed, htmlparser, makeSimpleStore, ref, store;
_ = require('underscore');
htmlparser = require('htmlparser2');
makeSimpleStore = require('./helpers').makeSimpleStore;
LINKS_BEGIN = ['#'];
LINKS_CONTAIN = ['cnx.org/contents/'];
MEDIA_LINK_EXCLUDES = ['.nav', '.view-reference-guide', '[data-type=footnote-number]', '[data-type=footnote-ref]', '[data-targeted=media]'];
buildAllowed = function(linksBegin, linksContain) {
var beginSelectors, containSelectors;
beginSelectors = _.map(linksBegin, function(linkString) {
return "a[href^='" + linkString + "']";
});
containSelectors = _.map(linksContain, function(linkString) {
return "a[href*='" + linkString + "']";
});
return _.union(beginSelectors, containSelectors);
};
MediaConfig = {
_local: {},
loaded: function(id, mediaDOM) {
this._local[id] = mediaDOM;
return this.emit("loaded." + id, mediaDOM);
},
_parseAndLoad: function(link) {
var id, idDOM, idHTML, mediaDOM;
if (link.attribs.href.search('#') === 0) {
id = link.attribs.href.replace('#', '');
idDOM = htmlparser.DomUtils.getElementById(id, dom);
if (idDOM) {
idHTML = htmlparser.DomUtils.getOuterHTML(idDOM);
mediaDOM = {
name: idDOM.name,
html: idHTML
};
return this.loaded(id, mediaDOM);
}
}
},
_parseHandler: function(actions, error, dom) {
var links;
links = htmlparser.DomUtils.getElementsByTagName('a', dom);
return _.each(links, function(link) {
var id, idDOM, idHTML, mediaDOM;
if (link.attribs.href.search('#') === 0) {
id = link.attribs.href.replace('#', '');
idDOM = htmlparser.DomUtils.getElementById(id, dom);
Eif (idDOM) {
idHTML = htmlparser.DomUtils.getOuterHTML(idDOM);
mediaDOM = {
name: idDOM.name,
html: idHTML
};
return actions.loaded(id, mediaDOM);
}
}
});
},
parse: function(htmlString) {
if (this.parseHandler == null) {
this.parseHandler = new htmlparser.DomHandler(_.partial(this._parseHandler, this));
}
if (this.parser == null) {
this.parser = new htmlparser.Parser(this.parseHandler);
}
return this.parser.parseComplete(htmlString);
},
_get: function(id) {
return this._local[id];
},
reset: function() {
this._local = {};
delete this.parseHandler;
return delete this.parser;
},
exports: {
get: function(id) {
return this._get(id);
},
isLoaded: function(id) {
return this._get(id) != null;
},
getMediaIds: function() {
return _.keys(this._local);
},
getLinksContained: function() {
return LINKS_CONTAIN;
},
getAllowed: function() {
return buildAllowed(LINKS_BEGIN, LINKS_CONTAIN);
},
getExcluded: function() {
return MEDIA_LINK_EXCLUDES;
},
getSelector: function() {
var notMedias;
notMedias = _.reduce(MEDIA_LINK_EXCLUDES, function(current, exclude) {
return current + ":not(" + exclude + ")";
}, '');
return _.map(buildAllowed(LINKS_BEGIN, LINKS_CONTAIN), function(allowed) {
return "" + allowed + notMedias;
}).join(', ');
}
}
};
ref = makeSimpleStore(MediaConfig), actions = ref.actions, store = ref.store;
module.exports = {
MediaActions: actions,
MediaStore: store
};
|