Conditions | 4 |
Paths | 170 |
Total Lines | 154 |
Code Lines | 105 |
Lines | 28 |
Ratio | 18.18 % |
Changes | 6 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
35 | initDropzone() { |
||
36 | if (typeof Dropzone != 'undefined') { |
||
37 | Dropzone.autoDiscover = false; |
||
38 | |||
39 | // Cards version |
||
40 | const dropzoneCards = $('#dropzone-cards'); |
||
41 | if(dropzoneCards.length) { |
||
42 | |||
43 | let dropzoneCardsActionUrl = dropzoneCards.data('action-url'); |
||
|
|||
44 | |||
45 | let dropzoneCardsFilePreview = dropzoneCardsElement.find('#dropzone-cards-template'); |
||
46 | dropzoneCardsFilePreview.removeAttr('id'); |
||
47 | |||
48 | let dropzoneCardsFilePreviewTemplate = dropzoneCardsFilePreview.parent().html(); |
||
49 | dropzoneCardsFilePreview.parent().remove(); |
||
50 | |||
51 | let dropzoneCards = new Dropzone('#dropzone-cards-form', { |
||
52 | url: dropzoneCardsActionUrl, |
||
53 | autoProcessQueue: true, |
||
54 | thumbnailWidth: null, |
||
55 | thumbnailHeight: null, |
||
56 | previewTemplate: dropzoneCardsFilePreviewTemplate |
||
57 | }); |
||
58 | |||
59 | View Code Duplication | dropzoneCards.on("addedfile", function (file) { |
|
60 | var fileId = 'media' + document.querySelectorAll('.media-list-item').length; |
||
61 | file.previewElement.getElementsByTagName('input')[0].setAttribute('id', fileId); |
||
62 | file.previewElement.getElementsByTagName('label')[0].setAttribute('for', fileId); |
||
63 | |||
64 | var imagesFileTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']; |
||
65 | if (imagesFileTypes.indexOf(file.type) != -1) { |
||
66 | file.previewElement.querySelector('.media-item-file-details').style.display = 'none'; |
||
67 | } else if (file.type === 'application/pdf') { |
||
68 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
69 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-pdf"></i>'; |
||
70 | } else if (file.type === 'application/doc' | 'application/docx') { |
||
71 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
72 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-word"></i>'; |
||
73 | } else if (file.type === 'application/ppt' | 'application/pptx') { |
||
74 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
75 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-powerpoint"></i>'; |
||
76 | } else if (file.type === 'video/mp4' | 'video/webm' | 'video/mkv') { |
||
77 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
78 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-video"></i>'; |
||
79 | } else if (file.type === 'audio/mpeg') { |
||
80 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
81 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-audio"></i>'; |
||
82 | } else { |
||
83 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
84 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file"></i>'; |
||
85 | } |
||
86 | }); |
||
87 | |||
88 | dropzoneCards.on("success", function (file, resp) { |
||
89 | file.previewElement.querySelector(".media-list-item").classList.remove('uploading'); |
||
90 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
91 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
92 | }); |
||
93 | |||
94 | dropzoneCards.on("error", function (file) { |
||
95 | file.previewElement.querySelector(".media-list-item").classList.remove('uploading'); |
||
96 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
97 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
98 | }); |
||
99 | } |
||
100 | |||
101 | // Table version |
||
102 | const dropzoneTable = $('#dropzone-table'); |
||
103 | if(dropzoneTable.length) { |
||
104 | |||
105 | let dropzoneTableActionUrl = dropzoneTable.data('action-url'); |
||
106 | |||
107 | let dropzoneTableFilePreview = dropzoneTableElement.find('#dropzone-table-template'); |
||
108 | dropzoneTableFilePreview.removeAttr('id'); |
||
109 | |||
110 | let dropzoneTableFilePreviewTemplate = dropzoneTableFilePreview.parent().html(); |
||
111 | dropzoneTableFilePreview.parent().remove(); |
||
112 | |||
113 | let dropzoneTable = new Dropzone('#dropzone-table-form', { |
||
114 | url: dropzoneTableActionUrl, |
||
115 | autoProcessQueue: false, |
||
116 | thumbnailWidth: null, |
||
117 | thumbnailHeight: null, |
||
118 | previewTemplate: dropzoneTableFilePreviewTemplate, // Define the container to display the previews |
||
119 | previewsContainer: ".media-list-table", |
||
120 | clickable: "#dropzone-add-file", // Define the element that should be used as click trigger to select files. |
||
121 | }); |
||
122 | |||
123 | dropzoneTable.on("addedfile", function (file) { |
||
124 | console.log('test'); |
||
125 | var imagesFileTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']; |
||
126 | if (imagesFileTypes.indexOf(file.type) != -1) { |
||
127 | file.previewElement.querySelector('.media-item-file-details').style.display = 'none'; |
||
128 | } else if (file.type === 'application/pdf') { |
||
129 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
130 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-pdf"></i>'; |
||
131 | } else if (file.type === 'application/doc' | 'application/docx') { |
||
132 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
133 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-word"></i>'; |
||
134 | } else if (file.type === 'application/ppt' | 'application/pptx') { |
||
135 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
136 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-powerpoint"></i>'; |
||
137 | } else if (file.type === 'video/mp4' | 'video/webm' | 'video/mkv') { |
||
138 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
139 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-video"></i>'; |
||
140 | } else if (file.type === 'audio/mpeg') { |
||
141 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
142 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-audio"></i>'; |
||
143 | } else { |
||
144 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
145 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file"></i>'; |
||
146 | } |
||
147 | // Hookup the start button |
||
148 | file.previewElement.querySelector(".start").onclick = function() { dropzoneTable.enqueueFile(file); }; |
||
149 | }); |
||
150 | |||
151 | dropzoneTable.on("totaluploadprogress", function(progress) { |
||
152 | document.querySelector("#dropzone-table-total-progress .progress-bar").style.width = progress + "%"; |
||
153 | }); |
||
154 | |||
155 | dropzoneTable.on("sending", function(file) { |
||
156 | // Show the total progress bar when upload starts |
||
157 | document.querySelector("#dropzone-table-total-progress").style.opacity = "1"; |
||
158 | // And disable the start button |
||
159 | file.previewElement.querySelector(".start").setAttribute("disabled", "disabled"); |
||
160 | }); |
||
161 | |||
162 | dropzoneTable.on("success", function (file, resp) { |
||
163 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
164 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
165 | }); |
||
166 | |||
167 | dropzoneTable.on("error", function (file) { |
||
168 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
169 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
170 | }); |
||
171 | |||
172 | dropzoneTable.on("queuecomplete", function(progress) { |
||
173 | document.querySelector("#dropzone-table-total-progress").style.opacity = "0"; |
||
174 | }); |
||
175 | |||
176 | // Setup the buttons for all transfers |
||
177 | // The "add files" button doesn't need to be setup because the config |
||
178 | // `clickable` has already been specified. |
||
179 | document.querySelector("#dropzone-table-actions .start").onclick = function() { |
||
180 | dropzoneTable.enqueueFiles(dropzoneTable.getFilesWithStatus(Dropzone.ADDED)); |
||
181 | }; |
||
182 | |||
183 | document.querySelector("#dropzone-table-actions .cancel").onclick = function() { |
||
184 | dropzoneTable.removeAllFiles(true); |
||
185 | }; |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | |||
193 | } |