From 04501c205a05bf321fc21d1ee1f0625a7e2c63af Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 10 Dec 2018 19:54:21 +0100 Subject: [PATCH] Introduce JS variant of path helpers --- .../javascript/editor/gollum.editor.js.erb | 114 ++++++------------ .../gollum/javascript/gollum.dialog.js.erb | 2 +- .../public/gollum/javascript/gollum.js.erb | 42 +++++-- lib/gollum/views/helpers.rb | 10 +- 4 files changed, 73 insertions(+), 95 deletions(-) diff --git a/lib/gollum/public/gollum/javascript/editor/gollum.editor.js.erb b/lib/gollum/public/gollum/javascript/editor/gollum.editor.js.erb index f9a9af65..6b408e8a 100755 --- a/lib/gollum/public/gollum/javascript/editor/gollum.editor.js.erb +++ b/lib/gollum/public/gollum/javascript/editor/gollum.editor.js.erb @@ -151,7 +151,7 @@ // get form fields var oldAction = $('#gollum-editor form').attr('action'); 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'); var paths = window.location.pathname.split('/'); $form.attr('page', paths[ paths.length - 1 ] || '') @@ -209,7 +209,7 @@ formData.append('file', file); $.ajax({ - url: '<%= upload_file_path %>', + url: routePath('upload_file'), data: formData, cache: false, contentType: false, @@ -317,49 +317,42 @@ } if ( !LanguageDefinition.isLoadedFor(name) ) { 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 + if ( EditorHas.functionBar() ) { + FunctionBar.refresh(); + } + + if ( LanguageDefinition.isValid() && EditorHas.formatSelector() ) { + FormatSelector.updateSelected(); + } + + if(LanguageDefinition.getHookFunctionFor("activate")) { + LanguageDefinition.getHookFunctionFor("activate")(); + } + + function hotkey( e, cmd ) { + e.preventDefault(); + + var def = LanguageDefinition.getDefinitionFor( cmd ); + if ( typeof def == 'object' ) { + FunctionBar.executeAction( def ); } + // Prevent bubbling of hotkey. + return false; + } - // update features that rely on the language definition - if ( EditorHas.functionBar() ) { - FunctionBar.refresh(); - } + Mousetrap.bind('mod+1', function( e ){ hotkey( e, 'function-h1' ); }); + Mousetrap.bind('mod+2', function( e ){ hotkey( e, 'function-h2' ); }); + Mousetrap.bind('mod+3', function( e ){ hotkey( e, 'function-h3' ); }); + Mousetrap.bind('mod+b', function( e ){ hotkey( e, 'function-bold' ); }); + Mousetrap.bind('mod+i', function( e ){ hotkey( e, 'function-italic' ); }); + Mousetrap.bind('mod+s', function( e ){ + e.preventDefault(); + $("#gollum-editor-submit").trigger("click"); + return false; + }); - if ( LanguageDefinition.isValid() && EditorHas.formatSelector() ) { - FormatSelector.updateSelected(); - } - - if(LanguageDefinition.getHookFunctionFor("activate")) { - LanguageDefinition.getHookFunctionFor("activate")(); - } - - function hotkey( e, cmd ) { - e.preventDefault(); - - var def = LanguageDefinition.getDefinitionFor( cmd ); - if ( typeof def == 'object' ) { - FunctionBar.executeAction( def ); - } - // Prevent bubbling of hotkey. - return false; - } - - Mousetrap.bind('mod+1', function( e ){ hotkey( e, 'function-h1' ); }); - Mousetrap.bind('mod+2', function( e ){ hotkey( e, 'function-h2' ); }); - Mousetrap.bind('mod+3', function( e ){ hotkey( e, 'function-h3' ); }); - Mousetrap.bind('mod+b', function( e ){ hotkey( e, 'function-bold' ); }); - Mousetrap.bind('mod+i', function( e ){ hotkey( e, 'function-italic' ); }); - Mousetrap.bind('mod+s', function( e ){ - e.preventDefault(); - $("#gollum-editor-submit").trigger("click"); - return false; - }); - - } ); } else { LanguageDefinition._ACTIVE_LANG = name; FunctionBar.refresh(); @@ -405,43 +398,6 @@ 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 * Checks to see if a definition file has been loaded for the diff --git a/lib/gollum/public/gollum/javascript/gollum.dialog.js.erb b/lib/gollum/public/gollum/javascript/gollum.dialog.js.erb index cd9a43c3..070d804c 100755 --- a/lib/gollum/public/gollum/javascript/gollum.dialog.js.erb +++ b/lib/gollum/public/gollum/javascript/gollum.dialog.js.erb @@ -101,7 +101,7 @@ var id = fieldAttributes.id || 'upload'; var name = fieldAttributes.name || 'file'; - var action = fieldAttributes.action || '<%= upload_file_path %>'; + var action = fieldAttributes.action || routePath('upload_file'); html += '
'; diff --git a/lib/gollum/public/gollum/javascript/gollum.js.erb b/lib/gollum/public/gollum/javascript/gollum.js.erb index 173bf27d..3d10bc7b 100755 --- a/lib/gollum/public/gollum/javascript/gollum.js.erb +++ b/lib/gollum/public/gollum/javascript/gollum.js.erb @@ -1,4 +1,27 @@ // 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(){ // "my/dir/file.md" => "file" if (typeof(pageFullPath) == 'undefined') { @@ -48,7 +71,7 @@ $(document).ready(function() { $('#delete-link').click( function(e) { var ok = confirm($(this).data('confirm')); if ( ok ) { - $.post('<%=delete_path %>/' + pageFullPath, + $.post(routePath('delete') + '/' + pageFullPath, {}, function (result) { // page successfully deleted, return to landing page @@ -63,7 +86,7 @@ $(document).ready(function() { var ok = confirm($(this).data('confirm')); if ( ok ) { var element = $(this); - $.post('<%=delete_path %>' + $(this).data('file-path'), + $.post(routePath('delete') + '/' + $(this).data('file-path'), {}, function (result) { // file successfully deleted, stay on overview but remove element from DOM @@ -183,7 +206,7 @@ $(document).ready(function() { { type: 'file', context: 'Your uploaded file will be accessible at
/'+uploadDest+'/[filename]', - action: '<%= upload_file_path %>' + action: routePath('upload_file') } ], OK: function( res ) { @@ -247,14 +270,7 @@ $(document).ready(function() { // In the pages view, pageFullPath isn't defined. // The new button will still expect a value however. // So we try to figure one out from window.location - path = window.location.pathname.substr(1) - // 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 %>\/?/,'') + path = window.location.pathname.replace(routePath('pages'), '') // For consistency remove the trailing / path = path.replace(/\/$/,'') } @@ -285,7 +301,7 @@ $(document).ready(function() { for( var i=0; i < name_parts.length; 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 ) { $("#page-info-toggle").click ( function () { $.ajax({ - url: '<%= last_commit_info_path %>', + url: routePath('last_commit_info'), data: {path: $("#page-info-toggle").data('pagepath')}, success: function ( data ) { $("#last-edit").html('Last edited by ' + data.author + ', ' + data.date); diff --git a/lib/gollum/views/helpers.rb b/lib/gollum/views/helpers.rb index 942df139..b328914d 100644 --- a/lib/gollum/views/helpers.rb +++ b/lib/gollum/views/helpers.rb @@ -1,4 +1,4 @@ -require 'yaml' +require 'json' module Precious module Views @@ -26,15 +26,21 @@ module Precious if path.respond_to?(:keys) self.parse_routes(path, "#{prefix}/#{name}") else + route_path = "#{prefix}/#{path}" + @@route_methods[name.to_s] = route_path 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 def self.included(base) + @@route_methods = {} self.parse_routes(ROUTES) + define_method :routes_to_json do + @@route_methods.to_json + end end end