123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- $(function(){
- var root = window || {};
- root = root.GravJS = root.GravJS || {};
- //Make it global because used by ./forms/form.js
- root.currentValues = getState();
- var clickedLink;
- // selectize
- var pageFilter = $('input.page-filter'),
- pageTypes = pageFilter.data('template-types'),
- options = [
- {flag: 'Modular', key: 'Modular', cat: 'mode'},
- {flag: 'Visible', key: 'Visible', cat: 'mode'},
- {flag: 'Routable', key: 'Routable', cat: 'mode'},
- {flag: 'Published', key: 'Published', cat: 'mode'},
- {flag: 'Non-Modular', key: 'NonModular', cat: 'mode'},
- {flag: 'Non-Visible', key: 'NonVisible', cat: 'mode'},
- {flag: 'Non-Routable', key: 'NonRoutable', cat: 'mode'},
- {flag: 'Non-Published', key: 'NonPublished', cat: 'mode'},
- ];
- if (pageFilter && pageTypes) {
- jQuery.each(pageTypes, function(key, name){
- options.push({flag: name, key: key, cat: 'type'});
- })
- pageFilter.selectize({
- maxItems: null,
- valueField: 'key',
- labelField: 'flag',
- searchField: ['flag', 'key'],
- options: options,
- optgroups: [
- {id: 'mode', name: 'Page Modes'},
- {id: 'type', name: 'Page Types'},
- ],
- optgroupField: 'cat',
- optgroupLabelField: 'name',
- optgroupValueField: 'id',
- optgroupOrder: ['mode', 'type'],
- plugins: ['optgroup_columns']
- });
- }
- var childrenToggles = $('[data-toggle="children"]'),
- storage = sessionStorage.getItem('grav:admin:pages'),
- collapseAll = function(store) {
- childrenToggles.each(function(i, element){
- var icon = $(element).find('.page-icon'),
- open = icon.hasClass('children-open'),
- key = $(element).closest('[data-nav-id]').data('nav-id'),
- children = $(element).closest('li.page-item').find('ul:first');
- if (open) {
- children.hide();
- if (store) delete storage[key];
- icon.removeClass('children-open').addClass('children-closed');
- }
- if (store) sessionStorage.setItem('grav:admin:pages', JSON.stringify(storage));
- });
- },
- expandAll = function(store) {
- childrenToggles.each(function(i, element){
- var icon = $(element).find('.page-icon'),
- open = icon.hasClass('children-open'),
- key = $(element).closest('[data-nav-id]').data('nav-id'),
- children = $(element).closest('li.page-item').find('ul:first');
- if (!open) {
- children.show();
- if (store) storage[key] = 1;
- icon.removeClass('children-closed').addClass('children-open');
- }
- if (store) sessionStorage.setItem('grav:admin:pages', JSON.stringify(storage));
- });
- },
- restoreStates = function() {
- collapseAll();
- for (var key in storage) {
- var element = $('[data-nav-id="' + key + '"]'),
- icon = element.find('.page-icon').first(),
- open = icon.hasClass('children-open'),
- children = element.closest('li.page-item').find('ul:first');
- children.show();
- icon.removeClass('children-closed').addClass('children-open');
- }
- };
- if (!storage) {
- sessionStorage.setItem('grav:admin:pages', (storage = '{}'));
- }
- storage = JSON.parse(storage);
- restoreStates();
- var startFilterPages = function () {
- var task = 'task' + GravAdmin.config.param_sep;
- $('input[name="page-search"]').focus();
- var flags = $('input[name="page-filter"]').val(),
- query = $('input[name="page-search"]').val();
- if (!flags.length && !query.length) {
- GravAjax.jqxhr.abort();
- return finishFilterPages([], true);
- }
- GravAjax({
- dataType: 'json',
- method: 'POST',
- url: GravAdmin.config.base_url_relative + '/pages-filter.json/' + task + 'filterPages',
- data: {
- flags: flags,
- query: query
- },
- toastErrors: true,
- success: function (result, status) {
- finishFilterPages(result.results);
- }
- });
- };
- var finishFilterPages = function (pages, reset) {
- var items = $('[data-nav-id]');
- items.removeClass('search-match');
- if (reset) {
- items.addClass('search-match');
- restoreStates();
- } else {
- pages.forEach(function (id) {
- var match = items.filter('[data-nav-id="' + id + '"]'),
- parents = match.parents('[data-nav-id]');
- match.addClass('search-match');
- match.find('[data-nav-id]').addClass('search-match');
- parents.addClass('search-match');
- parents.find('[data-toggle="children"]').each(function(index, element){
- var icon = $(this).find('.page-icon'),
- open = icon.hasClass('children-open'),
- children = $(this).closest('li.page-item').find('ul:first');
- if (!open) {
- children.show();
- icon.removeClass('children-closed').addClass('children-open');
- }
- });
- });
- }
- items.each(function (key, item) {
- if ($(item).hasClass('search-match')) {
- $(item).show();
- } else {
- $(item).hide();
- }
- });
- };
- // selectize
- $('input[name="page-search"]').on('input', startFilterPages);
- $('input[name="page-filter"]').on('change', startFilterPages);
- // auto generate folder based on title
- // on user input on folder, autogeneration stops
- // if user empties the folder, autogeneration restarts
- $('input[name="folder"]').on('input', function(){
- $(this).data('user-custom-folder', true);
- if (!$(this).val()) $(this).data('user-custom-folder', false);
- });
- $('input[name="title"]').on('input', function(e){
- if (!$('input[name="folder"]').data('user-custom-folder')) {
- folder = $.slugify($(this).val());
- $('input[name="folder"]').val(folder);
- }
- });
- $('#slug-target').slugify('#slug-source');
- $('input[name="folder"]').on('input', function(e){
- var start = this.selectionStart,
- end = this.selectionEnd;
- value = $(this).val().toLowerCase().replace(/\s/g, '-').replace(/[^a-z0-9_\-]/g, '');
- $(this).val(value);
- // restore cursor position
- this.setSelectionRange(start, end);
- });
- childrenToggles.on('click', function () {
- var icon = $(this).find('.page-icon'),
- open = icon.hasClass('children-open'),
- key = $(this).closest('[data-nav-id]').data('nav-id'),
- children = $(this).closest('li.page-item').find('ul:first');
- if (open) {
- children.hide();
- delete storage[key];
- icon.removeClass('children-open').addClass('children-closed');
- } else {
- children.show();
- storage[key] = true;
- icon.removeClass('children-closed').addClass('children-open');
- }
- sessionStorage.setItem('grav:admin:pages', JSON.stringify(storage));
- });
- $('[data-page-toggleall]').on('click', function() {
- var state = $(this).data('page-toggleall');
- if (state == 'collapse') collapseAll(true);
- else expandAll(true);
- });
- $('#admin-main button').on('click', function(){
- $(window).off('beforeunload');
- });
- $('[data-remodal-id] form').on('submit', function(){
- $(window).off('beforeunload');
- });
- $("#admin-mode-toggle input[name=mode-switch]").on('change', function(e){
- var value = $(this).val(),
- uri = $(this).data('leave-url');
- if (root.currentValues == getState()) {
- setTimeout(function(){
- window.location.href = uri;
- }, 200)
- return true;
- }
- e.preventDefault();
- var confirm = $.remodal.lookup[$('[data-remodal-id=changes]').data('remodal')],
- buttons = $('[data-remodal-id=changes] a.button'),
- action;
- buttons.on('click', function(e){
- e.preventDefault();
- action = $(this).data('leave-action');
- buttons.off('click');
- confirm.close();
- if (action == 'continue') {
- $(window).off('beforeunload');
- window.location.href = $("#admin-mode-toggle input[name=mode-switch]:checked").data('leave-url');
- } else {
- $('input[name=mode-switch][checked]').prop('checked', true);
- }
- });
- confirm.open();
- });
- $('a[href]:not([href^=#])').on('click', function(e){
- if (root.currentValues != getState()){
- e.preventDefault();
- clickedLink = $(this).attr('href');
- var confirm = $.remodal.lookup[$('[data-remodal-id=changes]').data('remodal')],
- buttons = $('[data-remodal-id=changes] a.button'),
- action;
- buttons.on('click', function(e){
- e.preventDefault();
- action = $(this).data('leave-action');
- buttons.off('click');
- confirm.close();
- if (action == 'continue') {
- $(window).off('beforeunload');
- window.location.href = clickedLink;
- }
- });
- confirm.open();
- }
- });
- // deletion
- $('[data-remodal-target="delete"]').on('click', function(){
- var okdelete = $('[data-remodal-id=delete] a.button');
- okdelete.data('delete-action', $(this).data('delete-url'));
- });
- $('[data-delete-action]').on('click', function(){
- var confirm = $.remodal.lookup[$('[data-remodal-id=delete]').data('remodal')],
- okdelete = $(this).data('delete-action');
- window.location.href = okdelete;
- confirm.close();
- });
- $(window).on('beforeunload', function(){
- if (root.currentValues != getState()){
- return "You have made changes on this page that you have not yet confirmed. If you navigate away from this page you will lose your unsaved changes";
- }
- });
- // Move dropdown sync (on dropdown change)
- /*$('body').on('change', '[data-page-move] select', function(){
- var route = jQuery('form#blueprints').first().find('select[name="route"]'),
- value = $(this).val();
- if (route.length && route.val() !== value) {
- route.val(value);
- route.data('selectize').setValue(value);
- }
- });*/
- // Move dropdown sync (on continue)
- $('[data-page-move] button').on('click', function(){
- var route = jQuery('form#blueprints').first().find('select[name="route"]'),
- value = $('[data-page-move] select').val();
- if (route.length && route.val() !== value) {
- var selectize = route.data('selectize');
- route.val(value);
- if (selectize) selectize.setValue(value);
- }
- });
- });
|