-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwsAccess.js
210 lines (194 loc) · 9.67 KB
/
wsAccess.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
function applicationStarted(pluginWorkspaceAccess) {
Packages.java.lang.System.err.println("Application started " + pluginWorkspaceAccess);
// Read the properties file that stores mappings from DITA class particle (e.g. "task/task") to icon file path
var inputStream = null;
var properties = null;
try {
inputStream = new Packages.java.io.FileInputStream(new Packages.java.io.File(new Packages.java.net.URI(jsDirURL + "/icons.properties")));
properties = new Packages.java.util.Properties();
properties.load(inputStream);
}
catch (err) {
err.printStackTrace();
}
finally {
if (inputStream) {
try {
inputStream.close();
}
catch (err) {
err.printStackTrace();
}
}
}
// Editor change listener. Triggered when a DITA Map is opened in the DITA Maps Manager.
edChangedListener = {
editorOpened: function (editorLocation) {
// Get the opened DITA Map
var editor = pluginWorkspaceAccess.getEditorAccess(editorLocation, Packages.ro.sync.exml.workspace.api.PluginWorkspace.DITA_MAPS_EDITING_AREA);
var ditaMapPage = editor.getCurrentPage();
// DITA map node renderer customizer
var customizer = {
// Get rendering information for the topicrefs in the DITA Maps Manager
getRenderingInformation: function (context) {
var renderingInfo = null;
var topicRefTargetInfo = context.getTopicRefTargetInfo();
if (topicRefTargetInfo && properties) {
// Get the "class" attribute's value for the current referred topic
clazz = topicRefTargetInfo.getProperty(Packages.ro.sync.exml.workspace.api.standalone.ditamap.TopicRefTargetInfo.CLASS_VALUE);
if (clazz) {
clazz = clazz.trim();
var iconPath = null;
var iterator = properties.keySet().iterator();
while (iterator.hasNext()) {
var key = iterator.next();
if (clazz.indexOf(key) !== -1) {
// Keep the first icon
iconPath = properties. get (key);
break;
}
}
// Set the custom icon
try {
if (iconPath) {
var iconURI = new Packages.java.net.URI(jsDirURL + "/" + iconPath);
var iconFile = new Packages.java.io.File(iconURI);
if (iconFile.exists()) {
renderingInfo = new Packages.ro.sync.exml.workspace.api.node.customizer.BasicRenderingInformation();
renderingInfo.setIconPath(iconURI);
}
}
}
catch (err) {
err.printStackTrace();
}
}
}
return renderingInfo;
},
customizeRenderedTopicrefTitle: function(topicref, defaultComputedTitle){
return customizeRenderedTopicrefTitleCommon(topicref, defaultComputedTitle);
},
// Customize the topicref titles
customizeComputedTopicrefTitle: function (topicref, targetTopicOrMap, defaultComputedTitle) {
var newComputedTitle = defaultComputedTitle;
var currentTopicref = topicref;
// Present profiling attributes set on the root element.
if (targetTopicOrMap.getType() == Packages.ro.sync.ecss.extensions.api.node.AuthorNode.NODE_TYPE_DOCUMENT) {
var rootElement = targetTopicOrMap.getRootElement();
if (rootElement.getType() == Packages.ro.sync.ecss.extensions.api.node.AuthorNode.NODE_TYPE_ELEMENT) {
var attrsCount = rootElement.getAttributesCount();
for (var i = 0; i < attrsCount; i++) {
var attrName = rootElement.getAttributeAtIndex(i);
if (attrName.equals("rev") || attrName.equals("audience") || attrName.equals("platform") || attrName.equals("product") || attrName.equals("props")) {
// Interesting attribute...
newComputedTitle = newComputedTitle + " [" + attrName + "='" + rootElement.getAttribute(attrName) + "']";
}
}
}
}
// Maybe it's a resource-only topic
if (currentTopicref.getAttribute("processing-role") != null) {
var processingRoleValue = currentTopicref.getAttribute("processing-role").getValue();
if (processingRoleValue.equals('resource-only')) {
newComputedTitle = newComputedTitle + " [resource-only]";
}
}
/* In Oxygen version 21 and newer we have a new API method called "customizeRenderedTopicrefTitle" which
* is better when computing titles which depend on the position in the DITA Map of a particular topicref
* For older Oxygen versions we can compute the counter value here. */
var version = pluginWorkspaceAccess.getVersion();
var indexOfDot = version.indexOf(".");
if(indexOfDot != -1){
version = version.substr(0, indexOfDot);
}
if(Number(version) < 21){
newComputedTitle = customizeRenderedTopicrefTitleCommon(topicref, defaultComputedTitle);
}
return newComputedTitle;
}
}
// Add customizer
ditaMapPage.addNodeRendererCustomizer(new Packages.ro.sync.exml.workspace.api.editor.page.ditamap.DITAMapNodeRendererCustomizer(customizer));
}
}
// Add editor change listener
edChangedListener = new JavaAdapter(Packages.ro.sync.exml.workspace.api.listeners.WSEditorChangeListener, edChangedListener);
pluginWorkspaceAccess.addEditorChangeListener(edChangedListener, Packages.ro.sync.exml.workspace.api.PluginWorkspace.DITA_MAPS_EDITING_AREA);
}
function applicationClosing(pluginWorkspaceAccess) {
Packages.java.lang.System.err.println("Application closing " + pluginWorkspaceAccess);
}
function countChapters(topicref) {
var cnt = 0;
if (topicref != null) {
if (typeof topicref.getName != "undefined" && "chapter".equals(topicref.getName())) {
var parentOfTopicRef = topicref.getParent();
cnt = 1;
var nodes = parentOfTopicRef.getContentNodes();
for (var i = 0; i < nodes.size(); i++) {
if (nodes. get (i) == topicref) {
break;
} else if (typeof nodes. get (i).getName != "undefined" && "chapter".equals(nodes. get (i).getName())) {
cnt = cnt + 1;
}
}
}
}
return cnt;
}
function countSiblings(topicref) {
var cnt = 0;
if (topicref != null) {
var parentOfTopicRef = topicref.getParent();
cnt = 1;
var nodes = parentOfTopicRef.getContentNodes();
for (var i = 0; i < nodes.size(); i++) {
if (nodes. get (i) == topicref) {
break;
} else {
cnt = cnt + 1;
}
}
}
return cnt;
}
function getAncestorChapter(topicref) {
var cnt = 0;
if (topicref != null) {
if (typeof topicref.getName != "undefined" && "chapter".equals(topicref.getName())) {
return topicref;
} else {
return getAncestorChapter(topicref.getParent());
}
}
return cnt;
}
function customizeRenderedTopicrefTitleCommon(topicref, defaultComputedTitle){
var newComputedTitle = defaultComputedTitle;
var currentTopicref = topicref;
// Count chapters and present counter before chapter name.
var chapterCnt = countChapters(currentTopicref);
if (chapterCnt > 0) {
newComputedTitle = chapterCnt + " - " + newComputedTitle;
} else {
//Maybe this is the child topicref of a chapter, count this as well
var ancestorChapter = getAncestorChapter(currentTopicref);
if (ancestorChapter != null) {
chapterCnt = countChapters(ancestorChapter);
if (chapterCnt > 0) {
var cntAccumulator = "";
var current = currentTopicref;
while (current != null && current != ancestorChapter) {
var subChapterCnt = countSiblings(current);
if (subChapterCnt > 0) {
cntAccumulator = subChapterCnt + "." + cntAccumulator;
}
current = current.getParent();
}
newComputedTitle = chapterCnt + "." + cntAccumulator + " - " + newComputedTitle;
}
}
}
return newComputedTitle;
}