jQuery(function($) { "use strict"; var TEMPLATES = new LuigiTemplate.Cache({ }); function update_slug(name, slug) { if (slug.prop('disabled')) { slug.val( name.val().toLowerCase() .replace(/[^a-z0-9_\.-]+/g, '-') .replace(/^-+|-+$/g, '') ); } } $.each(['blog', 'page', 'project'], function(i, id) { var p = '#' + id + '-edit-'; $(p + 'dialog').one('shown.bs.modal', function() { // lazy-init editor CKEDITOR.replace(id + '-edit-body'); }).on('show.bs.modal', function() { var me = $(this); // reset close confirmation me.data('close-dialog-confirmed', false); // show loading text me.find('.modal-body').addClass('hidden'); me.find('.modal-body.loading-text').removeClass('hidden'); send(id + '/get', { post_id: me.data('post_id') }).always(function() { me.find('.modal-body.loading-text').addClass('hidden'); }).fail(function(r) { var error = r.responseText; try { // try to extract error message from json response var data = $.parseJSON(r.responseText); if ('error' in data) error = data.error; } catch (e) {} me.find('.modal-body.loading-error').removeClass('hidden') .find('.error-text').text(error); }).done(function(r) { me.find('.modal-body.loading-done').removeClass('hidden'); me.trigger({ type: "post-data-loaded", post_data: r, }); }); }).on('shown.bs.modal', function() { $(p + 'name').focus(); }).on('hide.bs.modal', function() { return ( $(this).data('close-dialog-confirmed') || confirm('Close without saving changes?') ); }).find('button[data-dismiss="modal"]').click(function() { // override close confirmation // FIXME: should this only be on save? $(p + 'dialog').data('close-dialog-confirmed', true); }); $(p + 'confirm').click(function() { if ($(this).hasClass('disabled')) return false; // TODO: see #user-add-confirm alert('TODO: create'); // stop event return false; }); }); $('.post-name').keydown(function() { var name = $(this), slug = $(this).parents('.modal-body').find('.post-slug'); setTimeout(function() { update_slug(name, slug); }, 10); }); $('.post-slug-lock').click(function() { var modal_body = $(this).parents('.modal-body'); // toggle locked state $(this).toggleClass('btn-default btn-primary') .find('.fa').toggleClass('fa-lock fa-unlock'); var locked = $(this).hasClass('btn-default'); // update slug disabled state var slug = modal_body.find('.post-slug'); slug.prop('disabled', locked ? 'disabled' : null); if (locked) { // auto-generate slug update_slug(modal_body.find('.post-name'), slug); } // stop event return false; }); $('.state-buttons').on('a', 'click', function() { $(this).parent().find('.btn-primary').toggleClass('btn-default btn-primary'); $(this).toggleClass('btn-default btn-primary'); // stop event return false; }); });