Skip to content

Commit 2b0b939

Browse files
committed
jsdialog: apply prettier rules to Util.Dropdown.ts
Signed-off-by: Szymon Kłos <[email protected]> Change-Id: Ie0d539bd756d006d0c565c25d2ba38216f422005
1 parent bdcd2c4 commit 2b0b939

File tree

3 files changed

+95
-49
lines changed

3 files changed

+95
-49
lines changed

browser/.beforeprettier

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
/src/control/Control.UserList.js
6262
/src/control/Control.Zotero.js
6363
/src/control/IFrameDialog.js
64-
/src/control/jsdialog/Util.Dropdown.js
6564
/src/control/jsdialog/Util.FocusCycle.js
6665
/src/control/jsdialog/Util.ModalHelper.js
6766
/src/control/jsdialog/Util.StateChange.ts

browser/src/control/jsdialog/Definitions.Types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,14 @@ type CustomEntryRenderCallback = (pos: number | string) => void;
181181

182182
// used to define menus
183183
interface MenuDefinition extends WidgetJSON {
184-
type: 'action' | 'colorpicker' | 'menu' | 'separator' | 'html' | 'json' | 'comboboxentry'; // type of entry
184+
type:
185+
| 'action'
186+
| 'colorpicker'
187+
| 'menu'
188+
| 'separator'
189+
| 'html'
190+
| 'json'
191+
| 'comboboxentry'; // type of entry
185192
text?: string; // displayed text
186193
hint?: string; // hint text
187194
uno?: string; // uno command

browser/src/control/jsdialog/Util.Dropdown.ts

Lines changed: 87 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ function _createDropdownId(id: string) {
1919
return id + '-dropdown';
2020
}
2121

22-
JSDialog.CreateDropdownEntriesId = function(id: string) {
22+
JSDialog.CreateDropdownEntriesId = function (id: string) {
2323
return id + '-entries';
24-
}
24+
};
2525

26-
JSDialog.OpenDropdown = function (id: string,
27-
popupParent: string | ( HTMLElement & { _onDropDown: (open: boolean) => void } ),
28-
entries: Array<ComboBoxEntry>,
29-
innerCallback: JSDialogMenuCallback,
30-
popupAnchor: string,
31-
isSubmenu: boolean) {
26+
JSDialog.OpenDropdown = function (
27+
id: string,
28+
popupParent:
29+
| string
30+
| (HTMLElement & { _onDropDown: (open: boolean) => void }),
31+
entries: Array<ComboBoxEntry>,
32+
innerCallback: JSDialogMenuCallback,
33+
popupAnchor: string,
34+
isSubmenu: boolean,
35+
) {
3236
const json = {
3337
id: _createDropdownId(id),
3438
type: 'dropdown',
@@ -45,30 +49,40 @@ JSDialog.OpenDropdown = function (id: string,
4549
allyRole: 'listbox',
4650
cols: 1,
4751
rows: entries.length,
48-
children: [] as Array<WidgetJSON>
49-
}
50-
]
52+
children: [] as Array<WidgetJSON>,
53+
},
54+
],
5155
};
5256

53-
if (popupParent && typeof popupParent !== 'string' && typeof popupParent._onDropDown === 'function') {
57+
if (
58+
popupParent &&
59+
typeof popupParent !== 'string' &&
60+
typeof popupParent._onDropDown === 'function'
61+
) {
5462
popupParent._onDropDown(true);
5563
}
5664

5765
const isChecked = function (unoCommand: string) {
5866
const items = window.L.Map.THIS['stateChangeHandler'];
5967
const val = items.getItemValue(unoCommand);
6068

61-
if (val && (val === true || val === 'true'))
62-
return true;
63-
else
64-
return false;
69+
if (val && (val === true || val === 'true')) return true;
70+
else return false;
6571
};
6672

6773
for (const i in entries) {
68-
const checkedValue = (entries[i].checked === undefined)
69-
? undefined : (entries[i].uno && isChecked('.uno' + entries[i].uno));
74+
const checkedValue =
75+
entries[i].checked === undefined
76+
? undefined
77+
: entries[i].uno && isChecked('.uno' + entries[i].uno);
7078

71-
let entry: WidgetJSON | SeparatorWidgetJSON | HtmlContentJson | MenuDefinition | ComboBoxEntry | null = null;
79+
let entry:
80+
| WidgetJSON
81+
| SeparatorWidgetJSON
82+
| HtmlContentJson
83+
| MenuDefinition
84+
| ComboBoxEntry
85+
| null = null;
7286

7387
switch (entries[i].type) {
7488
// DEPRECACTED: legacy plain HTML adapter
@@ -77,32 +91,37 @@ JSDialog.OpenDropdown = function (id: string,
7791
id: id + '-entry-' + i,
7892
type: 'htmlcontent',
7993
htmlId: entries[i].htmlId,
80-
closeCallback: function () { JSDialog.CloseDropdown(id); }
94+
closeCallback: function () {
95+
JSDialog.CloseDropdown(id);
96+
},
8197
} as HtmlContentJson;
8298
json.gridKeyboardNavigation = true;
83-
break;
99+
break;
84100

85101
// dropdown is a colorpicker
86102
case 'colorpicker':
87103
entry = entries[i];
88104
// for color picker we have a "KeyboardGridNavigation" function defined separately to handle custom cases
89105
json.gridKeyboardNavigation = true;
90-
break;
106+
break;
91107

92108
// allows to put regular JSDialog JSON into popup
93109
case 'json':
94-
entry = typeof entries[i].content !== 'undefined' ? entries[i].content as WidgetJSON : null;
110+
entry =
111+
typeof entries[i].content !== 'undefined'
112+
? (entries[i].content as WidgetJSON)
113+
: null;
95114
if (entry?.type === 'grid') json.gridKeyboardNavigation = true;
96-
break;
115+
break;
97116

98117
// horizontal separator in menu
99118
case 'separator':
100119
entry = {
101120
id: id + '-entry-' + i,
102121
type: 'separator',
103-
orientation: 'horizontal'
122+
orientation: 'horizontal',
104123
} as SeparatorWidgetJSON;
105-
break;
124+
break;
106125

107126
// menu and submenu entry
108127
case 'action':
@@ -120,23 +139,31 @@ JSDialog.OpenDropdown = function (id: string,
120139
icon: entries[i].img,
121140
checked: entries[i].checked || checkedValue,
122141
selected: entries[i].selected,
123-
hasSubMenu: !!entries[i].items
142+
hasSubMenu: !!entries[i].items,
124143
} as ComboBoxEntry;
125-
break;
144+
break;
126145
}
127146

128147
if (entry) json.children[0].children.push(entry);
129148
}
130149

131-
const generateCallback = function (targetEntries: Array<MenuDefinition>): JSDialogCallback {
150+
const generateCallback = function (
151+
targetEntries: Array<MenuDefinition>,
152+
): JSDialogCallback {
132153
let lastSubMenuOpened: string | null = null;
133154
const closeLastSubMenu = () => {
134155
if (!lastSubMenuOpened) return;
135156
JSDialog.CloseDropdown(lastSubMenuOpened);
136157
lastSubMenuOpened = null;
137158
};
138159

139-
return function(objectType: string, eventType: string, object: any, data: any, builder: JSBuilder) {
160+
return function (
161+
objectType: string,
162+
eventType: string,
163+
object: any,
164+
data: any,
165+
builder: JSBuilder,
166+
) {
140167
let pos = -1;
141168
if (typeof data === 'number') pos = data;
142169
else pos = data ? parseInt(data.substr(0, data.indexOf(';'))) : -1;
@@ -151,9 +178,16 @@ JSDialog.OpenDropdown = function (id: string,
151178
const dropdown = JSDialog.GetDropdown(object.id);
152179
const allEntries = dropdown.querySelectorAll('.ui-grid-cell');
153180
const index = pos + 1;
154-
const targetEntry = allEntries && allEntries.length > index ? allEntries[index] : null;
155-
JSDialog.OpenDropdown(subMenuId, targetEntry, entry.items,
156-
generateCallback(entry.items), 'top-end', true);
181+
const targetEntry =
182+
allEntries && allEntries.length > index ? allEntries[index] : null;
183+
JSDialog.OpenDropdown(
184+
subMenuId,
185+
targetEntry,
186+
entry.items,
187+
generateCallback(entry.items),
188+
'top-end',
189+
true,
190+
);
157191
lastSubMenuOpened = subMenuId;
158192

159193
app.layoutingService.appendLayoutingTask(() => {
@@ -165,13 +199,13 @@ JSDialog.OpenDropdown = function (id: string,
165199
const container = dropdown.querySelector('.ui-grid');
166200
JSDialog.MakeFocusCycle(container);
167201
const focusables = JSDialog.GetFocusableElements(container);
168-
if (focusables && focusables.length)
169-
focusables[0].focus();
202+
if (focusables && focusables.length) focusables[0].focus();
170203
});
171204

172205
return;
173206
} else if (eventType === 'selected' && entry && entry.uno) {
174-
const uno = (entry.uno.indexOf('.uno:') === 0) ? entry.uno : '.uno:' + entry.uno;
207+
const uno =
208+
entry.uno.indexOf('.uno:') === 0 ? entry.uno : '.uno:' + entry.uno;
175209
window.L.Map.THIS.sendUnoCommand(uno);
176210
JSDialog.CloseDropdown(id);
177211
return;
@@ -184,25 +218,31 @@ JSDialog.OpenDropdown = function (id: string,
184218

185219
// for multi-level menus last parameter should be used to handle event (it contains selected entry)
186220
// usually last param is builder see: JSDialogCallback
187-
if (innerCallback && innerCallback(objectType, eventType, object, data, entry || builder))
221+
if (
222+
innerCallback &&
223+
innerCallback(objectType, eventType, object, data, entry || builder)
224+
)
188225
return;
189226

190-
if (eventType === 'selected')
191-
JSDialog.CloseDropdown(id);
192-
else
193-
console.debug('Dropdown: unhandled action: "' + eventType + '"');
227+
if (eventType === 'selected') JSDialog.CloseDropdown(id);
228+
else console.debug('Dropdown: unhandled action: "' + eventType + '"');
194229
};
195230
};
196231
window.L.Map.THIS.fire('closepopups'); // close popups if a dropdown menu is opened
197-
window.L.Map.THIS.fire('jsdialog', {data: json, callback: generateCallback(entries)});
232+
window.L.Map.THIS.fire('jsdialog', {
233+
data: json,
234+
callback: generateCallback(entries),
235+
});
198236
};
199237

200238
JSDialog.CloseDropdown = function (id: string) {
201-
window.L.Map.THIS.fire('jsdialog', {data: {
202-
id: _createDropdownId(id),
203-
jsontype: 'dialog',
204-
action: 'close'
205-
}});
239+
window.L.Map.THIS.fire('jsdialog', {
240+
data: {
241+
id: _createDropdownId(id),
242+
jsontype: 'dialog',
243+
action: 'close',
244+
},
245+
});
206246
};
207247

208248
JSDialog.CloseAllDropdowns = function () {

0 commit comments

Comments
 (0)