(function() { var activate, disableRenameButton, enableRenameButton, getEditFileNameTemplate, hideError, hideProggresBar, rename, setupInputValidation, setupKeyBehaviour, showDeleteButton, showEditFileNameButton, showError, showProgressBar; activate = function(element) { var cmsField, container; cmsField = $(element); container = scrivito.editors._setupButtonContainer(cmsField); if (cmsField.scrivito('content') && cmsField.is('[data-scrivito-editors-allow-delete]')) { showDeleteButton(container, cmsField); } return showEditFileNameButton(container, cmsField); }; showEditFileNameButton = function(container, cmsField) { var binary, editor, fileName, fileNameVisibility, renameBtn; binary = cmsField.scrivito("content"); fileNameVisibility = cmsField.data('scrivito-editors-binary-filename'); if (typeof fileNameVisibility === "undefined") { fileNameVisibility = true; } if (binary && fileNameVisibility) { fileName = binary.filename; editor = $(getEditFileNameTemplate(fileName)).appendTo(container); setupKeyBehaviour(editor, cmsField); setupInputValidation(binary, editor); renameBtn = editor.find(".scrivito_file_save"); return renameBtn.click((function(_this) { return function() { return rename(cmsField, editor); }; })(this)); } }; rename = function(cmsField, editor) { var binary, fileName, newBinary; if (editor.find(".scrivito_file_save").hasClass("scrivito_disabled")) { return false; } showProgressBar(editor); binary = cmsField.scrivito("content"); fileName = editor.find('#binary_file_name').val(); newBinary = binary.copy({ filename: fileName }); return cmsField.scrivito("save", newBinary).then(function() { return cmsField.scrivito('reload'); }).fail(function() { return hideProggresBar(editor); }) && false; }; setupInputValidation = function(binary, editor) { var input; input = editor.find('#binary_file_name'); disableRenameButton(editor); return input.on('input', (function(_this) { return function() { var filename, message; filename = input.val(); disableRenameButton(editor); hideError(editor); if (filename.match(/^[\w.-]+$/)) { if (filename !== binary.filename) { return enableRenameButton(editor); } } else { message = 'The filename may only contain letters from A to Z, periods, hyphens, and underscores.'; return showError(message, editor); } }; })(this)); }; disableRenameButton = function(editor) { var saveElement; saveElement = editor.find('.scrivito_file_save'); return saveElement.addClass('scrivito_disabled'); }; enableRenameButton = function(editor) { var saveElement; saveElement = editor.find('.scrivito_file_save'); return saveElement.removeClass('scrivito_disabled scrivito_saving'); }; showError = function(message, editor) { var errorElement, errorMessage; errorElement = editor.find('div.scrivito_error'); editor.find('#binary_file_name').addClass('scrivito_error'); errorMessage = editor.find('.scrivito_error_message'); errorMessage.text(message); return errorElement.show(); }; hideError = function(editor) { var errorElement; errorElement = editor.find('div.scrivito_error'); editor.find('#binary_file_name').removeClass('scrivito_error'); return errorElement.hide(); }; showProgressBar = function(editor) { var button, input; input = editor.find('#binary_file_name'); input.attr('disabled', 'disabled'); disableRenameButton(editor); button = editor.find('.scrivito_file_save'); button.addClass('scrivito_saving'); return button.find('span').text('Renaming...'); }; hideProggresBar = function(editor) { var button, input; input = editor.find('#binary_file_name'); button = editor.find('.scrivito_file_save'); input.removeAttr('disabled'); enableRenameButton(editor); return button.find('span').text('Rename'); }; setupKeyBehaviour = function(editor, cmsField) { return editor.on('keyup', function(event) { var input; switch (event.keyCode) { case 13: return rename(cmsField, editor); case 27: input = editor.find('#binary_file_name'); input.val(cmsField.scrivito('content').filename); hideError(editor); disableRenameButton(editor); return input.blur(); } }); }; getEditFileNameTemplate = function(content) { return "
\n
\n \n \n Rename\n \n
\n \n \n
\n
\n
"; }; showDeleteButton = function(container, cmsField) { if (container && container.find('.delete-binary').length === 0) { return $(" ").prependTo(container).click(function() { return cmsField.scrivito('save', null).then(function() { return cmsField.scrivito('reload'); }) && false; }); } }; scrivito.editors.binary_editor = { can_edit: function(element) { return $(element).is('[data-scrivito-field-type=binary]'); }, activate: function(element) { return activate(element); } }; scrivito.on('load', function() { return scrivito.define_editor('binary', scrivito.editors.binary_editor); }); }).call(this);