app/assets/javascripts/admin/banners/banners.js in tb_banners-1.2.1 vs app/assets/javascripts/admin/banners/banners.js in tb_banners-1.3.beta1

- old
+ new

@@ -1,21 +1,17 @@ // Place all the behaviors and hooks related to the matching controller here. // All this logic will automatically be available in application.js. (function(){ -var html5upload = false; var bannerEditId = false; spud.admin.banners = { // Index Page ///////////// init: function(){ - if(typeof(FormData) != 'undefined'){ - html5upload = true; - } $('.admin-banner-set-item-add').on('click', clickedAddNewBanner); $('.admin-banner-set-items-container').on('click', '.admin-banner-set-item-edit', clickedEditBanner); $('.admin-banner-set-items-container').on('click', '.admin-banner-set-item-delete', clickedDeleteBanner); $('.modal-body').on('submit', '.admin-banner-form', submittedBannerForm); $('.admin-banner-set-items-container').sortable({ @@ -85,47 +81,45 @@ } }); }; var submittedBannerForm = function(e){ - if(html5upload){ - e.preventDefault(); + e.preventDefault(); - var form = $(this); - var fd = new FormData(); - - $('input[type=text], input[type=hidden], select').each(function(index, element){ - var input = $(element); - var name = input.attr('name'); - var value = input.val(); - fd.append(name, value); - }); + var form = $(this); + var fd = new FormData(); - var richText = form.find('.admin-banner-rich-text'); - if(richText.length > 0){ - fd.append('spud_banner[rich_text]', richText.val()); - } - else{ - fd.append('spud_banner[rich_text]', ''); - } + $('input[type=text], input[type=hidden], select').each(function(index, element){ + var input = $(element); + var name = input.attr('name'); + var value = input.val(); + fd.append(name, value); + }); - var file = form.find('#spud_banner_banner')[0].files[0]; - if(file){ - fd.append('spud_banner[banner]', file); - $('.admin-banner-upload-progress').show(); - } + var richText = form.find('.admin-banner-rich-text'); + if(richText.length > 0){ + fd.append('spud_banner[rich_text]', richText.val()); + } + else{ + fd.append('spud_banner[rich_text]', ''); + } - var xhr = new XMLHttpRequest(); - xhr.upload.addEventListener('progress', onFileUploadProgress); - xhr.addEventListener('load', onFileUploadComplete); - xhr.addEventListener('error', onFileUploadError); - xhr.addEventListener('abort', onFileUploadAbort); - xhr.open('POST', form.attr('action')); - xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); - xhr.send(fd); - return false; + var file = form.find('#spud_banner_banner')[0].files[0]; + if(file){ + fd.append('spud_banner[banner]', file); + $('.admin-banner-upload-progress').show(); } + + var xhr = new XMLHttpRequest(); + xhr.upload.addEventListener('progress', onFileUploadProgress); + xhr.addEventListener('load', onFileUploadComplete); + xhr.addEventListener('error', onFileUploadError); + xhr.addEventListener('abort', onFileUploadAbort); + xhr.open('POST', form.attr('action')); + xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + xhr.send(fd); + return false; }; var onFileUploadProgress = function(e){ var percent = Math.round(e.loaded * 100 / e.total); var progress = $('.admin-banner-upload-progress'); @@ -136,14 +130,14 @@ }; var onFileUploadComplete = function(e){ switch(this.status){ case 200: - spud.admin.banners.onLegacyUploadComplete(e.target.response); + onUploadComplete(e.target.response); break; case 422: - spud.admin.banners.onLegacyUploadError(e.target.response); + onUploadError(e.target.response); break; default: window.alert("Whoops! Something has gone wrong."); } }; @@ -154,22 +148,21 @@ var onFileUploadAbort = function(e){ }; -// Non-html5 upload -spud.admin.banners.onLegacyUploadComplete = function(html){ +var onUploadComplete = function(html){ if(bannerEditId){ var item = $('.admin-banner-set-item[data-id="'+bannerEditId+'"]'); item.replaceWith(html); } else{ $('.admin-banner-set-items-container').append(html); } spud.admin.modal.hide(); }; -spud.admin.banners.onLegacyUploadError = function(html){ +var onUploadError = function(html){ $('.admin-banner-form').replaceWith(html); initForm(); }; var sortedBanners = function(e, ui){