Introduce JS variant of path helpers

This commit is contained in:
Dawa Ometto
2018-12-10 19:54:21 +01:00
parent 253ba89f90
commit 04501c205a
4 changed files with 73 additions and 95 deletions
@@ -151,7 +151,7 @@
// get form fields // get form fields
var oldAction = $('#gollum-editor form').attr('action'); var oldAction = $('#gollum-editor form').attr('action');
var $form = $($('#gollum-editor form').get(0)); var $form = $($('#gollum-editor form').get(0));
$form.attr('action', this.href || '<%= preview_path %>'); $form.attr('action', this.href || routePath('preview'));
$form.attr('target', '_blank'); $form.attr('target', '_blank');
var paths = window.location.pathname.split('/'); var paths = window.location.pathname.split('/');
$form.attr('page', paths[ paths.length - 1 ] || '') $form.attr('page', paths[ paths.length - 1 ] || '')
@@ -209,7 +209,7 @@
formData.append('file', file); formData.append('file', file);
$.ajax({ $.ajax({
url: '<%= upload_file_path %>', url: routePath('upload_file'),
data: formData, data: formData,
cache: false, cache: false,
contentType: false, contentType: false,
@@ -317,12 +317,6 @@
} }
if ( !LanguageDefinition.isLoadedFor(name) ) { if ( !LanguageDefinition.isLoadedFor(name) ) {
LanguageDefinition._ACTIVE_LANG = null; LanguageDefinition._ACTIVE_LANG = null;
LanguageDefinition.loadFor( name, function(x, t) {
if ( t != 'success' ) {
debug('Failed to load language definition for ' + name);
// well, fake it and turn everything off for this one
LanguageDefinition.define( name, {} );
}
// update features that rely on the language definition // update features that rely on the language definition
if ( EditorHas.functionBar() ) { if ( EditorHas.functionBar() ) {
@@ -359,7 +353,6 @@
return false; return false;
}); });
} );
} else { } else {
LanguageDefinition._ACTIVE_LANG = name; LanguageDefinition._ACTIVE_LANG = name;
FunctionBar.refresh(); FunctionBar.refresh();
@@ -405,43 +398,6 @@
return null; return null;
}, },
/**
* loadFor
* Asynchronously loads a definition file for the current markup.
* Definition files are necessary to use the code editor.
*
* @param string markup_name The markup name you want to load
* @return void
*/
loadFor: function( markup_name, on_complete ) {
// Keep us from hitting 404s on our site, check the definition blacklist
if ( ActiveOptions.NoDefinitionsFor.length ) {
for ( var i=0; i < ActiveOptions.NoDefinitionsFor.length; i++ ) {
if ( markup_name == ActiveOptions.NoDefinitionsFor[i] ) {
// we don't have this. get out.
if ( typeof on_complete == 'function' ) {
on_complete( null, 'error' );
return;
}
}
}
}
// attempt to load the definition for this language
var script_uri = '<%= assets_path %>/editor/langs/' + markup_name + '.js';
$.ajax({
url: script_uri,
dataType: 'script',
complete: function( xhr, textStatus ) {
if ( typeof on_complete == 'function' ) {
on_complete( xhr, textStatus );
}
}
});
},
/** /**
* isLoadedFor * isLoadedFor
* Checks to see if a definition file has been loaded for the * Checks to see if a definition file has been loaded for the
@@ -101,7 +101,7 @@
var id = fieldAttributes.id || 'upload'; var id = fieldAttributes.id || 'upload';
var name = fieldAttributes.name || 'file'; var name = fieldAttributes.name || 'file';
var action = fieldAttributes.action || '<%= upload_file_path %>'; var action = fieldAttributes.action || routePath('upload_file');
html += '<form method=post enctype="multipart/form-data" ' + html += '<form method=post enctype="multipart/form-data" ' +
'action="' + action + '" ' + 'id="' + id + '">'; 'action="' + action + '" ' + 'id="' + id + '">';
@@ -1,4 +1,27 @@
// Helpers // Helpers
// Get path for named route, prefixing baseUrl if necessary
// Uses the route definitions in /lib/gollum/views/helpers.rb.
// For example, routePath('delete') is equivalent to 'delete_path' in the mustache templates.
function routePath(name){
var routes = $.parseJSON('<%= routes_to_json %>')
path = routes[name]
if (baseUrl == undefined ) {
console.log('Gollum error: baseUrl undefined')
} else if (path == undefined ) {
console.log('Could not find route with name: ' + name)
} else {
if ( baseUrl == "") {
return path;
} else if (baseUrl.charAt(baseUrl.length -1) == '/') {
result = baseUrl + path;
} else {
result = baseUrl + '/' + path;
}
return result.replace(/\/{2}/g, '/')
}
}
function pageName(){ function pageName(){
// "my/dir/file.md" => "file" // "my/dir/file.md" => "file"
if (typeof(pageFullPath) == 'undefined') { if (typeof(pageFullPath) == 'undefined') {
@@ -48,7 +71,7 @@ $(document).ready(function() {
$('#delete-link').click( function(e) { $('#delete-link').click( function(e) {
var ok = confirm($(this).data('confirm')); var ok = confirm($(this).data('confirm'));
if ( ok ) { if ( ok ) {
$.post('<%=delete_path %>/' + pageFullPath, $.post(routePath('delete') + '/' + pageFullPath,
{}, {},
function (result) { function (result) {
// page successfully deleted, return to landing page // page successfully deleted, return to landing page
@@ -63,7 +86,7 @@ $(document).ready(function() {
var ok = confirm($(this).data('confirm')); var ok = confirm($(this).data('confirm'));
if ( ok ) { if ( ok ) {
var element = $(this); var element = $(this);
$.post('<%=delete_path %>' + $(this).data('file-path'), $.post(routePath('delete') + '/' + $(this).data('file-path'),
{}, {},
function (result) { function (result) {
// file successfully deleted, stay on overview but remove element from DOM // file successfully deleted, stay on overview but remove element from DOM
@@ -183,7 +206,7 @@ $(document).ready(function() {
{ {
type: 'file', type: 'file',
context: 'Your uploaded file will be accessible at<br>/'+uploadDest+'/[filename]', context: 'Your uploaded file will be accessible at<br>/'+uploadDest+'/[filename]',
action: '<%= upload_file_path %>' action: routePath('upload_file')
} }
], ],
OK: function( res ) { OK: function( res ) {
@@ -247,14 +270,7 @@ $(document).ready(function() {
// In the pages view, pageFullPath isn't defined. // In the pages view, pageFullPath isn't defined.
// The new button will still expect a value however. // The new button will still expect a value however.
// So we try to figure one out from window.location // So we try to figure one out from window.location
path = window.location.pathname.substr(1) path = window.location.pathname.replace(routePath('pages'), '')
// Remove the page viewer part of the url.
<%
pages_path_uri = pages_path
pages_path_uri = pages_path_uri[1..-1] if pages_path_uri[0] == '/'
pages_path_uri = pages_path_uri.gsub('/','\/')
%>
path = path.replace(/^<%= pages_path_uri %>\/?/,'')
// For consistency remove the trailing / // For consistency remove the trailing /
path = path.replace(/\/$/,'') path = path.replace(/\/$/,'')
} }
@@ -285,7 +301,7 @@ $(document).ready(function() {
for( var i=0; i < name_parts.length; i++ ){ for( var i=0; i < name_parts.length; i++ ){
name_encoded.push(encodeURIComponent(name_parts[i])); name_encoded.push(encodeURIComponent(name_parts[i]));
} }
window.location = '<%= create_path %>' + name_encoded.join('/'); window.location = routePath('create') + name_encoded.join('/');
} }
}); });
}); });
@@ -340,7 +356,7 @@ $(document).ready(function() {
if( $("#last-edit").length ) { if( $("#last-edit").length ) {
$("#page-info-toggle").click ( function () { $("#page-info-toggle").click ( function () {
$.ajax({ $.ajax({
url: '<%= last_commit_info_path %>', url: routePath('last_commit_info'),
data: {path: $("#page-info-toggle").data('pagepath')}, data: {path: $("#page-info-toggle").data('pagepath')},
success: function ( data ) { success: function ( data ) {
$("#last-edit").html('Last edited by <b>' + data.author + '</b>, ' + data.date); $("#last-edit").html('Last edited by <b>' + data.author + '</b>, ' + data.date);
+8 -2
View File
@@ -1,4 +1,4 @@
require 'yaml' require 'json'
module Precious module Precious
module Views module Views
@@ -26,15 +26,21 @@ module Precious
if path.respond_to?(:keys) if path.respond_to?(:keys)
self.parse_routes(path, "#{prefix}/#{name}") self.parse_routes(path, "#{prefix}/#{name}")
else else
route_path = "#{prefix}/#{path}"
@@route_methods[name.to_s] = route_path
define_method :"#{name.to_s}_path" do define_method :"#{name.to_s}_path" do
"#{base_url}/#{prefix}/#{path}".gsub(/\/{2,}/, '/') # remove double slashes "#{base_url}/#{route_path}".gsub(/\/{2,}/, '/') # remove double slashes
end end
end end
end end
end end
def self.included(base) def self.included(base)
@@route_methods = {}
self.parse_routes(ROUTES) self.parse_routes(ROUTES)
define_method :routes_to_json do
@@route_methods.to_json
end
end end
end end