forked from supermamon/scriptable-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-ui.js
62 lines (54 loc) · 1.71 KB
/
basic-ui.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-brown; icon-glyph: drafting-compass;
function Picker(data, idProperty, captionProperty, header, returnIDOnly) {
this.data = data
this.idProperty = idProperty
this.captionProperty = captionProperty
this.header = header
this.dataOffset = header?1:0
this.hasHeader = !!header
this.returnIDOnly = returnIDOnly?true:false
}
Picker.prototype.show = async function() {
let table = new UITable()
table.showSeparators = true
if (this.hasHeader) {
let headerRow = new UITableRow()
headerRow.isHeader = true;
headerRow.addCell( UITableCell.text(this.header))
table.addRow(headerRow)
}
let selectedItem;
this.data.forEach( item => {
let row = new UITableRow()
row.cellSpacing = 10
row.dismissOnSelect = true
let cell = UITableCell.text(item[this.captionProperty])
cell.leftAligned()
row.addCell(cell)
table.addRow(row)
row.onSelect = (index) => {
var item = this.data[index-this.dataOffset]
selectedItem = this.returnIDOnly ? item[this.idProperty] : item
}
})
await QuickLook.present(table)// table.present()
return selectedItem
}
function Prompt(caption, defaultValue) {
var prompt = new Alert()
prompt.title = caption
prompt.addAction('OK')
prompt.addCancelAction('Cancel')
prompt.addTextField(caption, defaultValue)
this.prompt = prompt
}
Prompt.prototype.show = async function() {
await this.prompt.presentAlert()
return this.prompt.textFieldValue(0)
}
module.exports = {
Picker: Picker,
Prompt: Prompt
}