#10 Refactored change_filename_base()

open
nobody
5
2011-04-05
2011-04-05
Anonymous
No

Hi there,

FYI: I refactored change_filename_base() so that it (somewhat) properly rebases the source code links in the document.

function change_filename_base() {
var new_base_dir = document.getElementById('new_base_dir').value;
new_base_dir = new_base_dir.replace(/\\/g, "/");
new_base_dir = new_base_dir.replace(/\/+/g, "/");
new_base_dir = new_base_dir.replace(/\/$/g, "");

var orig_base_dir = document.getElementById('base_dir').value.toLowerCase();
orig_base_dir = orig_base_dir.replace(/\\/g, "/");
orig_base_dir = orig_base_dir.replace(/\/+/g, "/");
orig_base_dir = orig_base_dir.replace(/\/$/g, "");

var tags = document.getElementsByTagName("a");
for (var i=0; i<tags.length; i++) {
sourceCodeLink = tags[i].getAttribute("source_code_link");
if ( sourceCodeLink && sourceCodeLink == "true" ) {
var href = "";
// Construct into href the path elements from new_base_dir.
var baseParts = new_base_dir.split('/');
for( var baseIndex=0; baseIndex<baseParts.length; baseIndex++ ) {
var part = baseParts[baseIndex];
if(part.length>0) {
href+='/'+part;
}
}
// Get the title-attribute. It seems to contain the file's path.
var title = tags[i].getAttribute("title");
var titleParts = [];
if( title ) {
titleParts = title.replace(/\\/g, "/").split('/');
}
// Construct into href the path elements from title.
for( var titleIndex=0; titleIndex<titleParts.length; titleIndex++ ) {
var part = titleParts[titleIndex];
if(part.length>0) {
href+='/'+part;
}
}
// Prefix the new href with 'file://'.
tags[i].href = "file://"+href;
}
}
document.getElementById('base_dir').value = new_base_dir;
document.getElementById('new_base_dir').value = new_base_dir;
hide_popups();
}
--
jussi

Discussion


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks