Jake Vanderwerf
4 hours ago 56a9a1ccf764ff7a6af8f8a2292cb07443cb4aa7
assets/js/concise/CRUD.js
@@ -324,7 +324,11 @@
            },
            date: '[data-filter="date"]'
         },
         uploader: 'details.uploader'
         uploader: {
            details: 'details.uploader',
            form: 'details.uploader form',
            uploader: 'details.uploader [data-field-type="upload"]'
         }
      }
      this.ui = window.uiFromSelectors(this.selectors);
@@ -340,12 +344,17 @@
      this.isTimeline = !!document.querySelector('[data-timeline]');
   }
      initUploader() {
         if (!this.ui.uploader) return;
         if (!this.ui.uploader.form) return;
         this.uploadForm = this.forms.registerForm(this.ui.uploader.form).id??false;
         window.jvbUploads.scanFields(this.ui.uploader);
         // window.jvbUploads.scanFields(this.ui.uploader);
         window.jvbUploads.subscribe((event, data) => {
            if (event === 'sent-to-queue') {
               if (data === this.ui.uploader.dataset.uploader) {
               if (data.field.id === this.ui.uploader.uploader.dataset.uploader) {
                  if (this.uploadForm ) {
                     this.forms.store.delete(this.uploadForm);
                  }
                  window.debouncer.schedule('crud-complete', ()=> {
                     this.store.clearCache();
                  });
@@ -422,6 +431,7 @@
                  { name: 'modified', keyPath: 'modified'},
                  { name: 'title', keyPath: 'title'},
               ],
               isAuth: true,
               filters: filters,
               ignore: ['content', 'user'],
               TTL: 60 * 60 * 1000,       //1 hour cache
@@ -513,8 +523,10 @@
            && data.status === 'completed'
            && data.endpoint === 'uploads/groups') {
            if (data.result && data.result.group_mappings) {
               console.log('Handling group mapping from queue response');
               this.handleGroupMappings(data.result.group_mappings);
            }
            this.store.clearCache();
         }
@@ -539,39 +551,55 @@
            }
            if (Object.keys(data.result.success).length === 0) {
               this.changesStore.delete(id);
               console.log(data.result.success);
               data.result.success.forEach(id => this.changesStore.delete(id));
               this.store.clearCache();
            }
         }
         if (event === 'sent-to-server' && data.type === 'content_update') {
            if (data instanceof FormData) return;
            for ( let [id, changes] of Object.entries(data.posts)) {
               this.compareStored(id, changes);
            }
         }
      });
   }
   checkCompletedChanges(items) {
      for (let [id, data] of items) {
         this.compareStored(id, data);
      }
   }
      compareStored(id, data) {
         let stored = this.changesStore.get(id);
         if (!stored) continue;
         if (!stored) return;
         for (let [field, value] of Object.entries(data)) {
            if (Object.hasOwn(stored, field)) {
               let changes = window.getDifferences.map(stored[field], value);
               if (!changes) {
                  delete stored[field];
               } else {
                  stored[field] = changes;
               }
            }
         }
         //It'll have the id and the content still
         if (Object.values(stored).length === 2) {
         let hasID = Object.hasOwn(stored, 'id');
         let hasContent = Object.hasOwn(stored, 'content');
         if ((hasID && hasContent && Object.keys(stored).length === 2)
            || ((hasID || hasContent) && Object.keys(stored).length === 1)
            || Object.keys(stored).length === 0
         ) {
            this.changesStore.delete(id);
            this.store.clearCache();
         } else {
            this.changesStore.save(stored);
         }
      }
   }
   checkFailedChanges(items) {
      //TODO do something.
   }
@@ -637,7 +665,7 @@
            default: 'closed',
         },
         showUploader: {
            element: this.ui.uploader,
            element: this.ui.uploader.details,
            default: 'open'
         }
      };
@@ -689,6 +717,7 @@
      let title = `Saving changes for multiple ${this.plural}`;
      this.scheduleSave(0);
      this.modals.edit.handleClose();
   }
   async handleCreateSubmit(modal) {
@@ -856,6 +885,7 @@
      if (this.isPopulating) {
         return;
      }
      name.replace(`[${itemId}]`, '');
      const stored = this.store.get(itemId);
      if (stored) {
@@ -1037,7 +1067,7 @@
         return;
      }
      if (e.target.matches(this.selectors.buttons.create)) {
      if (e.target.matches(this.selectors.buttons.create) || e.target.closest(this.selectors.buttons.create)) {
         this.openCreateModal();
      }
   }
@@ -1264,8 +1294,11 @@
      this.ui.modals.edit.form.dataset.formId = `edit-${itemID}`;
      this.modals.edit.handleOpen();
      this.forms.registerForm(this.ui.modals.edit.form, {cache: false,
         autoUpload: true,});
      this.isPopulating = true;
      this.populate.populate(this.ui.modals.edit.form, item);
      //For quill/taxonomy selector's async setups
@@ -1275,7 +1308,6 @@
         });
      });
      this.modals.edit.handleOpen();
   }
   openBulkEditModal() {
      window.removeChildren(this.ui.modals.bulkEdit.selected);