Skip to content

Commit

Permalink
Merge pull request #326 from matematikk-mooc/KURSP-902-uob-tabs
Browse files Browse the repository at this point in the history
KURSP-902: uob-tabs and mark-as-done button
  • Loading branch information
manilpit authored Oct 24, 2023
2 parents 1376c5b + f438aeb commit e44eefd
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 2,710 deletions.
2,515 changes: 0 additions & 2,515 deletions Find Results

This file was deleted.

91 changes: 0 additions & 91 deletions login/common.css

This file was deleted.

104 changes: 0 additions & 104 deletions src/js/3party/uob7.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,56 +132,6 @@ export default (function() {
$table.remove();
}

// ================================================================================
// Tabs (Part 1/2)
//
// Convert up to 10 uob-tabs tables to format required for tabs.
// --------------------------------------------------------------------------------

strSetNum = 0;

for (i = 0; i < 10; i++) {
// Locate the next uob-tabs table.
$table = $content
.find("table")
.has("table > tbody > tr > td:contains([uob-tabs])")
.last();

// Break loop if no more tabs are to be displayed.
if ($table.length != 1) break;

// Convert table into a set of tabs.
$table.before("<div class='uob-tabs'><ul></ul></div>");
strSetNum++;

$table
.find("tbody:first > tr:gt(0) > td")
.each(function (_idx, _item) {
var strAnchor =
"set" + strSetNum + "tab" + (_idx - (_idx % 2)) / 2;

if ((_idx + 1) % 2) {
// Add list item for the tab label.
var strHTML =
'<li><a href="#' +
strAnchor +
'">' +
$(_item).text().trim() +
"</a></li>";
$table.prev().find("ul").first().append(strHTML);
}

if (_idx % 2) {
// Add div for the tab content.
$table.prev().append('<div id="' + strAnchor + '"></div>');
$("#" + strAnchor).append($(_item).contents());
}
});

// Remove original table from the DOM
$table.remove();
}

// ================================================================================
// Reveal (Part 1/2)
//
Expand Down Expand Up @@ -358,60 +308,6 @@ export default (function() {
}, 200);
});
}
// ================================================================================
// Tabs (Part 2/2)
//
// Tabs will be contained within elements with a uob-tabs class.
// --------------------------------------------------------------------------------

// Initialise tabs
var $tabs = $content.find(".uob-tabs");

if ($tabs.length > 0) {
$tabs.tabs({
active: 0,
collapsible: false,
heightStyle: "content",
beforeActivate: function (event, ui) {
ui.oldPanel.find(".hide_youtube_embed_link").click();
},
activate: function (event, ui) {
console.log("Tab activate");
// Let h5p iframes know we're ready!
var iframes = document.getElementsByTagName("iframe");
var ready = {
context: "h5p",
action: "ready",
};
var resize = {
context: "h5p",
action: "resize",
};

for (var i = 0; i < iframes.length; i++) {
if (iframes[i].src.indexOf("h5p") !== -1) {
iframes[i].contentWindow.postMessage(ready, "*");
iframes[i].contentWindow.postMessage(resize, "*");
}
}
},
beforeLoad: function (event, ui) {
console.log("Tab beforeLoad");
},
create: function (event, ui) {
console.log("Tab create");
},
load: function (event, ui) {
console.log("Tab load");
},
});
}

// ================================================================================
// Reveal (Part 2/2)
//
// The uob-reveal-button and uob-reveal-content classes are required for reveals.
// ................................................................................

// Initialise reveal contents.
var $revealBody = $content.find(".uob-reveal");
Expand Down
5 changes: 5 additions & 0 deletions src/js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import announcements from './modules/announcements.js';
import api from './api/api.js';
import coursePageButtons from './modules/coursePageButtons.js';
import courselist from './modules/courselist.js';
import coursepage from './modules/coursepage.js';
import coursesettings from './modules/coursesettings.js';
Expand All @@ -21,6 +22,7 @@ import pages from './modules/pages.js';
import privacyPolicy from './3party/privacypolicy.js';
import routes from './modules/routes.js';
import settings from './settings.js';
import tabs from './modules/tabs.js';
import tinyMCEEditor from './modules/tinyMCEEditor';
import uob from './3party/uob7.js';
import util from './modules/util.js';
Expand Down Expand Up @@ -542,6 +544,7 @@ jQuery(function($) {
greeting.enableDownloadDiplomaButtonIfNecessary); //This is the newest method which should replace the two old ones.

var courseId = api.getCurrentCourseId();
coursePageButtons.replaceMarkAsDone();

if ($("#kpas-lti-info").length ||
$(".kpas-lti-info").length ||
Expand Down Expand Up @@ -628,7 +631,9 @@ jQuery(function($) {
messagehandler.init();
uob.init();
nrk.init();
tabs.init();
infoboxes.init();

} catch (e) {
console.log(e);
}
Expand Down
69 changes: 69 additions & 0 deletions src/js/modules/coursePageButtons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import '../../vue/design/re-styles/buttons.scss';

export default (function () {
return {
replaceMarkAsDone: function () {

const originalMarkAsDoneButton = document.querySelector('#mark-as-done-checkbox');

if (!originalMarkAsDoneButton) {
return;
}

const notCompletedContent = `<span class="mark-done-labels"><img src="${SERVER}vector_images/markasdone.svg"/><p>Merk som ferdig</p></span>`;
const completedContent = `<span class="mark-done-labels"><img src="${SERVER}vector_images/markasdonecompleted.svg"/><p>Ferdig</p></span>`;

const attributes = Array.from(originalMarkAsDoneButton.attributes).reduce((acc, attr) => {
if (attr.name !== 'class') {
acc[attr.name] = attr.value;
}
return acc;
}, {});

let newMarkAsDoneButton = document.createElement('button');
newMarkAsDoneButton.classList.add('custom-button');
newMarkAsDoneButton.classList.add('custom-mark-as-done-checkbox');

for (const [attr, value] of Object.entries(attributes)) {
newMarkAsDoneButton.setAttribute(attr, value);
if(attr === 'data-is-checked' && value === 'true') {
newMarkAsDoneButton.classList.add('custom-mark-as-done-completed');
newMarkAsDoneButton.setAttribute('completed', 'true')
newMarkAsDoneButton.innerHTML = completedContent
}
else if(attr === 'data-is-checked' && value === 'false') {
newMarkAsDoneButton.innerHTML = notCompletedContent
}
}

newMarkAsDoneButton.onclick = ((event) => {
console.log(event)
if(newMarkAsDoneButton.getAttribute('completed') === 'true') {
newMarkAsDoneButton.setAttribute('completed', 'false');
newMarkAsDoneButton.classList.remove('custom-mark-as-done-completed');
newMarkAsDoneButton.innerHTML = notCompletedContent
}
else{
newMarkAsDoneButton.setAttribute('completed', 'true');
newMarkAsDoneButton.classList.add('custom-mark-as-done-completed');
newMarkAsDoneButton.innerHTML = completedContent

}
})
originalMarkAsDoneButton.parentNode.replaceChild(newMarkAsDoneButton, originalMarkAsDoneButton);

},


}

})();

//Rediger
// let editButton = document.getElementsByClassName("btn edit-wiki")[0];
//Publisert
// let publishButton = document.getElementsByClassName("btn btn-published")[0];
//Avpublisert
// let unpublishButton = document.getElementsByClassName("btn btn-unpublished")[0];
//Studentvisning
// const studentViewButton = document.getElementById('easy_student_view');
140 changes: 140 additions & 0 deletions src/js/modules/tabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import '../../vue/design/re-styles/tabs.scss';
export default (function () {

var strSetNum = 0;
function transformTabs(){
onElementRendered('#content .user_content.enhanced,#content .show-content.enhanced',
function($content) {
// Store references to the active tab and pane
var activeTab = null;
var activePane = null;

var tables = document.getElementsByTagName('table');

for (var i = 0; i < 10; i++) {
var table = null;
for (var j = 0; j < tables.length; j++) {
let cell = tables[j].querySelectorAll('tbody > tr > td')[0];
let celltext = cell.textContent.trim();
if (celltext.includes("[uob-tabs]")) {
table = tables[j];
}
}

if (!table) {
break;
}

var customSegments = document.createElement('div');
customSegments.className = 'custom-segments';
customSegments.setAttribute('id', 'custom-segments' + i);
var ul = document.createElement('ul');
var div = document.createElement('div');
div.className = 'custom-segments__tabHeaders';
ul.className = 'custom-segments__segments';
div.appendChild(ul);
customSegments.appendChild(div);
strSetNum++;

var tds = table.querySelectorAll('td');
for (var k = 1; k < tds.length; k++) {
var strAnchor = 'set' + strSetNum + 'tab' + Math.floor(k / 2);

if (k % 2 === 0) {
var li = document.createElement('li');
li.className = 'custom-segments__segment';
var a = document.createElement('a');
a.href = '#' + strAnchor;
a.innerHTML = tds[k].textContent;

// Add a click event listener to the tab
li.addEventListener('click', function (event) {
event.preventDefault();

var iframes = document.getElementsByTagName('iframe');
var ready = {
context: 'h5p',
action: 'ready'
};
var resize = {
context: 'h5p',
action: 'resize'
};

for (var i = 0; i < iframes.length; i++) {
if (iframes[i].src.indexOf('h5p') !== -1) {
iframes[i].contentWindow.postMessage(ready, '*');
iframes[i].contentWindow.postMessage(resize, '*');
};
}

// Deactivate the previously active tab and pane
if (activeTab) {
activeTab.classList.remove('active');
}
if (activePane) {
activePane.classList.remove('active');
}


// Activate the current tab and pane
activeTab = this;
activePane = document.getElementById(this.firstChild.getAttribute('href').substring(1));
activeTab.classList.add('active');
activePane.classList.add('active');
});

li.appendChild(a);
ul.appendChild(li);
}

if (k % 2 === 1) {
var pane = document.createElement('div');
pane.className = 'custom-segments__pane';
pane.setAttribute('id', strAnchor);
let child = tds[k].innerHTML;
pane.innerHTML = child;
customSegments.appendChild(pane);

// Initially, activate the first tab and pane
if (Math.floor(k / 2) == 1) {
pane.classList.add('active');
li.classList.add('active');
activeTab = li
activePane = pane;

}
}
}

table.parentNode.insertBefore(customSegments, table);
table.remove();


}});
}
function onElementRendered(selector, cb, _attempts) {
var el = $(selector);
_attempts = ++_attempts || 1;
if (el.length) return cb(el);
if (_attempts >= 60) return;

setTimeout(function() {
onElementRendered(selector, cb, _attempts);
}, 200);
}

function onPage(regex, fn) {
if (location.pathname.match(regex)) fn();
}
return {
init: function() {
// Add UoB enhancements to rich content displayed in courses.
onPage(/\/(courses|groups)\/\d+/, function() {
transformTabs();
});
}
};


})();
5 changes: 5 additions & 0 deletions src/vector_images/markasdone.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/vector_images/markasdonecompleted.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e44eefd

Please sign in to comment.