let Mega = { init: false, init: function() { if (this.initialized) return; this.initialized = true; this.mega(); }, mega: function() { this.mega_sortable(); this.mega_del_item(); this.mega_del_all_item(); this.mega_add_single(); this.mega_add_folder(); this.mega_upload_files(); this.mega_click_upload(); }, mega_update: function() { this.mega_maxid(); this.mega_del_item(); AveAdmin.fancy_box(); AveAdmin.tooltip(); }, mega_maxid: function(id, doc) { let maxid = 1; $('#mega_' + doc + '_' + id).children('.mega_sortable').children('.mega_item').each(function() { maxid = Math.max(maxid, parseInt($(this).attr("data-id")) + 1); }); return maxid; }, mega_del_item: function() { $('.mega_item .delete').on('click', function(event) { event.preventDefault(); let id = $(this).attr('data-id'); jConfirm( mega_del_conf, mega_del_head, function(b) { if (b) { $('#mega_image_' + id).remove(); } } ); }); }, mega_del_all_item: function() { $('.mega_del_all').on('click', function(event) { event.preventDefault(); let c_id = $(this).parent().parent().parent('.mega').attr("data-id"); let d_id = $(this).parent().parent().parent('.mega').attr("data-doc"); let empty_input = ''; jConfirm( mega_del_all_c, mega_del_all_h, function(b) { if (b) { $('#mega_' + d_id + '_' + c_id).children('.mega_sortable').children('.mega_item').each(function() { $(this).remove(); }); $('#mega_' + d_id + '_' + c_id).append(empty_input); } } ); }); }, mega_upload_files: function() { $('.mega_upload').on('change', function(event) { let mega_input = $(this); let iid; event.preventDefault(); if (mega_input.val() == '') { return false; } let files_input = this.files.length; let max_files = mega_input.attr("data-max-files"); if (files_input > max_files) { $.jGrowl(mega_max_f_t, { header: mega_max_f_h, theme: 'error' }); mega_input.replaceWith(mega_input.val('').clone(true)); return false; } let c_id = $(this).parent('.mega').attr("data-id"); let d_id = $(this).parent('.mega').attr("data-doc"); let r_id = $(this).parent('.mega').attr("data-rubric"); $('#formDoc').ajaxSubmit({ url: 'index.php?do=fields', data: { "field_id": c_id, "rubric_id": r_id, "doc_id": d_id, "field": 'image_mega', "type": 'upload' }, beforeSend: function() { $.alerts._overlay('show'); }, dataType: "JSON", success: function(data) { if (data['respons'] == 'success') { for (let p = 0, max = data.files.length; p < max; p++) { iid = Mega.mega_maxid(c_id, d_id); let field_value = data['dir'] + data.files[p]; let img_path = data.thumbs[p]; $('#mega_' + d_id + '_' + c_id + ' > .mega_sortable:last').append( '