@nys-cui/cui-formpill 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # cui-formpill
2
+
3
+
4
+
5
+ ## Getting started
6
+
7
+ To make it easy for you to get started with GitLab, here's a list of recommended next steps.
8
+
9
+ Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
10
+
11
+ ## Add your files
12
+
13
+ - [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
14
+ - [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
15
+
16
+ ```
17
+ cd existing_repo
18
+ git remote add origin https://nyssc.svc.ny.gov/DTF/ui/cui2-components/cui-formpill.git
19
+ git branch -M main
20
+ git push -uf origin main
21
+ ```
22
+
23
+ ## Integrate with your tools
24
+
25
+ - [ ] [Set up project integrations](https://nyssc.svc.ny.gov/DTF/ui/cui2-components/cui-formpill/-/settings/integrations)
26
+
27
+ ## Collaborate with your team
28
+
29
+ - [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
30
+ - [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
31
+ - [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
32
+ - [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
33
+ - [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
34
+
35
+ ## Test and Deploy
36
+
37
+ Use the built-in continuous integration in GitLab.
38
+
39
+ - [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
40
+ - [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
41
+ - [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
42
+ - [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
43
+ - [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
44
+
45
+ ***
46
+
47
+ # Editing this README
48
+
49
+ When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
50
+
51
+ ## Suggestions for a good README
52
+
53
+ Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
54
+
55
+ ## Name
56
+ Choose a self-explaining name for your project.
57
+
58
+ ## Description
59
+ Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
60
+
61
+ ## Badges
62
+ On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
63
+
64
+ ## Visuals
65
+ Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
66
+
67
+ ## Installation
68
+ Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
69
+
70
+ ## Usage
71
+ Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
72
+
73
+ ## Support
74
+ Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
75
+
76
+ ## Roadmap
77
+ If you have ideas for releases in the future, it is a good idea to list them in the README.
78
+
79
+ ## Contributing
80
+ State if you are open to contributions and what your requirements are for accepting them.
81
+
82
+ For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
83
+
84
+ You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
85
+
86
+ ## Authors and acknowledgment
87
+ Show your appreciation to those who have contributed to the project.
88
+
89
+ ## License
90
+ For open source projects, say how it is licensed.
91
+
92
+ ## Project status
93
+ If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
package/cui.jsonc ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "CUI2 FORMPILL",
3
+ "mode": "component",
4
+ "src": "src",
5
+ "entry": "cui-formpill.js"
6
+ }
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@nys-cui/cui-formpill",
3
+ "description": "A Formpill/Filtering component for use in Core UI 2",
4
+ "author": {
5
+ "name": "New York State Department of Tax and Finance"
6
+ },
7
+ "main": "./dist/js/cui-formpill.js",
8
+ "type": "module",
9
+ "version": "0.1.0",
10
+ "scripts": {
11
+ "clean": "rm -rf ./dist",
12
+ "build": "npm run clean && cui --dep",
13
+ "postinstall": "node -e \"console.log('Post-Installing cui-help: %s', Date())\" && npm run build"
14
+ },
15
+ "dependencies": {
16
+ "@nys-cui/cui-popover": "^0.1.0"
17
+ }
18
+ }
@@ -0,0 +1,15 @@
1
+ import CUI_POPOVER from '@nys-cui/cui-popover';
2
+
3
+ export default class CUI_ADDFILTER_POPOVER extends CUI_POPOVER {
4
+
5
+ constructor() {
6
+ super();
7
+ }
8
+
9
+
10
+
11
+ connectedCallback() {
12
+ super.connectedCallback();
13
+ }
14
+
15
+ }
@@ -0,0 +1,1080 @@
1
+ import POPOVER_FORMPILL from './cui-formpill-popover.js';
2
+
3
+ class CUI_FORMPILL_PILL extends HTMLElement {
4
+
5
+ constructor() {
6
+ super();
7
+
8
+ this.attachShadow({ mode: 'open' });
9
+
10
+ this._state = {};
11
+
12
+ this.oPropsToStateNames = {
13
+ "text": "sText",
14
+ "value1": "sValue1",
15
+ "value2": "sValue2",
16
+ "value3": "sValue3",
17
+ "values": "asValues"
18
+ }
19
+
20
+ this._state.sPosition = 'bottom-center'
21
+
22
+ }
23
+
24
+ get state() {
25
+
26
+ return this._state;
27
+ }
28
+
29
+
30
+ get contents() {
31
+ if (this._state.sValue3) {
32
+ return {
33
+ 'fieldName': this._state.sValue1,
34
+ 'operator': this._state.sValue2,
35
+ 'value': this._state.sValue3
36
+ }
37
+ } else if (this._state.asValues) {
38
+ return {
39
+ 'fieldName': this._state.sValue1,
40
+ 'value': this._state.asValues.split(',')
41
+ }
42
+ } else {
43
+ return {
44
+ 'fieldName': this._state.sValue1,
45
+ 'value': this._state.sValue2
46
+ }
47
+ }
48
+ }
49
+
50
+ setState(props) {
51
+ // Need to map props to state variable names if we want to keep them prefixed.
52
+ this._state = Object.assign(this._state, this.normalizeProps(props));
53
+
54
+ this.render();
55
+ }
56
+
57
+ clearSlottedElements(slotName) {
58
+ let aSlottedElements = this.querySelectorAll("[slot = " + slotName + "]");
59
+ if (aSlottedElements && aSlottedElements.length > 0) {
60
+ for (let i = 0; i < aSlottedElements.length; i++) {
61
+ let dElement = aSlottedElements[i];
62
+ this.removeChild(dElement);
63
+ }
64
+ }
65
+ }
66
+
67
+ normalizeProps(props) {
68
+ let updatedProps = {};
69
+
70
+ for (const [key, value] of Object.entries(props)) {
71
+ if (this.oPropsToStateNames[key]) {
72
+ updatedProps[this.oPropsToStateNames[key]] = value;
73
+ }
74
+ }
75
+
76
+ return updatedProps;
77
+ }
78
+
79
+ get template() {
80
+
81
+ let sTemplate = `<slot name="filter"></slot> `;
82
+
83
+ return sTemplate;
84
+
85
+ }
86
+ connectedCallback() {
87
+ this._state.sText = (this.getAttribute('text')) ? this.getAttribute('text') : "";
88
+ this._state.sValue1 = (this.getAttribute('value1')) ? this.getAttribute('value1') : null;
89
+ this._state.sValue2 = (this.getAttribute('value2')) ? this.getAttribute('value2') : null;
90
+ this._state.sValue3 = (this.getAttribute('value3')) ? this.getAttribute('value3') : null;
91
+ this._state.asValues = (this.getAttribute('values')) ? this.getAttribute('values') : null;
92
+
93
+ this._initialState = this._state;
94
+ this.render();
95
+ }
96
+
97
+
98
+ reset() {
99
+ this._state = this._initialState;
100
+
101
+ this.render();
102
+ }
103
+
104
+ render() {
105
+
106
+ this.clearSlottedElements('filter');
107
+
108
+ this.shadowRoot.innerHTML = `${this.template}`;
109
+
110
+ this.part = 'cui-formpill';
111
+
112
+ let filter = document.createElement('div');
113
+ filter.part = 'filter';
114
+ filter.slot = 'filter';
115
+
116
+ let removeButton = document.createElement('button');
117
+ removeButton.textContent = '×'
118
+ removeButton.classList.add('pill-remove');
119
+ removeButton.part = 'pill-remove';
120
+
121
+ const formPill = document.createElement("div");
122
+ formPill.classList.add("form-pill")
123
+ formPill.part = "form-pill";
124
+ filter.appendChild(formPill);
125
+
126
+ this.append(filter);
127
+
128
+ const title = document.createElement('div');
129
+ title.classList.add('pill-title');
130
+ title.innerText = this._state.sText;
131
+
132
+ formPill.appendChild(title);
133
+
134
+ formPill.appendChild(removeButton);
135
+
136
+ const myEvent = new CustomEvent("pill_removed", {
137
+ detail: { 'state': this._state },
138
+ bubbles: true,
139
+ cancelable: true,
140
+ composed: false,
141
+ });
142
+
143
+ removeButton.addEventListener('click', (e) => {
144
+ this.remove();
145
+
146
+ this.dispatchEvent(myEvent)
147
+ })
148
+
149
+ }
150
+
151
+
152
+ }
153
+
154
+ customElements.define('cui-formpill-pill', CUI_FORMPILL_PILL);
155
+
156
+ class CUI_FORMPILL extends HTMLElement {
157
+ constructor() {
158
+ super();
159
+ this._connected = false;
160
+ this.attachShadow({ mode: 'open' });
161
+
162
+ this._state = {};
163
+ this._initialState = null;
164
+
165
+ this._state.bMenuDisplayed = false;
166
+
167
+ this._state.aMenuContents = [];
168
+
169
+ this._state.sHtmlContent = null;
170
+
171
+ this.dLabelContents;
172
+
173
+ this._state.configData = null;
174
+
175
+ this._state.adPills = [];
176
+
177
+ this._state.criteriaList = null;
178
+
179
+ this.oPropsToStateNames = {
180
+ "text": "sTextContent",
181
+ "label": "sLabelContent",
182
+ "contents": "aMenuContents",
183
+ "componentKey": "sComponentKey",
184
+ "position": "sPosition",
185
+ "iconbase": "sIconBasePath",
186
+ "accessibleHideLabel": "bAccessibleHideLabel",
187
+ "html": "sHtmlContent",
188
+ "config": "configData",
189
+ "addTitle": "sAddTitle",
190
+ "updateTitle": "sUpdateTitle"
191
+
192
+ }
193
+
194
+ this.cValidator = new VALIDATOR();
195
+ }
196
+
197
+ get sFieldLayout() {
198
+ return `
199
+ <div class="cui-field" part="outer-wrapper">
200
+ <div class="cui-label " part="label-wrapper">
201
+ <slot name="labelContents"></slot>
202
+ </div>
203
+ <slot name="pill-container"></slot>
204
+ <slot name="menu-trigger"></slot>
205
+ </div>`;
206
+ }
207
+
208
+ setState(props) {
209
+ // Need to map props to state variable names if we want to keep them prefixed.
210
+ this._state = Object.assign(this._state, this.normalizeProps(props));
211
+
212
+ if (this._connected) {
213
+ this.render();
214
+ }
215
+ }
216
+
217
+ get label() {
218
+
219
+ if (this.dLabelContents) {
220
+
221
+ return this.dLabelContents.textContent;
222
+
223
+ }
224
+
225
+ return null;
226
+ }
227
+
228
+ get value() {
229
+
230
+ let values = [];
231
+
232
+ for (let pill of this.querySelectorAll('cui-formpill-pill')) {
233
+ values.push(pill.contents);
234
+ }
235
+
236
+ return values;
237
+ }
238
+
239
+ normalizeProps(props) {
240
+ let updatedProps = {};
241
+
242
+ for (const [key, value] of Object.entries(props)) {
243
+ if (this.oPropsToStateNames[key]) {
244
+ updatedProps[this.oPropsToStateNames[key]] = value;
245
+ }
246
+ }
247
+
248
+ return updatedProps;
249
+ }
250
+
251
+ connectedCallback() {
252
+ // Call above setting connected to avoid second render.
253
+ this._connected = true;
254
+
255
+ //Set initial menu object based on children.
256
+
257
+ var match = window.matchMedia || window.msMatchMedia;
258
+
259
+ if (match) {
260
+ var mq = match("(pointer:coarse)");
261
+ this.bMobile = mq.matches;
262
+ }
263
+
264
+ //Set initial menu object based on children.
265
+ this._state.sTextContent = (this.getAttribute('text')) ? this.getAttribute('text') : null;
266
+ this._state.sLabel = (this.getAttribute('label')) ? this.getAttribute('label') : "";
267
+ this._state.sAddTitle = (this.getAttribute('addTitle')) ? this.getAttribute('addTitle') : "Add";
268
+ this._state.sUpdateTitle = (this.getAttribute('updateTitle')) ? this.getAttribute('updateTitle') : "Update";
269
+ this._state.sIconBasePath = (this.getAttribute('iconbase')) ? this.getAttribute('iconbase') : null;
270
+ this._state.sComponentKey = (this.getAttribute('componentKey')) ? this.getAttribute('componentKey') : "";
271
+ this._state.bAccessibleHideLabel = this.hasAttribute('accessiblehidelabel') ? true : false;
272
+ this._state.sPosition = (this.getAttribute('position')) ? this.getAttribute('position') : "bottom-center";
273
+
274
+ //Set initial state only once at connection to page. At this point any initial data should have been passed to the field.
275
+ if (!this._initialState || this._initialState == null) {
276
+ // Shallow clone for now unless it's determined we need a deep clone.
277
+ this._initialState = { ...this._state };
278
+ }
279
+
280
+ this._state.fGlobalBodyClick = this.globalBodyClick.bind(this);
281
+
282
+ this.render();
283
+ }
284
+
285
+ get footer() {
286
+ return `<div class="grid button-row">
287
+ <div class="row">
288
+ <div class="col-full align-end">
289
+ <cui-button class="addBtn" text="Add" primary="true"></cui-button>
290
+ <cui-button class="resetBtn" text="Clear"></cui-button>
291
+ </div>
292
+ </div>
293
+ </div>`
294
+ }
295
+
296
+ get pillFooter() {
297
+ return `<div class="grid button-row">
298
+ <div class="row">
299
+ <div class="col-full align-end">
300
+ <cui-button class="updateBtn" text="Update" primary="true"></cui-button>
301
+ <cui-button class="resetBtn" text="Reset"></cui-button>
302
+ </div>
303
+ </div>
304
+ </div>`
305
+ }
306
+
307
+ render() {
308
+ this.clearShadowDom(this.shadowRoot);
309
+ this.clearSlottedElements("menu-trigger");
310
+ this.clearSlottedElements("labelContents");
311
+ this.clearSlottedElements("pill-container");
312
+
313
+ this.shadowRoot.innerHTML = `${this.sFieldLayout} `;
314
+
315
+ let adSlots = this.shadowRoot.querySelectorAll('slot');
316
+
317
+ if (adSlots.length) {
318
+
319
+ for (let dSlot of adSlots) {
320
+ let dNode = dSlot.assignedNodes()[0];
321
+
322
+ if (dNode) {
323
+ let sSlotName = dNode.getAttribute("slot");
324
+
325
+ if (sSlotName && sSlotName.toLowerCase() === "labelcontents") {
326
+ this.dLabelContents = dNode;
327
+ }
328
+
329
+ }
330
+
331
+ }
332
+
333
+ }
334
+
335
+ let label = document.createElement('label');
336
+ label.slot = 'labelContents';
337
+
338
+ label.setAttribute('for', this._state.sComponentKey)
339
+
340
+ let pillDiv = document.createElement('div');
341
+ pillDiv.classList.add('pill-container');
342
+ pillDiv.part = "pill-container"
343
+ pillDiv.slot = "pill-container"
344
+ let dTriggerElem = document.createElement('button');
345
+
346
+ if (this._state.adPills.length > 0) {
347
+ for (let i = 0; i < this._state.adPills.length; i++) {
348
+ pillDiv.append(this._state.adPills[i])
349
+ }
350
+ }
351
+
352
+ dTriggerElem.title = this._state.sLabel;
353
+ dTriggerElem.setAttribute('aria-label', this._state.sLabel);
354
+
355
+ if (this._state.bAccessibleHideLabel) {
356
+ dTriggerElem.textContent = "";
357
+ label.textContent = "";
358
+ } else {
359
+ dTriggerElem.textContent = this._state.sLabel;
360
+ label.textContent = this._state.sLabel + ':';
361
+ }
362
+
363
+ dTriggerElem.setAttribute('aria-controls', 'menu2');
364
+ dTriggerElem.setAttribute('aria-haspopup', 'true');
365
+ dTriggerElem.classList.add("button-menu-trigger");
366
+ dTriggerElem.part = "menu-trigger"
367
+ dTriggerElem.slot = "menu-trigger";
368
+ dTriggerElem.type = "button";
369
+ dTriggerElem.addEventListener('click', async (event) => {
370
+ this.menuTriggerOnClick();
371
+ });
372
+
373
+ if (this._state.sIconBasePath) {
374
+ let icon = document.createElement('cui-icon');
375
+ icon.setAttribute("src", this._state.sIconBasePath)
376
+ icon.classList.add('accessibleHideLabel')
377
+ dTriggerElem.prepend(icon);
378
+ } else {
379
+ let addIconDiv = document.createElement('div');
380
+ addIconDiv.textContent = '+';
381
+ addIconDiv.classList.add('add-icon');
382
+ addIconDiv.part = 'add-icon';
383
+ dTriggerElem.prepend(addIconDiv);
384
+ }
385
+
386
+ this._state.dTriggerElem = dTriggerElem;
387
+ this.appendChild(label)
388
+ this.appendChild(pillDiv)
389
+ this.appendChild(this._state.dTriggerElem);
390
+
391
+ window.addEventListener('resize', (e) => {
392
+ this.windowResize(e);
393
+ window.removeEventListener('resize', this.windowResize())
394
+ });
395
+
396
+
397
+ }
398
+
399
+ menuTriggerOnClick(formpill = null) {
400
+ if (this._state.bMenuDisplayed) {
401
+ this.hidePopover();
402
+ } else if (formpill !== null) {
403
+ this.displayPillPopover(formpill);
404
+ }
405
+ else {
406
+ this.displayPopover(); 1
407
+ }
408
+ }
409
+
410
+ globalBodyClick(evt) {
411
+ if (this._state.dMenuPopover && !this._state.dMenuPopover.contains(evt.target)) {
412
+ this.hidePopover();
413
+ }
414
+ }
415
+
416
+ async createPopover(dTriggerElem, isPillPopover = false) {
417
+ if (this._state.bMenuDisplayed == true) {
418
+ return;
419
+ }
420
+
421
+ this._state.bMenuDisplayed = true;
422
+
423
+ let dMenuPopover = document.createElement('cui-formpill-popover');
424
+ if (!this.bMobile) {
425
+ dMenuPopover.classList.add("button-menu");
426
+ }
427
+
428
+ dMenuPopover.zIndex = 900;
429
+ dMenuPopover.for = dTriggerElem;
430
+ dMenuPopover.appendLocation = document.body;
431
+ dMenuPopover.includeArrow = true;
432
+
433
+ this._state.dMenuPopover = dMenuPopover;
434
+ this._state.dMenuPopover.state.sPosition = this._state.sPosition;
435
+ this._state.dMenuPopover.state.bOpen = true;
436
+ let dPopoverContent = null;
437
+ if (this.bMobile) {
438
+ dPopoverContent = document.createElement('dialog');
439
+ dPopoverContent.classList.add('dialog-popover-contents');
440
+
441
+ } else {
442
+ dPopoverContent = document.createElement('div');
443
+ dPopoverContent.classList.add('popover-contents');
444
+ }
445
+
446
+ dPopoverContent.slot = "popover-contents";
447
+ dPopoverContent.classList.add('popover-contents');
448
+ let dPopoverBody = document.createElement('div');
449
+ dPopoverBody.classList.add("popover-body");
450
+
451
+ let dPopoverHeader = document.createElement('div')
452
+ dPopoverHeader.classList.add('popover-header')
453
+
454
+ let dPopoverTitle = document.createElement('h3');
455
+
456
+ dPopoverHeader.appendChild(dPopoverTitle);
457
+
458
+ let closeButton = document.createElement('button');
459
+ closeButton.classList.add('close-button');
460
+ closeButton.textContent = '×'
461
+ dPopoverHeader.appendChild(closeButton);
462
+ closeButton.addEventListener('click', () => {this.hidePopover()})
463
+
464
+ let dPopoverFooter = document.createElement('div');
465
+ dPopoverFooter.classList.add("popover-footer");
466
+
467
+ if (isPillPopover === true) {
468
+ dPopoverTitle.textContent = this._state.sUpdateTitle;
469
+ dPopoverFooter.innerHTML = this.pillFooter
470
+ } else {
471
+ dPopoverTitle.textContent = this._state.sAddTitle;
472
+ dPopoverFooter.innerHTML = this.footer
473
+ }
474
+
475
+ let closeBtn = null;
476
+ if (this.bMobile) {
477
+
478
+ closeBtn = document.createElement('button');
479
+ closeBtn.classList.add('dialog-close');
480
+ closeBtn.setAttribute('id', 'dialog-close');
481
+ closeBtn.part = 'dialog-close';
482
+ let span = document.createElement('span');
483
+ span.part = 'dialog-close-span';
484
+ span.innerHTML = `&#215;`
485
+ closeBtn.appendChild(span)
486
+ let header = document.createElement('header');
487
+ header.appendChild(closeBtn)
488
+
489
+ dPopoverBody.appendChild(header)
490
+ }
491
+
492
+ dPopoverContent.addEventListener(
493
+ "eventAction",
494
+ (e) => {
495
+
496
+ if (e.target != this) {
497
+
498
+ e.stopImmediatePropagation();
499
+
500
+ let eventDetail = e.detail;
501
+ eventDetail.target = this;
502
+
503
+ // //target to detail
504
+
505
+ const E_CONTROL_EVENT = new CustomEvent("eventAction", {
506
+ bubbles: true,
507
+ composed: true,
508
+ detail: eventDetail
509
+ });
510
+
511
+ this.dispatchEvent(E_CONTROL_EVENT);
512
+ }
513
+
514
+ },
515
+ false
516
+ );
517
+
518
+ dPopoverBody.appendChild(dPopoverHeader)
519
+ dPopoverBody.appendChild(dPopoverFooter);
520
+ dPopoverContent.appendChild(dPopoverBody);
521
+ dMenuPopover.appendChild(dPopoverContent);
522
+
523
+ return [dPopoverBody, dPopoverContent, dMenuPopover, dPopoverFooter]
524
+ }
525
+
526
+ async displayPopover() {
527
+
528
+ let adPopoverElements = await this.createPopover(this._state.dTriggerElem);
529
+ let dPopoverBody = adPopoverElements[0];
530
+ let dPopoverContent = adPopoverElements[1];
531
+ let dMenuPopover = adPopoverElements[2];
532
+ let dPopoverFooter = adPopoverElements[3];
533
+
534
+
535
+ this.criteria = null;
536
+ this.method = null;
537
+ this.dValueField = null;
538
+ this.field = null;
539
+ this.adFields = [];
540
+
541
+ if (this._state.configData) {
542
+ if (this._state.configData.filterCriteria) {
543
+ this.criteria = document.createElement('cui-listbox');
544
+ this.criteria.setAttribute('search', '')
545
+ let items = []
546
+
547
+ if (this._state.criteriaList === null) {
548
+ for (let item of this._state.configData.filterCriteria.items) {
549
+ let lbValue = { 'label': item.label, 'value': item.value };
550
+ if (item.multiple) {
551
+ lbValue.multiple = item.multiple
552
+ }
553
+ items.push(lbValue);
554
+ }
555
+ this._state.criteriaList = items;
556
+ } else {
557
+ for (let item of this._state.criteriaList) {
558
+ items.push(item);
559
+ }
560
+ }
561
+
562
+ this.criteria.setState({ 'items': items, 'label': `${(this._state.configData.filterCriteria.fieldLabel) ? this._state.configData.filterCriteria.fieldLabel : 'Criteria'}` })
563
+ this.criteria.setAttribute('required', '');
564
+ this.criteria.setAttribute('componentKey', 'contextHeaderCriteria');
565
+ dPopoverBody.insertBefore(this.criteria, dPopoverFooter)
566
+
567
+ this.handleCriteriaChange(dPopoverBody, dPopoverFooter)
568
+
569
+ }
570
+ let dAddButton = dPopoverBody.querySelector('.addBtn');
571
+ this.handleAddUpdate(dAddButton, dPopoverBody, 'add')
572
+
573
+ dPopoverBody.querySelector('.resetBtn').addEventListener('click', (e) => {
574
+ this.criteria.setState({ 'value': '' })
575
+ this.criteria.messages.removeAll();
576
+ })
577
+
578
+ }
579
+
580
+ window.requestAnimationFrame(() => {
581
+ dMenuPopover.append();
582
+ if (this.bMobile) {
583
+ dPopoverContent.showModal()
584
+ closeBtn.addEventListener('click', (e) => {
585
+ dPopoverContent.close();
586
+ this.hidePopover();
587
+ })
588
+ }
589
+ document.body.addEventListener('click', this._state.fGlobalBodyClick, false);
590
+ });
591
+
592
+
593
+ }
594
+
595
+ async displayPillPopover(pill) {
596
+
597
+ let adPopoverElements = await this.createPopover(pill, true);
598
+ let dPopoverBody = adPopoverElements[0];
599
+ let dPopoverContent = adPopoverElements[1];
600
+ let dMenuPopover = adPopoverElements[2];
601
+ let dPopoverFooter = adPopoverElements[3];
602
+
603
+ this.criteria = null;
604
+ this.method = null;
605
+ this.dValueField = null;
606
+ this.field = null;
607
+ this.adFields = [];
608
+ if (this._state.configData) {
609
+ if (this._state.configData.filterCriteria) {
610
+ this.criteria = document.createElement('cui-listbox');
611
+ let items = []
612
+
613
+ for (let item of this._state.configData.filterCriteria.items) {
614
+ let lbValue = { 'label': item.label, 'value': item.value };
615
+ items.push(lbValue);
616
+ }
617
+
618
+ this.criteria.setState({ 'items': items, 'label': `${(this._state.configData.filterCriteria.fieldLabel) ? this._state.configData.filterCriteria.fieldLabel : 'Criteria'}` })
619
+ this.criteria.setAttribute('value', pill._state.sValue1)
620
+ this.criteria.setAttribute('required', '');
621
+ this.criteria.setAttribute('readOnly', '');
622
+ this.criteria.setAttribute('componentKey', 'contextHeaderCriteria');
623
+ dPopoverBody.insertBefore(this.criteria, dPopoverFooter)
624
+
625
+ this.handleCriteriaChange(dPopoverBody, dPopoverFooter, pill, true)
626
+
627
+ }
628
+ }
629
+ let formPill = dMenuPopover.state.dFor;
630
+ let dUpdateButton = dPopoverBody.querySelector('.updateBtn');
631
+ this.handleAddUpdate(dUpdateButton, dPopoverBody, 'update', formPill)
632
+
633
+ dPopoverBody.querySelector('.resetBtn').addEventListener('click', (e) => {
634
+ this.hidePopover();
635
+ this.menuTriggerOnClick(formPill);
636
+ })
637
+
638
+ window.requestAnimationFrame(() => {
639
+ dMenuPopover.append();
640
+ if (this.bMobile) {
641
+ dPopoverContent.showModal()
642
+ closeBtn.addEventListener('click', (e) => {
643
+ dPopoverContent.close();
644
+ this.hidePopover();
645
+ })
646
+ }
647
+ document.body.addEventListener('click', this._state.fGlobalBodyClick, false);
648
+ });
649
+
650
+
651
+ }
652
+
653
+ handleCriteriaChange(dPopoverBody, dPopoverFooter, pill = null, isPill = false) {
654
+ let selectedValue;
655
+
656
+ if (pill && isPill) {
657
+ selectedValue = pill._state.sValue1;
658
+ let selectedItem = this._state.configData.filterCriteria.items.find(item => item.value === selectedValue);
659
+
660
+ let controls = selectedItem.controls;
661
+ if (controls.options && controls.value) {
662
+
663
+ this.method = document.createElement('cui-listbox');
664
+ this.method.setState({ 'items': controls.options, 'label': `${(controls.fieldLabel) ? controls.fieldLabel : 'Method'}` });
665
+ this.method.setAttribute('value', pill._state.sValue2)
666
+ this.method.setAttribute('required', '');
667
+ this.method.setAttribute('componentKey', 'contextHeaderMethod');
668
+ dPopoverBody.insertBefore(this.method, dPopoverFooter);
669
+
670
+ if (controls.value.control === 'text') {
671
+ this.dValueField = document.createElement('cui-text');
672
+ }
673
+ if (controls.value.control === 'date') {
674
+ this.dValueField = document.createElement('cui-date');
675
+ }
676
+
677
+ this.dValueField.setState({ 'label': `${(controls.value.fieldLabel) ? controls.value.fieldLabel : 'Value'}` })
678
+ this.dValueField.setAttribute('value', pill._state.sValue3)
679
+ this.dValueField.setAttribute('required', '');
680
+ this.dValueField.setAttribute('componentKey', 'contextHeaderValue');
681
+ dPopoverBody.insertBefore(this.dValueField, dPopoverFooter);
682
+
683
+ } else if (controls.value && controls.value.options) {
684
+ this.dValueField = document.createElement('cui-listbox');
685
+ this.dValueField.setState({ 'items': controls.value.options, 'label': `${(controls.fieldLabel) ? controls.fieldLabel : 'Value'}` });
686
+
687
+ if (pill._state.sValue2.indexOf(',') == -1) {
688
+ this.dValueField.setAttribute('value', pill._state.sValue2)
689
+ }
690
+ else {
691
+ this.dValueField.setAttribute('multiple', '');
692
+ let aValues = pill._state.sValue2.split(',');
693
+ this.dValueField.setAttribute('value', aValues)
694
+ }
695
+
696
+ this.dValueField.setAttribute('required', '');
697
+ this.dValueField.setAttribute('componentKey', 'contextHeaderValue');
698
+ dPopoverBody.insertBefore(this.dValueField, dPopoverFooter);
699
+ } else if (controls.value && controls.value.control) {
700
+
701
+ for (let i = 0; i < controls.value.control.length; i++) {
702
+
703
+ if (controls.value.control[i].type === "text") {
704
+ this.field = document.createElement('cui-text');
705
+
706
+ } else if (controls.value.control[i].type === "date") {
707
+ this.field = document.createElement('cui-date');
708
+ }
709
+
710
+ this.field.setState({ 'label': controls.value.control[i].label })
711
+ if (controls.value.control[i].required) {
712
+ this.field.setAttribute('required', '');
713
+ }
714
+ this.field.setAttribute('componentKey', controls.value.control[i].componentKey);
715
+ dPopoverBody.insertBefore(this.field, dPopoverFooter);
716
+ this.adFields.push(this.field);
717
+
718
+ }
719
+ let pillValues = pill._state.asValues.split(',');
720
+ for (let i = 0; i < pillValues.length; i++) {
721
+ this.adFields[i].setAttribute('value', pillValues[i])
722
+
723
+ }
724
+ }
725
+ } else {
726
+
727
+
728
+ this.criteria.addEventListener('change', (e) => {
729
+
730
+ const customEvent = new CustomEvent('filter-change', { detail: { value: this.criteria.value, label: this.criteria.label } });
731
+ this.dispatchEvent(customEvent);
732
+
733
+ let removeCriteriaSiblings = function (element) {
734
+ Array.from(element.parentNode.children).filter(sibling => sibling !== element && (sibling.tagName.toLowerCase() === 'cui-listbox' || sibling.tagName.toLowerCase() === 'cui-text' || sibling.tagName.toLowerCase() === 'cui-date')).forEach(sibling => sibling.remove());
735
+ };
736
+ removeCriteriaSiblings(this.criteria);
737
+
738
+ selectedValue = e.detail.data.value;
739
+ let selectedItem = this._state.configData.filterCriteria.items.find(item => item.value === selectedValue);
740
+ if (selectedItem) {
741
+ let controls = selectedItem.controls;
742
+
743
+ if (controls.options && controls.value) {
744
+
745
+ this.method = document.createElement('cui-listbox');
746
+ this.method.setState({ 'items': controls.options, 'label': `${(controls.fieldLabel) ? controls.fieldLabel : 'Method'}` });
747
+ this.method.setAttribute('required', '');
748
+ this.method.setAttribute('componentKey', 'contextHeaderMethod');
749
+ dPopoverBody.insertBefore(this.method, dPopoverFooter);
750
+
751
+ if (controls.value.control === 'text') {
752
+ this.dValueField = document.createElement('cui-text');
753
+ this.dValueField.setState({ 'label': `${(controls.value.fieldLabel) ? controls.value.fieldLabel : 'Value'}` })
754
+ this.dValueField.setAttribute('required', '');
755
+ this.dValueField.setAttribute('componentKey', 'contextHeaderValue');
756
+ dPopoverBody.insertBefore(this.dValueField, dPopoverFooter);
757
+ }
758
+
759
+ } else if (controls.value && controls.value.options) {
760
+ this.dValueField = document.createElement('cui-listbox');
761
+ this.dValueField.setState({ 'items': controls.value.options, 'label': `${(controls.fieldLabel) ? controls.fieldLabel : 'Value'}` });
762
+ this.dValueField.setAttribute('required', '');
763
+ this.dValueField.setAttribute('componentKey', 'contextHeaderValue');
764
+ if (controls.value.multiple) {
765
+ this.dValueField.setAttribute('multiple', true)
766
+ }
767
+ dPopoverBody.insertBefore(this.dValueField, dPopoverFooter);
768
+ } else if (controls.value && controls.value.control) {
769
+
770
+ for (let i = 0; i < controls.value.control.length; i++) {
771
+ if (controls.value.control[i].type === "text") {
772
+ this.field = document.createElement('cui-text');
773
+
774
+ } else if (controls.value.control[i].type === "date") {
775
+ this.field = document.createElement('cui-date');
776
+ }
777
+
778
+ this.field.setState({ 'label': controls.value.control[i].label })
779
+ if (controls.value.control[i].required) {
780
+ this.field.setAttribute('required', '');
781
+ }
782
+ this.field.setAttribute('componentKey', controls.value.control[i].componentKey);
783
+ dPopoverBody.insertBefore(this.field, dPopoverFooter);
784
+ this.adFields.push(this.field);
785
+
786
+ }
787
+ }
788
+ } else {
789
+ console.log("No item found with the selected value.");
790
+ }
791
+ })
792
+ }
793
+
794
+ }
795
+
796
+ handleAddUpdate(dButton, dPopoverBody, sFor, pill = null) {
797
+ dButton.addEventListener('click', (e) => {
798
+ let validatedFields = this.validateRegion(dPopoverBody);
799
+
800
+ if (validatedFields) {
801
+ let formPill;
802
+ if (sFor === 'add') {
803
+ formPill = document.createElement('cui-formpill-pill');
804
+ this._state.adPills.push(formPill);
805
+ } else {
806
+ formPill = pill;
807
+ }
808
+
809
+ if (this.method !== null && this.dValueField !== null) {
810
+ let curValue = this.criteria.value;
811
+ formPill.setAttribute('value1', this.criteria.value)
812
+ formPill.setAttribute('value2', this.method.value)
813
+ formPill.setAttribute('value3', this.dValueField.value)
814
+ let pillText = `${this.criteria._state.aItems.find(item => item.value === this.criteria.value)?.label}: ${this.dValueField.value}`
815
+
816
+ if (sFor === 'update') {
817
+ formPill.setState({
818
+ 'value1': this.criteria.value, 'value2': this.method.value, 'value3': this.dValueField.value,
819
+ 'text': `${this.criteria._state.aItems.find(item => item.value === this.criteria.value)?.label}: ${this.dValueField.value}`
820
+ })
821
+ }
822
+ formPill.setAttribute('text', pillText.slice(0, 30) + (pillText.length > 30 ? "..." : ""))
823
+
824
+ if (sFor === 'add') {
825
+ let updatedList = this._state.criteriaList.filter(item => {
826
+ if (item.multiple) {
827
+ return true;
828
+ }
829
+ return item.value !== curValue;
830
+ });
831
+
832
+ this._state.criteriaList = updatedList;
833
+
834
+ formPill.addEventListener('pill_removed', (e) => {
835
+ let removedValue = e.detail.state.sValue1;
836
+ if (!this._state.criteriaList.some(item => item.value === removedValue)) {
837
+ this._state.criteriaList.unshift(this._state.configData.filterCriteria.items.find(item => item.value === removedValue));
838
+ }
839
+
840
+ const myEvent = new CustomEvent("pill-removed", {
841
+ detail: { pill: e.detail }
842
+ });
843
+ this.dispatchEvent(myEvent);
844
+ });
845
+ }
846
+
847
+ if (sFor === 'update') {
848
+ const myEvent = new CustomEvent("pill-updated", {
849
+ detail: { pill: formPill }
850
+ });
851
+ this.dispatchEvent(myEvent);
852
+ }
853
+
854
+ } else if (this.dValueField !== null) {
855
+
856
+ let curValue = this.criteria.value;
857
+ formPill.setAttribute('value1', curValue)
858
+
859
+
860
+ let pillText;
861
+ if (this.dValueField.value.indexOf(',') == -1) {
862
+ formPill.setAttribute('value2', this.dValueField.value)
863
+ pillText = `${this.criteria._state.aItems.find(item => item.value === this.criteria.value)?.label}: ${this.dValueField._state.aItems.find(item => item.value === this.dValueField.value)?.label}`
864
+
865
+ }
866
+ else {
867
+ let aValues = this.dValueField.value.split(',');
868
+ formPill.setAttribute('value2', aValues)
869
+
870
+ // Loop through each value in the array
871
+ const labels = aValues.map(val => {
872
+ // Find the corresponding label for each value
873
+ const item = this.dValueField._state.aItems.find(item => item.value === val);
874
+ return item?.label;
875
+ });
876
+
877
+ // Join the labels with a comma
878
+ pillText = `${this.criteria._state.aItems.find(item => item.value === this.criteria.value)?.label}: ${labels.join(', ')}`;
879
+
880
+ }
881
+
882
+ if (sFor === 'update') {
883
+ formPill.setState({
884
+ 'value1': this.criteria.value, 'value2': this.dValueField.value,
885
+ 'text': pillText
886
+ });
887
+ }
888
+
889
+ formPill.setAttribute('text', pillText.slice(0, 30) + (pillText.length > 30 ? "..." : ""))
890
+
891
+ if (sFor === 'add') {
892
+ let updatedList = this._state.criteriaList.filter(item => {
893
+ if (item.multiple) {
894
+ return true;
895
+ }
896
+ return item.value !== curValue;
897
+ });
898
+
899
+ this._state.criteriaList = updatedList;
900
+
901
+ formPill.addEventListener('pill_removed', (e) => {
902
+ let removedValue = e.detail.state.sValue1;
903
+ if (!this._state.criteriaList.some(item => item.value === removedValue)) {
904
+ this._state.criteriaList.unshift(this._state.configData.filterCriteria.items.find(item => item.value === removedValue));
905
+ }
906
+
907
+
908
+ const myEvent = new CustomEvent("pill-removed", {
909
+ detail: { pill: e.detail }
910
+ });
911
+ this.dispatchEvent(myEvent);
912
+ });
913
+
914
+
915
+ }
916
+
917
+ if (sFor === 'update') {
918
+ const myEvent = new CustomEvent("pill-updated", {
919
+ detail: { pill: formPill }
920
+ });
921
+ this.dispatchEvent(myEvent);
922
+ }
923
+ } else if (this.adFields.length > 0) {
924
+
925
+ let curValue = this.criteria.value;
926
+ let values = [];
927
+ for (let curField of this.adFields) {
928
+ values.push(curField.value)
929
+ }
930
+
931
+ formPill.setAttribute('value1', this.criteria.value)
932
+ formPill.setAttribute('values', values)
933
+
934
+ let sPillText = `${this.criteria._state.aItems.find(item => item.value === this.criteria.value)?.label}: ${values}`
935
+
936
+ if (sFor === 'update') {
937
+ formPill.setState({
938
+ 'value1': this.criteria.value, 'values': values.join(","),
939
+ 'text': `${this.criteria._state.aItems.find(item => item.value === this.criteria.value)?.label}: ${values.join(",")}`
940
+ });
941
+ }
942
+ formPill.setAttribute('text', sPillText.slice(0, 30) + (sPillText.length > 30 ? "..." : ""))
943
+
944
+ if (sFor === 'add') {
945
+ let updatedList = this._state.criteriaList.filter(item => {
946
+ if (item.multiple) {
947
+ return true;
948
+ }
949
+ return item.value !== curValue;
950
+ });
951
+
952
+ this._state.criteriaList = updatedList;
953
+
954
+ formPill.addEventListener('pill_removed', (e) => {
955
+ let removedValue = e.detail.state.sValue1;
956
+ if (!this._state.criteriaList.some(item => item.value === removedValue)) {
957
+ this._state.criteriaList.unshift(this._state.configData.filterCriteria.items.find(item => item.value === removedValue));
958
+ }
959
+
960
+
961
+ const myEvent = new CustomEvent("pill-removed", {
962
+ detail: { pill: e.detail }
963
+ });
964
+ this.dispatchEvent(myEvent);
965
+ });
966
+ }
967
+
968
+ if (sFor === 'update') {
969
+ const myEvent = new CustomEvent("pill-updated", {
970
+ detail: { pill: formPill }
971
+ });
972
+ this.dispatchEvent(myEvent);
973
+ }
974
+ }
975
+
976
+
977
+ this.querySelector('.pill-container').append(formPill);
978
+
979
+ if (sFor === 'add') {
980
+ const customEvent = new CustomEvent('pill-created', { detail: { pill: formPill } });
981
+ this.dispatchEvent(customEvent);
982
+
983
+ }
984
+
985
+
986
+ formPill.querySelector('.pill-title').addEventListener('click', (e) => {
987
+ this.menuTriggerOnClick(formPill);
988
+ });
989
+
990
+ this.hidePopover();
991
+ }
992
+ })
993
+
994
+ }
995
+
996
+ validateRegion(dRegion) {
997
+ let validationResult = this.cValidator.validateRegion(dRegion);
998
+
999
+
1000
+ if (validationResult == true) {
1001
+ this._removeScreenValidationMessage();
1002
+ return true;
1003
+ }
1004
+ else {
1005
+ this._setScreenValidationMessage();
1006
+ return false;
1007
+ }
1008
+ }
1009
+
1010
+ _setScreenValidationMessage() {
1011
+ let messageText = "Please correct the highlighted errors shown below before continuing.";
1012
+ if (this.messages) {
1013
+ this.messages.add({ "text": messageText, "type": "error" });
1014
+ }
1015
+ }
1016
+
1017
+ _removeScreenValidationMessage() {
1018
+ let messageText = "Please correct the highlighted errors shown below before continuing.";
1019
+ if (this.messages) {
1020
+ this.messages.remove({ "text": messageText });
1021
+ }
1022
+ }
1023
+
1024
+ /**
1025
+ *
1026
+ * @param {*} event
1027
+ */
1028
+ windowResize(event) {
1029
+
1030
+ if (this._state.bMenuDisplayed == true) {
1031
+ this.hidePopover();
1032
+ }
1033
+
1034
+ }
1035
+
1036
+ /**
1037
+ *
1038
+ * @returns
1039
+ */
1040
+ hidePopover() {
1041
+ if (this._state.bMenuDisplayed == false) {
1042
+ return;
1043
+ }
1044
+
1045
+ if (this._state.dMenuPopover) {
1046
+ this._state.dMenuPopover.state.bOpen = false;
1047
+ this._state.dMenuPopover.parentNode.removeChild(this._state.dMenuPopover)
1048
+ this._state.dMenuPopover = null;
1049
+ }
1050
+
1051
+ document.body.removeEventListener('click', this._state.fGlobalBodyClick);
1052
+
1053
+ this._state.bMenuDisplayed = false;
1054
+ }
1055
+
1056
+ disconnectedCallback() {
1057
+ this.hidePopover();
1058
+ // closeBtn.removeEventListener('click', (e));
1059
+ document.body.removeEventListener('click', this._state.fGlobalBodyClick);
1060
+ }
1061
+
1062
+ // Utility function function to clear all elements from shadow root.
1063
+ clearShadowDom(dShadowRoot) {
1064
+ while (dShadowRoot.lastChild) {
1065
+ dShadowRoot.removeChild(dShadowRoot.lastChild);
1066
+ }
1067
+ }
1068
+
1069
+ clearSlottedElements(slotName) {
1070
+ let aSlottedElements = this.querySelectorAll("[slot = " + slotName + "]");
1071
+ if (aSlottedElements && aSlottedElements.length > 0) {
1072
+ for (let i = 0; i < aSlottedElements.length; i++) {
1073
+ let dElement = aSlottedElements[i];
1074
+ this.removeChild(dElement);
1075
+ }
1076
+ }
1077
+ }
1078
+ }
1079
+
1080
+ export { CUI_FORMPILL, POPOVER_FORMPILL }