-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmenuloader.js
268 lines (215 loc) · 10.7 KB
/
menuloader.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
(function () {
var VERSION = 0.4;
var MODULE_PATH = "menuloader";
// This module is inspired by posts from Marc Autret (Indiscripts)
// http://www.indiscripts.com/post/2010/02/how-to-create-your-own-indesign-menus
// http://www.indiscripts.com/post/2011/12/indesign-scripting-forum-roundup-2
// ---
// Note: menus/submenus are application-persistent
var thisModule = Sky.getUtil(MODULE_PATH);
if( thisModule && thisModule.version >= VERSION) {
return;
};
//--------------------------
// Start menuloader class
function moduleClass() {
var menuloader = this;
menuloader.version = VERSION;
menuloader.description = "Load an InDesign menu item.";
menuloader.funWrapper = function( fun, menuName ) {
// Wrap the given function in a try-catch statement and add single undo for user action.
// So we don't halt the whole application on error and user can undo any manu action.
return function () {
try {
// prevent undo - CS5+
app.doScript(fun, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Expand State Abbreviations");
} catch(e) {
alert(menuName + " generated an error:\n" + e.message + " (Line " + e.line + " in file " + e.fileName + ")");
};
};
};
menuloader.getMenu = function( MenuTemplate ) {
var location = app.menus.item( '$ID/Main' );
for (var i = 0; i < MenuTemplate.path.length; i++) {
location = location.submenus.item( MenuTemplate.path[i] );
};
if(!location.isValid){
return new Error( "InDesign main menu $ID/Main does not resolve into object.");
};
return location.menuElements.itemByName( MenuTemplate.menuName );
};
menuloader.unloadElement = function( Element ) {
if(!Element.isValid){
return new Error( "Element does not resolve to object." );
};
try {
if(Element.hasOwnProperty('menuElements')) {
// Clean subitems recursively
var subCount = Element.menuElements.count();
for (var i = subCount - 1; i >= 0; i--) {
var subElement = Element.menuElements[i];
if( subElement.menuElements.count() > 0 ) {
menuloader.unloadElement( subElement );
};
};
};
} catch(e) { };
Element.remove();
};
menuloader.load = function( MenuTemplate, alertUser ) {
// Make sure old menu is removed before building a new one
menuloader.unload( MenuTemplate, alertUser );
// Enable ExtendScript localisation engine
$.localize = true;
var AT_END = LocationOptions.atEnd;
var alertUser = (typeof alertUser === 'boolean')? alertUser : true;
try{
var MainMenu = app.menus.item( '$ID/Main' );
var menuInstaller = menuInstaller || ( function() {
var location = MainMenu;
for (var i = 0; i < MenuTemplate.path.length; i++) {
location = location.submenus.item( MenuTemplate.path[i] );
};
var refItem = location.submenus.lastItem();
if( MenuTemplate.ref ) {
refItem = location.menuItems.item( MenuTemplate.ref );
};
var loc = LocationOptions.after;
if( MenuTemplate.loc ) {
loc = MenuTemplate.loc;
};
if(location === MainMenu) {
location = location.submenus.add( MenuTemplate.menuName, LocationOptions.before, location.submenus.lastItem() );
};
// Load Menu
if(MenuTemplate.sub.length === 0) {
// There is no sub menu, load default action
var fun = menuloader.funWrapper( MenuTemplate.fun, MenuTemplate.menuName );
MenuTemplate.action = app.scriptMenuActions.add( MenuTemplate.menuName );
MenuTemplate.action.addEventListener('onInvoke', fun);
location.menuItems.add( MenuTemplate.action, loc, refItem );
} else { // Load with Sub Menu!
location = location.submenus.add( MenuTemplate.menuName, loc, refItem );
// (Re)set the menu actions
// ---
var subItem, i = MenuTemplate.sub.length;
while( i-- ) {
subItem = MenuTemplate.sub[i];
if( subItem.separator ) continue;
if( typeof subItem.fun === 'function') {
// Create the corresponding action
// ---
var fun = menuloader.funWrapper( subItem.fun, MenuTemplate.menuName + " - " + subItem.caption );
subItem.action = app.scriptMenuActions.add( subItem.caption );
subItem.action.addEventListener('onInvoke', fun);
};
};
// Build Sub Menu
// ---
// Fill menu with respect to MenuTemplate order
// (Possible submenus are specified in .subName and created on the fly)
// ---
var s, n = MenuTemplate.sub.length, subs = {}, sub = null;
for( i=0 ; i < n ; ++i ) {
subItem = MenuTemplate.sub[i];
// Target the desired submenu
// ---
sub = (s=subItem.subName) ? ( subs[s] || (subs[s]=location.submenus.add( s, AT_END )) ) : location;
// Connect the related action OR create a separator
// ---
if( subItem.separator ) {
sub.menuSeparators.add( AT_END );
} else {
sub.menuItems.add( subItem.action, AT_END );
};
};
// End Build Sub Menu
}; // End Load Menu
return true;
})(); // invoke!!
} catch ( err ) {
if(alertUser) alert("Unable to load menu " + "\n" + err.message + " (Line " + err.line + " in file " + err.fileName + ")")
return err;
};
};
menuloader.unload = function( MenuTemplate, alertUser ) {
// Enable ExtendScript localisation engine
$.localize = true;
var alertUser = (typeof alertUser === 'boolean')? alertUser : true;
var MainMenu = app.menus.item( '$ID/Main' );
if(!MainMenu.isValid){
return new Error("InDesign main menu $ID/Main does not resolve into object.");
};
var Submenu = MainMenu;
try{
for (var i = 0; i < MenuTemplate.path.length; i++) {
Submenu = Submenu.submenus.item( MenuTemplate.path[i] );
};
} catch ( err ) {
if(alertUser) alert("Unable to unload menu " + String(MenuTemplate.menuName) + "\n" + err.message + " (Line " + err.line + " in file " + err.fileName + ")");
return err;
};
if(Submenu.isValid) {
menuloader.unloadElement(
Submenu.menuElements.itemByName(MenuTemplate.menuName)
);
} else {
if(alertUser) alert("Unable to unload menu " + String(MenuTemplate.menuName) + "\n" + err.message + " (Line " + err.line + " in file " + err.fileName + ")");
return new Error("Menu location does not seem to resolve to a valid menu option.");
};
};
menuloader.template = function( menuName, Options ) {
if (!(this instanceof menuloader.template)) {
throw new Error("menuTemplate should be created using new operator.");
};
var Menu = this;
var Options = (typeof Options === 'object') ? Options : {};
Menu.menuName = String( menuName );
// Array, Empty === Main
Menu.path = (Options.hasOwnProperty('path')) ? [].concat(Options.path) : [];
Menu.sub = (Options.hasOwnProperty('sub' )) ? [].concat(Options.sub) : [];
Menu.ref = (Options.hasOwnProperty('ref' )) ? Options.ref : undefined;
Menu.loc = (Options.hasOwnProperty('loc' )) ? Options.loc : undefined;
Menu.fun = (Options.hasOwnProperty('fun' )) ? Options.fun : undefined;
// Menu Tools
//- - - - - -
Menu.addElement = function( elementTemplate ) {
Menu.sub.push( elementTemplate );
return Menu;
};
// Element Templates
//- - - - - - - - - -
Menu.createItem = function( caption, fun, subName ) {
// subName is optional
var subName = (typeof subName === 'string')? subName : "";
return { caption: String(caption), fun: fun, subName: String(subName) };
};
Menu.createSeparator = function( subName ) {
// subName is optional
var subName = (typeof subName === 'string')? subName : "";
return { separator: true, subName: subName };
};
Menu.load = function( alertUser ) {
menuloader.load( Menu, alertUser );
return Menu;
};
Menu.unload = function( alertUser ) {
menuloader.unload( Menu, alertUser );
return Menu;
};
Menu.getLoaded = function(){
return menuloader.getMenu(Menu);
};
Menu.isLoaded = function(){
var loadedMenu = menuloader.getMenu(Menu);
if( loadedMenu.isValid ) {
return true;
};
return false;
};
};
};
//--------------------------
// End menuloader class
Sky.setUtil(MODULE_PATH, new moduleClass() );
})();