-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwoqlChooser.js
167 lines (148 loc) · 4.57 KB
/
woqlChooser.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
/* eslint-disable consistent-return */
/* eslint-disable no-plusplus */
/* eslint-disable camelcase */
/* eslint-disable no-undef */
const WOQLChooserConfig = require('./chooserConfig');
const UTILS = require('../utils');
const { WOQLRule } = require('./woqlRule');
/**
* Very simple implementation of a WOQL backed chooser
* Makes a drop down from a WOQL query - configuration tells it which columns to use...
* @param {WOQLClient} client
* @param {WOQLChooserConfig} config
* @returns {WOQLChooser}
*/
function WOQLChooser(client, config) {
this.client = client;
this.config = (config || new WOQLChooserConfig());
this.selected = false;
this.cursor = 0;
return this;
}
WOQLChooser.prototype.options = function (config) {
this.config = config;
return this;
};
WOQLChooser.prototype.set = function (id) {
if (this.selected !== id) {
this.selected = id;
const ch = this.config.change;
if (ch) ch(id);
}
};
/*
* Sets up the required variables from the result / config
*/
WOQLChooser.prototype.setResult = function (result) {
this.result = result;
this.choices = [];
let rows = 0;
const variables = result.getVariableList();
if (!this.config.values() && variables.length) {
this.config.values(variables[0]);
}
// sort it
if (this.config.sort()) {
this.result.sort(this.config.sort(), this.config.direction());
}
// eslint-disable-next-line no-cond-assign
while (row = this.result.next()) {
if (row && this.includeRow(row, this.result.cursor)) {
this.choices.push(this.rowToChoice(row, rows++));
}
}
return this;
};
WOQLChooser.prototype.includeRow = function (row, index) {
const matched_rules = new WOQLRule().matchRow(this.config.rules, row, index, 'hidden');
for (let i = 0; i < matched_rules.length; i++) {
if (matched_rules[i].rule.hidden) return false;
}
return true;
};
WOQLChooser.prototype.rowToChoice = function (row, rownum) {
const choice = {
id: this.getRowID(row),
};
choice.label = this.getLabelFromBinding(row, rownum);
choice.title = this.getTitleFromBinding(row, rownum);
choice.selected = this.getSelectedFromBinding(row, rownum);
return choice;
};
WOQLChooser.prototype.getRowID = function (row) {
const rval = row[this.config.values()];
if (rval['@value']) return rval['@value'];
return rval;
};
WOQLChooser.prototype.getLabelFromBinding = function (row, rownum) {
const sp = this.getSpecialRenderer(row, rownum, 'label');
if (sp) return this.renderSpecial(sp, row, rownum);
if (this.config.labels()) {
if (row[this.config.labels()]) {
let lab = row[this.config.labels()];
if (lab['@value']) lab = lab['@value'];
if (lab !== 'system:unknown') return lab;
}
}
return UTILS.labelFromURL(this.getRowID(row));
};
WOQLChooser.prototype.getTitleFromBinding = function (row, rownum) {
const sp = this.getSpecialRenderer(row, rownum, 'title');
if (sp) return this.renderSpecial(sp, row, rownum);
if (this.config.titles()) {
if (row[this.config.titles()]) {
let lab = row[this.config.titles()];
if (lab['@value']) lab = lab['@value'];
if (lab !== 'system:unknown') return lab;
}
}
return false;
};
WOQLChooser.prototype.getSelectedFromBinding = function (row, rownum) {
const matched_rules = new WOQLRule().matchRow(this.config.rules, row, rownum, 'selected');
if (matched_rules && matched_rules.length) {
return matched_rules[matched_rules.length - 1].rule.selected;
}
return false;
};
WOQLChooser.prototype.render = function () {
if (this.renderer) return this.renderer.render(this);
};
WOQLChooser.prototype.setRenderer = function (rend) {
this.renderer = rend;
return this;
};
WOQLChooser.prototype.getSpecialRenderer = function (row, index, type) {
const matched_rules = new WOQLRule().matchRow(this.config.rules, row, index, type);
for (let i = 0; i < matched_rules.length; i++) {
if (matched_rules[i].rule[type]) return matched_rules[i].rule[type];
}
return false;
};
WOQLChooser.prototype.renderSpecial = function (rule, row) {
if (rule && typeof rule === 'function') {
return rule(row);
}
if (rule && typeof rule === 'string') {
return rule;
}
};
WOQLChooser.prototype.count = function () {
return this.result.count();
};
WOQLChooser.prototype.first = function () {
this.cursor = 0;
return this.choices[this.cursor];
};
WOQLChooser.prototype.next = function () {
const res = this.choices[this.cursor];
this.cursor++;
return res;
};
WOQLChooser.prototype.prev = function () {
if (this.cursor > 0) {
this.cursor--;
return this.choices[this.cursor];
}
};
module.exports = WOQLChooser;