(function($){ /***************************************************************** * Here starting main functionality of mail builder **/ var textEditor, MediumEditorHook ={ clean : function(){ (function(e){ var editor = $(e); editor.each(function(){ $(this) .removeAttr('contenteditable') .removeAttr('spellcheck') .removeAttr('data-medium-editor-element') .removeAttr('role') .removeAttr('aria-multiline') .removeAttr('data-medium-editor-editor-index') .removeAttr('medium-editor-index') .removeAttr('data-placeholder') .removeClass('medium-editor-element'); }); }('.medium-editor-element')); } }, /** * Clear tooltip **/ totalCleaner = function(){ $('.tooltip-editor').remove(); $('.editable-open, .editable').find('tbody > tr > td').popover('destroy'); $($('.editable-open, .editable').find('tbody > tr > td > table > tbody > tr')).popover('destroy'); $('*[data-toggle^="popover"]').each(function(){ $(this) .removeAttr('data-toggle') .removeAttr('data-content') .removeAttr('data-container') .removeAttr('data-original-title') .removeAttr('title') }); } /* Global Selectors */ var mb = { chooseTemplate : $('#choose-template'), optionTabs : $('#option-tabs'), mailTemplate : $('#mail-template'), editor : $("#editor"), }; $.fn.info = function(message, type, offset){ type = type || 'info'; offset = offset || 0; $id = $.rand(10000,99999); var $this = this; $this.append('
| '+ init.tooltips + movedData + ' | '+ '
Once you confirm, you can\'t stop this proccess and you will lost everything.
', title : 'Please confirm!', buttons: { confirm: { label: 'DELETE WHOLE PROJECT', className: 'btn-danger' }, cancel: { label: 'Cancel', className: 'btn-default' } }, callback: function(result){ if(result) { if(['no-sidebar','left-sidebar','right-sidebar','both-sidebar'].indexOf(id) > -1){ $.storage('save-'+id,null); $.storage('session_id',null); } window.location.href = window.base; var currentAttachments = $.storage('attachments'); if(null !== currentAttachments && currentAttachments.length > 0) { currentAttachments = JSON.parse(currentAttachments); for(id in currentAttachments) { $.post( window.base + '/include/upload.php', { session_id : localStorage.session_id, filename : currentAttachments[id], option : 'remove' }); // Clean image editor $.post( window.base + '/include/fetch-image.php', { clean : window.session_id, option : 'destroy' }).done(function(data){ }); // Clean image editor session for(var i in localStorage) { if(/editor_\d+/.test(i)) { localStorage.removeItem(i); } } $.storage('attachments',null); } } } }}); } }); $(document).on('click touchstart','#mail-template button.copy',function(e){ e.preventDefault(); var $this = $(this), copy = $this.parents('table'), parent = $(copy[0]).parent().attr('id'); $(copy[0]).clone().appendTo('#' + parent); }); /* Remove content section */ $(document).on('click touchstart','#mail-template button.remove',function(e){ e.preventDefault(); openEditor = false; var $this = $(this), remove = $this.parents('table')[0]; bootbox.confirm({ message : 'Please delete existing and upload new one.
','danger',offset); $($files).val(''); return false; } } $form.tooltip('hide'); var formData = new FormData($form[0]); formData.append('session_id',window.session_id); formData.append('option','upload'), metadata = []; if('FileReader' in window) { if(typeof $files.files !== 'undefined' && $files.files.length > 0) { // Check maximum upload size var maxSize = 0,errorExtension=false, offset = 0; for(fs=0; fs < $files.files.length; fs++) { metadata[localStorage.session_id + '_' + $files.files[fs].name] = { name : $files.files[fs].name, size : $files.files[fs].size, type : $files.files[fs].type, }; maxSize = maxSize + $files.files[fs].size; var fileType = $files.files[fs].name.split('.'); var extension = fileType[fileType.length-1].toLowerCase(); if(!(allowed.indexOf(extension)>-1)) { $('#alerts').info('Plese choose allowed file extensions.
','danger',offset); offset += 100; return false; } } maxSize = (maxSize/1000000); if(maxSize > 7) { $('#alerts').info('Your upload size is ' + (maxSize) + 'MB.
Plese resize your files or choose somethig smaller.
','danger'); $($files).val(''); return false; } } } $.ajax({ url: window.base + '/include/upload.php', type: 'POST', xhr: function() { var myXhr = $.ajaxSettings.xhr(); return myXhr; }, data: formData, cache: false, contentType: false, processData: false, dataType : 'json' }).always(function(){ $form.find('.glyphicon').text('Loading...'); }).done(function (data) { if(data[0].return !== true){ $('#alerts').info(data[0].message,'danger'); } $($files).val(''); // collection var attachments = [], attachmentsData = []; // Get from existing if(null !== currentAttachments && currentAttachments.length > 0) { currentAttachments = JSON.parse(currentAttachments); for(i in currentAttachments) { attachments.push(currentAttachments[i]); } } // Get new for(i in data) { if(data[i].return === true) { attachments.push(data[i].file_name); } } attachments = attachments.filter(function(val,i){ return attachments.indexOf(val)==i; }); // Save new data if(attachments.length > 0){ $.storage('attachments', JSON.stringify(attachments)); // attach-data init.attachments(); } $form.find('.glyphicon').text(''); }).fail(function (jqXHR,textStatus,errorThrown) { $($files).val(''); $('#alerts').info('ERROR: Can\'t upload this file for unexpected reason.','danger'); console.log('UPLOAD ERROR'); console.log('jqXHR = %s', jqXHR); console.log('textStatus = %s', textStatus); console.log('errorThrown = %s', errorThrown); console.log('-----------------------'); $form.find('.glyphicon').text('ERROR!'); }); }); /***************************************************************** * Here starting main DOM snippets and functionality **/ /* When form is changed, save into storage */ $('#mail-template').isChange(function(e){ if(window.location.hash) { var html = $(this).html(), id = window.location.hash, id = id.replace(/\#/,''); if(['no-sidebar','left-sidebar','right-sidebar','both-sidebar'].indexOf(id) > -1){ $.storage('save-'+id,html); } } },{offset:1000}); /* Load page settings */ $(document).on('click touchstart','#setting', function(){ $('#settings').toggleClass('in'); }); /* Dimiss tools */ $('[data-dismiss="tools"]').on('click toucstart',function(e){ e.preventDefault(); var $this = $(this), parent = $this.parents('.' + $this.attr('data-dismiss')); if(parent) parent.toggleClass('in'); }); /* When DOM is ready */ $(document).ready(function(){ // load theme on window refresh init.loadTheme(function(load){ if(load===true) { init.editorLoad(); init.dragAndDrop(); init.loadOptions(); } }); $('[data-toggle="tooltip"]').tooltip(); }); /* When AJAX is complete */ $(document).ajaxComplete(function(){ }); /* When resize happen */ $(window).resize(function(){ }); }(window.jQuery || window.Zepto));