Browse Source

Fix #192: Ability to extend and add one's own ajax settings for delete and upload

Kartik Visweswaran 10 years ago
parent
commit
5d9e819b1c
4 changed files with 74 additions and 62 deletions
  1. 3 0
      CHANGE.md
  2. 4 0
      README.md
  3. 67 62
      js/fileinput.js
  4. 0 0
      js/fileinput.min.js

+ 3 - 0
CHANGE.md

@@ -16,6 +16,9 @@ version 4.1.8
 12. (enh #187): New property `previewFileIcon` to configure file icon shown in preview for unreadable file types.
 12. (enh #187): New property `previewFileIcon` to configure file icon shown in preview for unreadable file types.
 13. (enh #188): Clear fileinput more correctly for all browsers when initialPreview is set enhancement
 13. (enh #188): Clear fileinput more correctly for all browsers when initialPreview is set enhancement
 14. (enh #189): Reinitialize initial preview delete events correctly on file selection.
 14. (enh #189): Reinitialize initial preview delete events correctly on file selection.
+15. (enh #192): Ability to extend and add one's own ajax settings.
+    - New property `ajaxDeleteSettings` to help extend and add to delete ajax settings. 
+    - `ajaxSettings` to help extend and add upload ajax settings
 
 
 version 4.1.7
 version 4.1.7
 =============
 =============

+ 4 - 0
README.md

@@ -954,6 +954,10 @@ _string_ the encoding to be used while reading a text file. Applicable only for
 #### ajaxSettings
 #### ajaxSettings
 _object_ additional ajax settings to pass to the plugin before submitting the ajax request. Applicable only for ajax uploads. This can be useful to pass additional tokens to headers or one can use it for setting other ajax options for advanced cases. Refer the [jQuery ajax documentation](http://api.jquery.com/jQuery.ajax/) for the various settings you can configure.
 _object_ additional ajax settings to pass to the plugin before submitting the ajax request. Applicable only for ajax uploads. This can be useful to pass additional tokens to headers or one can use it for setting other ajax options for advanced cases. Refer the [jQuery ajax documentation](http://api.jquery.com/jQuery.ajax/) for the various settings you can configure.
 
 
+#### ajaxDeleteSettings
+_object_ additional ajax settings to pass to the plugin before submitting the delete ajax request in each initial preview thumbnail. Applicable only for ajax deletions. This can be useful to pass additional tokens to headers or one can use it for setting other ajax options for advanced cases. Refer the [jQuery ajax documentation](http://api.jquery.com/jQuery.ajax/) for the various settings you can configure.
+
+
 ### Plugin Events
 ### Plugin Events
 The plugin supports these events:
 The plugin supports these events:
 
 

+ 67 - 62
js/fileinput.js

@@ -28,7 +28,7 @@
             div.parentNode.removeChild(div);
             div.parentNode.removeChild(div);
             return status;
             return status;
         },
         },
-        getNum = function(num, def) {
+        getNum = function (num, def) {
             def = def || 0;
             def = def || 0;
             if (typeof num === "number") {
             if (typeof num === "number") {
                 return num;
                 return num;
@@ -638,7 +638,8 @@
                 url = isSet('url', config) ? config.url : false,
                 url = isSet('url', config) ? config.url : false,
                 key = isSet('key', config) ? config.key : null,
                 key = isSet('key', config) ? config.key : null,
                 disabled = (url === false),
                 disabled = (url === false),
-                actions = self.initialPreviewShowDelete ? self.renderFileActions(false, true, disabled, url, key, i) : '',
+                actions = self.initialPreviewShowDelete ? self.renderFileActions(false, true, disabled, url, key,
+                    i) : '',
                 footer = template.repl('{actions}', actions);
                 footer = template.repl('{actions}', actions);
             return footer.repl('{caption}', caption).repl('{width}', width)
             return footer.repl('{caption}', caption).repl('{width}', width)
                 .repl('{indicator}', '').repl('{indicatorTitle}', '');
                 .repl('{indicator}', '').repl('{indicatorTitle}', '');
@@ -742,69 +743,70 @@
                     }
                     }
                 };
                 };
             self.$preview.find('.kv-file-remove').each(function () {
             self.$preview.find('.kv-file-remove').each(function () {
-                var $el = $(this), $frame = $el.closest('.file-preview-frame'), index = $el.data('index'), 
+                var $el = $(this), $frame = $el.closest('.file-preview-frame'), index = $el.data('index'),
                     config = isEmpty(self.initialPreviewConfig[index]) ? null : self.initialPreviewConfig[index],
                     config = isEmpty(self.initialPreviewConfig[index]) ? null : self.initialPreviewConfig[index],
                     extraData = isEmpty(config) || isEmpty(config.extra) ? deleteExtraData : config.extra,
                     extraData = isEmpty(config) || isEmpty(config.extra) ? deleteExtraData : config.extra,
-                    vUrl = $el.data('url') || self.deleteUrl, vKey = $el.data('key'), $content;
-                if (typeof extraData === "function") { 
+                    vUrl = $el.data('url') || self.deleteUrl, vKey = $el.data('key'), $content, settings;
+                if (typeof extraData === "function") {
                     extraData = extraData();
                     extraData = extraData();
                 }
                 }
                 if (vUrl === undefined || vKey === undefined) {
                 if (vUrl === undefined || vKey === undefined) {
                     return;
                     return;
                 }
                 }
-                $el.off('click').on('click', function () {
-                    $.ajax({
-                        url: vUrl,
-                        type: 'POST',
-                        dataType: 'json',
-                        data: $.extend({key: vKey}, extraData),
-                        beforeSend: function (jqXHR) {
-                            addCss($frame, 'file-uploading');
-                            addCss($el, 'disabled');
-                            self.raise('filepredelete', [vKey, jqXHR, extraData]);
-                        },
-                        success: function (data, textStatus, jqXHR) {
-                            if (data.error === undefined) {
-                                self.raise('filedeleted', [vKey, jqXHR, extraData]);
-                            } else {
-                                self.showError(data.error, extraData, $el.attr('id'), vKey, 'filedeleteerror', jqXHR);
-                                $frame.removeClass('file-uploading');
-                                $el.removeClass('disabled');
-                                resetProgress();
-                                return;
-                            }
-                            $frame.removeClass('file-uploading').addClass('file-deleted');
-                            $frame.fadeOut('slow', function () {
-                                self.clearObjects($frame);
-                                $frame.remove();
-                                $content = $(document.createElement('div')).html(self.original.preview);
-                                $content.find('.file-preview-frame').each(function () {
-                                    $that = $(this);
-                                    if ($that.find('.kv-file-remove').attr('data-key') == vKey) {
-                                        $that.remove();
-                                    }
-                                });
-                                self.initialPreviewContent = $content.html();
-                                if (self.initialPreviewCount > 0) {
-                                    self.initialPreviewCount -= 1;
-                                }
-                                caption = self.initialCaption;
-                                if (self.initialCaption.length === 0) {
-                                    caption = self.msgSelected.repl('{n}', self.initialPreviewCount);
-                                }
-                                self.original.preview = $content.html();
-                                self.setCaption(caption);
-                                self.original.caption = self.$caption.html();
-                                $content.remove();
-                                resetProgress();
-                            });
-                        },
-                        error: function (jqXHR, textStatus, errorThrown) {
-                            self.showError(errorThrown, extraData, $el.attr('id'), vKey, 'filedeleteerror', jqXHR);
+                settings = $.extend({
+                    url: vUrl,
+                    type: 'POST',
+                    dataType: 'json',
+                    data: $.extend({key: vKey}, extraData),
+                    beforeSend: function (jqXHR) {
+                        addCss($frame, 'file-uploading');
+                        addCss($el, 'disabled');
+                        self.raise('filepredelete', [vKey, jqXHR, extraData]);
+                    },
+                    success: function (data, textStatus, jqXHR) {
+                        if (data.error === undefined) {
+                            self.raise('filedeleted', [vKey, jqXHR, extraData]);
+                        } else {
+                            self.showError(data.error, extraData, $el.attr('id'), vKey, 'filedeleteerror', jqXHR);
                             $frame.removeClass('file-uploading');
                             $frame.removeClass('file-uploading');
+                            $el.removeClass('disabled');
                             resetProgress();
                             resetProgress();
+                            return;
                         }
                         }
-                    });
+                        $frame.removeClass('file-uploading').addClass('file-deleted');
+                        $frame.fadeOut('slow', function () {
+                            self.clearObjects($frame);
+                            $frame.remove();
+                            $content = $(document.createElement('div')).html(self.original.preview);
+                            $content.find('.file-preview-frame').each(function () {
+                                $that = $(this);
+                                if ($that.find('.kv-file-remove').attr('data-key') == vKey) {
+                                    $that.remove();
+                                }
+                            });
+                            self.initialPreviewContent = $content.html();
+                            if (self.initialPreviewCount > 0) {
+                                self.initialPreviewCount -= 1;
+                            }
+                            caption = self.initialCaption;
+                            if (self.initialCaption.length === 0) {
+                                caption = self.msgSelected.repl('{n}', self.initialPreviewCount);
+                            }
+                            self.original.preview = $content.html();
+                            self.setCaption(caption);
+                            self.original.caption = self.$caption.html();
+                            $content.remove();
+                            resetProgress();
+                        });
+                    },
+                    error: function (jqXHR, textStatus, errorThrown) {
+                        self.showError(errorThrown, extraData, $el.attr('id'), vKey, 'filedeleteerror', jqXHR);
+                        $frame.removeClass('file-uploading');
+                        resetProgress();
+                    }
+                }, self.ajaxDeleteSettings);
+                $el.off('click').on('click', function () {
+                    $.ajax(settings);
                 });
                 });
             });
             });
         },
         },
@@ -988,7 +990,7 @@
         ajaxSubmit: function (fnBefore, fnSuccess, fnComplete, fnError) {
         ajaxSubmit: function (fnBefore, fnSuccess, fnComplete, fnError) {
             var self = this, settings;
             var self = this, settings;
             self.uploadExtra();
             self.uploadExtra();
-            settings = $.extend(self.ajaxSettings, {
+            settings = $.extend({
                 xhr: function () {
                 xhr: function () {
                     var xhrobj = $.ajaxSettings.xhr();
                     var xhrobj = $.ajaxSettings.xhr();
                     return self.initXhr(xhrobj, 98);
                     return self.initXhr(xhrobj, 98);
@@ -1004,7 +1006,7 @@
                 success: fnSuccess,
                 success: fnSuccess,
                 complete: fnComplete,
                 complete: fnComplete,
                 error: fnError
                 error: fnError
-            });
+            }, self.ajaxSettings);
             self.ajaxRequests.push($.ajax(settings));
             self.ajaxRequests.push($.ajax(settings));
         },
         },
         uploadSingle: function (i, files, allFiles) {
         uploadSingle: function (i, files, allFiles) {
@@ -1426,7 +1428,7 @@
                     $status.html('');
                     $status.html('');
                     return;
                     return;
                 }
                 }
-                var node = ctr + i, previewId = previewInitId + "-" + node, isText, file = files[i], 
+                var node = ctr + i, previewId = previewInitId + "-" + node, isText, file = files[i],
                     caption = self.slug(file.name), fileSize = (file.size || 0) / 1000, checkFile, fileExtExpr = '',
                     caption = self.slug(file.name), fileSize = (file.size || 0) / 1000, checkFile, fileExtExpr = '',
                     previewData = objUrl.createObjectURL(file), fileCount = 0, j, msg, typ, chk,
                     previewData = objUrl.createObjectURL(file), fileCount = 0, j, msg, typ, chk,
                     fileTypes = self.allowedFileTypes, strTypes = isEmpty(fileTypes) ? '' : fileTypes.join(', '),
                     fileTypes = self.allowedFileTypes, strTypes = isEmpty(fileTypes) ? '' : fileTypes.join(', '),
@@ -1543,7 +1545,7 @@
             self.$container.removeClass('file-input-new file-input-ajax-new');
             self.$container.removeClass('file-input-new file-input-ajax-new');
             if (arguments.length === 1) {
             if (arguments.length === 1) {
                 self.raise('fileselect', [numFiles, label]);
                 self.raise('fileselect', [numFiles, label]);
-            }            
+            }
             if (self.initialPreviewContent.length > 0) {
             if (self.initialPreviewContent.length > 0) {
                 self.initPreviewDeletes();
                 self.initPreviewDeletes();
             }
             }
@@ -1566,8 +1568,10 @@
                 };
                 };
             self.resetUpload();
             self.resetUpload();
             self.hideFileIcon();
             self.hideFileIcon();
-            if (isDragDrop) {
+            if (self.isUploadable) {
                 self.$container.find('.file-drop-zone .' + self.dropZoneTitleClass).remove();
                 self.$container.find('.file-drop-zone .' + self.dropZoneTitleClass).remove();
+            }
+            if (isDragDrop) {
                 tfiles = files;
                 tfiles = files;
             } else {
             } else {
                 if (e.target.files === undefined) {
                 if (e.target.files === undefined) {
@@ -1595,7 +1599,7 @@
                 } else {
                 } else {
                     $preview.html('');
                     $preview.html('');
                 }
                 }
-                
+
                 if (isSingleUpload && ctr > 0) {
                 if (isSingleUpload && ctr > 0) {
                     self.filestack = [];
                     self.filestack = [];
                 }
                 }
@@ -1837,7 +1841,8 @@
         fileActionSettings: {},
         fileActionSettings: {},
         otherActionButtons: '',
         otherActionButtons: '',
         textEncoding: 'UTF-8',
         textEncoding: 'UTF-8',
-        ajaxSettings: {}
+        ajaxSettings: {},
+        ajaxDeleteSettings: {}
     };
     };
 
 
     $.fn.fileinput.Constructor = FileInput;
     $.fn.fileinput.Constructor = FileInput;

File diff suppressed because it is too large
+ 0 - 0
js/fileinput.min.js


Some files were not shown because too many files changed in this diff