Files
Grav-Admin-Plugin/themes/grav/js/pages-all.js

330 lines
11 KiB
JavaScript
Raw Normal View History

2014-09-18 17:58:47 -06:00
$(function(){
var root = window || {};
root = root.GravJS = root.GravJS || {};
2014-09-18 17:58:47 -06:00
//Make it global because used by ./forms/form.js
root.currentValues = getState();
var clickedLink;
2015-07-31 14:32:49 -07:00
2014-09-18 17:58:47 -06:00
// 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'}
];
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']
2014-09-18 17:58:47 -06:00
});
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);
2014-09-18 17:58:47 -06:00
// 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);
});
2014-09-18 17:58:47 -06:00
$('input[name="title"]').on('input', function(e){
if (!$('input[name="folder"]').data('user-custom-folder')) {
folder = $.slugify($(this).val());
2014-09-18 17:58:47 -06:00
$('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);
});
2014-09-18 17:58:47 -06:00
$('#admin-main button').on('click', function(){
$(window).off('beforeunload');
});
$('[data-remodal-id] form').on('submit', function(){
$(window).off('beforeunload');
});
2014-09-18 17:58:47 -06:00
$("#admin-mode-toggle input[name=mode-switch]").on('change', function(e){
2014-09-18 17:35:05 -07:00
var value = $(this).val(),
uri = $(this).data('leave-url');
2014-09-18 17:58:47 -06:00
if (root.currentValues == getState()) {
2014-09-18 17:58:47 -06:00
setTimeout(function(){
2014-09-18 17:35:05 -07:00
window.location.href = uri;
2014-09-18 17:58:47 -06:00
}, 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');
2014-09-18 17:58:47 -06:00
} else {
$('input[name=mode-switch][checked]').prop('checked', true);
}
});
confirm.open();
});
$('a[href]:not([href^=#])').on('click', function(e){
if (root.currentValues != getState()){
2014-09-18 17:58:47 -06:00
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();
}
});
2014-09-30 15:49:28 -07:00
// 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();
});
2014-09-18 17:58:47 -06:00
$(window).on('beforeunload', function(){
if (root.currentValues != getState()){
2014-09-18 17:58:47 -06:00
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";
}
});
2015-08-03 11:26:58 -07:00
// Move dropdown sync (on dropdown change)
/*$('body').on('change', '[data-page-move] select', function(){
2015-08-03 11:26:58 -07:00
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);
}
2015-08-03 11:26:58 -07:00
});
2014-09-18 17:58:47 -06:00
});