Skip to content

Commit

Permalink
suggest an edit
Browse files Browse the repository at this point in the history
  • Loading branch information
vikram-redhat authored and openshift-cherrypick-robot committed Jun 22, 2020
1 parent 2a4bd23 commit d777207
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 19 deletions.
127 changes: 111 additions & 16 deletions _javascripts/page-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,121 @@ function versionSelector(list) {

}

// sets the current version in the drop down and sets up suggest an edit options
function selectVersion(currentVersion) {

// currentVersion = "3.11"; // for testing

// set the version selector to what the current version is
var el = document.getElementById("version-selector");
if(el) {
el.value = currentVersion;
}
// alert(currentVersion);

// in enterprise branch 4, we have modules and this is an attempt to load the
// modules by double clicking on them.
// if(currentVersion.charAt(0) === "4") {
// var element = document.getElementsByTagName('h2');
// Object.entries(element).map(( object ) => {
// object[1].addEventListener("dblclick", function() {
// alert(this.id);
// alert(this.id.split("_", 1)[0] + ".adoc");
// var fn = this.id.split("_", 1)[0] + ".adoc";
// window.open("https://github.com/openshift/openshift-docs/tree/enterprise-" +
// currentVersion + "/modules/" + fn, "_new");
// });
// });
// }

// the rest creates an suggest an edit element for h1 and h2 elements

// only enabled at the moment on the 3.11 docs
if(currentVersion != "3.11") return;

var is3 = (currentVersion.charAt(0) == 3);
var is4 = (currentVersion.charAt(0) == 4);

// in version 4 and version 3 books are put together differently. In 3,
// the WYSIWYG (mostly) and there are not many includes. In 4, everything
// (mostly) is an include and the wrapper is just an assembly.

// in version 3, there are generally no modules, and the page you are on, is
// the page you will edit, so the logic is a bit different.

// there is always just one h1 whether you are on version 4 or 3.
// In 4, this is the main assembly, in 3, this is the file to edit.
// in version 4 it assumes that the h2 section's id is correctly named as per the file that it resides in. This is the convention.

// we start with adding suggest an edit to the main assembly/file
var h1s = document.getElementsByTagName('h1');
var h1 = h1s[0]; // there is only one ever

// main file to edit is the file path after the version to the html at
// the end.
// Example: https://docs.openshift.com/container-platform/4.4/updating/updating-cluster-between-minor.html
// file path is updating/updating-cluster-between-minor.adoc

mainFileToEdit =
window.location.pathname.substring(
window.location.pathname.lastIndexOf(currentVersion) +
currentVersion.length, window.location.pathname.length - 4);

// rest api is put together automatically, so ignore
if(mainFileToEdit.includes("rest_api")) return;

var fn = mainFileToEdit + "adoc"; // add adoc to file name

var message = "message=[Suggested Edit] for " + fn + "' target='_new' id='" + fn + "' style='font-size: x-small; display: inline; visibility: hidden'>Suggest an edit</a>";

// in 4, edit the file in master, so it can cped to the right places. In 3,
// edit in the branch
h1.innerHTML += "&nbsp;<a href='https://github.com/openshift/openshift-docs/edit/" + (is4? "master" : ("enterprise-" + currentVersion)) + "/" + fn + "?" + message;

// add mouseover and out to the h1 tag to show or hide the link
// unlike the links added to h2, here it is at [0], the only 'a' tag
// added
h1.addEventListener("mouseover", function() {
this.getElementsByTagName('a')[0].style.visibility = "visible";
});

h1.addEventListener("mouseout", function() {
this.getElementsByTagName('a')[0].style.visibility = "hidden";
});


if(is4) { // in version 4 also allow to edit subsections which are modules

// all h2 elements map to modules
var h2s = document.getElementsByTagName('h2');

var i;
for (i = 0; i < h2s.length; i++) {

var h2 = h2s[i];
var splitFile = h2.id.split("_", 1)[0]; // file ids are of the format: filename_context
if(splitFile === h2.id) { continue; } // nothing was found, don't create an edit file link

// this overwrites the global fn for h1 from earlier
var fn = splitFile + ".adoc"; // add adoc to file name

// we are going to allow the PR to open against the master branch
h2.innerHTML += "&nbsp;<a href='https://github.com/openshift/openshift-docs/edit/master/modules/" + fn + "?" + "message=[Suggested Edit] for " + fn + "' target='_new' id='" + fn + "' style='font-size: x-small; display: inline; visibility: hidden'>Suggest an edit</a>";

// add mouseover and out to the h2 tag to show or hide the link
// in 4, the h2 also has an 'a' tag already, so the tag we are looking for
// here is the second one ([1] and not [0])
h2.addEventListener("mouseover", function() {
this.getElementsByTagName('a')[1].style.visibility = "visible";
});

h2.addEventListener("mouseout", function() {
this.getElementsByTagName('a')[1].style.visibility = "hidden";
});
}
}





// Object.entries(element).map(( object ) => {
// object[1].innerHTML += "&nbsp;&nbsp;<font size='-2'><a href=''>Edit this section</a></font>";
// var fn = this.id.split("_", 1)[0] + ".adoc";
// alert(fn);
// object[1].addEventListener("dblclick", function() {
// // alert(this.id);
// // alert(this.id.split("_", 1)[0] + ".adoc");
// var fn = this.id.split("_", 1)[0] + ".adoc";
// // window.open("https://github.com/openshift/openshift-docs/edit/enterprise-" +
// // currentVersion + "/modules/" + fn, "_new");
// window.open("https://github.com/openshift/openshift-docs/edit/master/modules/" + fn, "_new");
// });
// });
// }

}
6 changes: 3 additions & 3 deletions _templates/_page_openshift.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
unless (unsupported_versions.include? version)
%>
/
<a href="https://github.com/openshift/openshift-docs/issues/new?title=<%= (distro_key == "openshift-enterprise") ? "[enterprise-#{version}] Edit suggested in file #{repo_path}" :
((distro_key == "openshift-dedicated") ? "[dedicated] Edit suggested in file #{repo_path}" : "[online] Edit suggested in file #{repo_path}") %>">
Suggest an edit
<a href="https://github.com/openshift/openshift-docs/issues/new?title=<%= (distro_key == "openshift-enterprise") ? "[enterprise-#{version}] Issue in file #{repo_path}" :
((distro_key == "openshift-dedicated") ? "[dedicated] Issue in file #{repo_path}" : "[online] Issue in file #{repo_path}") %>">
Open an issue
</a>
<% end %>
</span>
Expand Down

0 comments on commit d777207

Please sign in to comment.