core/admin/assets/js/admin-ui-temp.js

Temporary Admin UI

/** * globals document, * jQuery **/ (function ($) { "use strict"; $(document).ready(function () { /** * Allows to check contents of each element exactly * @param obj * @param index * @param meta * @param stack * @returns {boolean} */ $.expr[":"].containsExact = function (obj, index, meta, stack) { return (obj.textContent || obj.innerText || $(obj).text() || "") == meta[3]; };

EDITOR / NOTIFICATIONS

$('.entry-content header, .entry-preview header').on('click', function () { $('.entry-content, .entry-preview').removeClass('active'); $(this).closest('section').addClass('active'); }); $('.entry-title .icon-fullscreen').on('click', function (e) { e.preventDefault(); $('body').toggleClass('fullscreen'); }); $('.content-list-content li').on('click', function (e) { console.log('hello'); var $target = $(e.target).closest('li'), $preview = $('.content-preview'); $('.content-list-content li').removeClass('active'); $target.addClass('active');

this means a lot of extra gumpf is in the DOM and should really be done with AJAX when we have proper data API endpoints ideally, we need a way to bind data to views properly... backbone marionette, angular, etc


// /** * @todo Remove gumpf */ $preview.find('.content-preview-content .wrapper').html($target.data('content')); $preview.find('.post-controls .post-edit').attr('href', '/ghost/editor/' + $target.data('id')); }); $('.options.up').on('click', function(){ $(this).next("ul").fadeToggle(200); }); $('.editor-options').on('click', 'li', function(){ $('.button-save').data("state", $(this).data("title")).attr('data-state', $(this).data("title")).text($(this).text()); }); /** Tag Selector UI **/ var existingTags = [ 'quim', 'quimtastic', 'quimmy', 'quimlord', 'joaquim pheonix', 'quimcy jones' ]; var suggestions = $("ul.suggestions").hide(); if($('.category-input').length) { var categoryOffset = $('.category-input').offset().left; $('.category-blocks').css({'left':categoryOffset + 'px'}); } function findTerms(searchTerm, array) { searchTerm = searchTerm.toUpperCase(); return $.map(array, function(item){ var match = item.toUpperCase().indexOf(searchTerm) !== -1; return match ? item : null; }); } $('[data-input-behaviour="tag"]').keyup(function(e){ var searchTerm = $.trim($(this).val()).toLowerCase(); if (e.keyCode == 38) { e.preventDefault(); if(suggestions.is(":visible")){ if(suggestions.children(".selected").length == 0){ suggestions.find("li:last-child").addClass('selected'); } else{ suggestions.children(".selected").removeClass('selected').prev().addClass('selected'); } } return false; }else if(e.keyCode == 40){ if(suggestions.is(":visible")){ if(suggestions.children(".selected").length == 0){ suggestions.find("li:first-child").addClass('selected'); } else{ suggestions.children(".selected").removeClass('selected').next().addClass('selected'); } } return false; }else if(e.keyCode == 27){ suggestions.hide(); }else if((e.keyCode == 13 || e.keyCode == 188) && searchTerm) { //enter or comma key e.preventDefault(); if(suggestions.is(":visible") && suggestions.children(".selected").length != 0){ if($('.category:containsExact("'+suggestions.children(".selected").text()+'")').length == 0){ var category = $('<span class="category">'+suggestions.children(".selected").text()+'</span>'); if($(this).data('populate')){ var populator = $($(this).data('populate')); populator.append(category); } } suggestions.hide(); }else{ if($('.category:containsExact("'+searchTerm+'")').length == 0){ var category = $('<span class="category">'+searchTerm+'</span>'); if($(this).data('populate')){ var populator = $($(this).data('populate')); populator.append(category); } } } $(this).val('').focus(); }else if(e.keyCode != 8) { //not deleting } if(searchTerm){ suggestions.show(); var results = findTerms(searchTerm, existingTags); var pos = $(this).position(); var styles = { left : pos.left }; suggestions.css(styles); suggestions.html(""); for(var i=0; i < results.length; i++){ suggestions.append("<li><a href='#'>"+results[i]+"</a></li>"); } var suggest = $('ul.suggestions li a:contains("' + searchTerm+ '")'); suggest.each(function(){ var src_str = $(this).html(); var term = searchTerm; term = term.replace(/(\s+)/,"(<[^>]+>)*$1(<[^>]+>)*"); var pattern = new RegExp("("+term+")", "i"); src_str = src_str.replace(pattern, "<mark>$1</mark>"); src_str = src_str.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/,"$1</mark>$2<mark>$4"); $(this).html(src_str); }); }else{ suggestions.hide(); } }).keydown(function(e){

Delete character tiggers on Keydown, so needed to check on that event rather than Keyup.

if(e.keyCode == 8 && !$(this).val()) { var populator = $($(this).data('populate')); var lastBlock = populator.find('.category').last(); lastBlock.remove(); } }); $('ul.suggestions').on('click', "li", function(){ var category = $('<span class="category">'+$(this).text()+'</span>'); if($(this).parent().data('populate')){ var populator = $($(this).parent().data('populate')); populator.append(category); suggestions.hide(); $('[data-input-behaviour="tag"]').val('').focus(); } }); $('.categories').on('click', ".category", function(){ $(this).remove(); }); $('[data-off-canvas]').click(function(e){ if(window.matchMedia('max-width: 650px')) { e.preventDefault(); $('body').toggleClass('off-canvas'); } }); }); }(jQuery));