Version 1
[yaffs-website] / web / modules / contrib / dropzonejs / modules / eb_widget / js / dropzonejs_eb_widget.common.js
1 /**
2  * @file
3  * dropzonejs_eb_widget.common.js
4  *
5  * Bundles various dropzone eb widget behaviours.
6  */
7
8 (function ($, Drupal, drupalSettings) {
9   'use strict';
10
11   Drupal.behaviors.dropzonejsPostIntegrationEbWidgetCommon = {
12     attach: function (context) {
13       if (typeof drupalSettings.dropzonejs.instances !== 'undefined') {
14         _.each(drupalSettings.dropzonejs.instances, function (item) {
15           var $form = $(item.instance.element).parents('form');
16
17           if ($form.hasClass('dropzonejs-disable-submit')) {
18             var $submit = $form.find('.is-entity-browser-submit');
19             $submit.prop('disabled', true);
20
21             item.instance.on('queuecomplete', function () {
22               if (item.instance.getRejectedFiles().length === 0) {
23                 $submit.prop('disabled', false);
24               }
25               else {
26                 $submit.prop('disabled', true);
27               }
28             });
29
30             item.instance.on('removedfile', function () {
31               if (item.instance.getRejectedFiles().length === 0) {
32                 $submit.removeAttr('disabled');
33               }
34
35               // If there are no files in DropZone -> disable Button.
36               if (item.instance.getAcceptedFiles().length === 0) {
37                 $submit.prop('disabled', true);
38               }
39             });
40
41             if (drupalSettings.entity_browser_widget && drupalSettings.entity_browser_widget.auto_select) {
42               item.instance.on('queuecomplete', function () {
43                 var dzInstance = item.instance;
44                 var filesInQueue = dzInstance.getQueuedFiles();
45                 var acceptedFiles;
46                 var i;
47
48                 if (filesInQueue.length === 0) {
49                   acceptedFiles = dzInstance.getAcceptedFiles();
50
51                   // Ensure that there are some files that should be submitted.
52                   if (acceptedFiles.length > 0 && dzInstance.getUploadingFiles().length === 0) {
53                     // First submit accepted files and clear them from list of
54                     // dropped files afterwards.
55                     jQuery(dzInstance.element)
56                       .parent()
57                       .siblings('[name="auto_select_handler"]')
58                       .trigger('auto_select_enity_browser_widget');
59
60                     // Remove accepted files -> because they are submitted.
61                     for (i = 0; i < acceptedFiles.length; i++) {
62                       dzInstance.removeFile(acceptedFiles[i]);
63                     }
64                   }
65                 }
66               });
67             }
68
69           }
70         });
71       }
72     }
73   };
74
75 }(jQuery, Drupal, drupalSettings));