Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / permissions_by_term / js / src / drupal-behavior-function / node-form-client.js
1 import createPermission from '../async-function/create-permission';
2 import fetchFromBackend from '../async-function/fetch-from-backend';
3 import DomClient from '../client/dom-client.prototype';
4 import PermissionOutput from '../model/permission-output.prototype';
5 import PermissionOutputCollector from '../client/permission-output-collector.prototype';
6 import TermCollector from "../client/term-collector.prototype";
7
8 (($) => {
9
10   'use strict';
11
12   if (document.querySelector("#edit-permissions-by-term-info") !== null) {
13
14     /**
15      * @type {Drupal~behavior}
16      */
17     Drupal.behaviors.nodeForm = {
18       attach: async () => {
19         /**
20          * @var Backend backend
21          */
22         let backend = await createPermission(fetchFromBackend);
23
24         const hasTaxonomyFormFields = (permissions) => {
25           if (permissions.taxonomyRelationFieldNames.length !== 0) {
26             return true;
27           }
28
29           return false;
30         }
31
32         if (hasTaxonomyFormFields(backend)) {
33
34           const processPermissionsDisplay = () => {
35
36             const permissionOutput = new PermissionOutput,
37                 termCollector = new TermCollector,
38                 domClient = new DomClient(document, permissionOutput, Drupal);
39
40             const permissionOutputCollector = new PermissionOutputCollector(permissionOutput);
41
42             for (let formElementCssClass of backend.getFieldWrapperCSSClasses()) {
43               termCollector.addSelectedTids(domClient.computeTids(formElementCssClass));
44
45               permissionOutputCollector.collect(backend, termCollector.getSelectedTids());
46             }
47
48             domClient.renderPermissionsInfo();
49           }
50
51           for (let formElementCssClass of backend.getFieldWrapperCSSClasses()) {
52
53             $(formElementCssClass + ' input[type="text"]').on('autocomplete-select', () => {
54               processPermissionsDisplay();
55             });
56
57             $(formElementCssClass + ' select').change(function (){
58               processPermissionsDisplay();
59             });
60
61             $(formElementCssClass + ' input[type="text"]').on('keyup', function (){
62               processPermissionsDisplay();
63             });
64
65             $(formElementCssClass + ' input[type="checkbox"]').change(function (){
66               processPermissionsDisplay();
67             });
68
69           }
70
71         };
72
73       }
74
75     };
76
77     if (Drupal.autocomplete) {
78       /**
79        * Handles an auto-complete select event.
80        *
81        * Override the autocomplete method to add a custom event. Overriding is
82        * happening to get full input.
83        *
84        * @param {jQuery.Event} event
85        *   The event triggered.
86        * @param {object} ui
87        *   The jQuery UI settings object.
88        *
89        * @return {boolean}
90        *   Returns false to indicate the event status.
91        */
92       Drupal.autocomplete.options.select = function selectHandler(event, ui) {
93         var terms = Drupal.autocomplete.splitValues(event.target.value);
94         // Remove the current input.
95         terms.pop();
96         // Add the selected item.
97         if (ui.item.value.search(',') > 0) {
98           terms.push('"' + ui.item.value + '"');
99         }
100         else {
101           terms.push(ui.item.value);
102         }
103         event.target.value = terms.join(', ');
104         // Fire custom event that other controllers can listen to.
105         jQuery(event.target).trigger('autocomplete-select');
106
107         // Return false to tell jQuery UI that we've filled in the value
108         // already.
109         return false;
110       }
111     }
112
113   }
114
115 })(jQuery);