The place for more technical questions for those who want to create new pagetypes or bespoke functionality.

Surfacing global variables

  • Neil Pollock
  • Onderwerp Auteur
  • Offline
  • Premium lid
  • Premium lid
Meer
5 jaren 4 weken geleden #7490 door Neil Pollock
Surfacing global variables werd gestart door Neil Pollock
I notice in the upcoming version of Xerte there is an option to use three global variables (date created, date updated, number of views).
Is there a way to get the current page number as a variable? That is, if the page is the third visible page in a project, you would get "3", if it's the fifth page you get "5" etc.

The reason I ask is that on our resources we currently have a language toggle on each page which links directly to the equivalent Welsh language version (click through some pages here for an example: xerte.cardiff.ac.uk/play_8659#page1 ). Obviously, setting this up is a tedious, manual job, and was wondering if there was a more automated way to do this.

Graag Inloggen of een account aanmaken deelnemen aan het gesprek.

Meer
5 jaren 4 weken geleden #7491 door Fay
Beantwoord door Fay in topic Surfacing global variables
{x_currentPage+1} will return the current page number of an XOT template project. This may not always be right though - for example, if a project contains standalone pages then this number may not reflect the page number you'd expect to see.

Graag Inloggen of een account aanmaken deelnemen aan het gesprek.

Meer
5 jaren 4 weken geleden #7492 door tom
Beantwoord door tom in topic Surfacing global variables
You might want to consider just replacing the template_id in the url.

Graag Inloggen of een account aanmaken deelnemen aan het gesprek.

  • Neil Pollock
  • Onderwerp Auteur
  • Offline
  • Premium lid
  • Premium lid
Meer
5 jaren 4 weken geleden #7493 door Neil Pollock
Beantwoord door Neil Pollock in topic Surfacing global variables
My initial thought was to do something like this:
Code:
var pageNo = x_currentPage+1; $("#translation").html("<a href='https://xerte.cardiff.ac.uk/play_4848#page" + pageNo + "''>Cymraeg</a>");

Then I could just have <span id="translation"></span> on each page.

The problem is that this doesn't work if added to the project-level script box. It only works if added to each individual page, but doing this is almost as much work as manually adding all of the URLs.

Graag Inloggen of een account aanmaken deelnemen aan het gesprek.

Meer
5 jaren 3 weken geleden - 5 jaren 3 weken geleden #7497 door JohnSmith
Beantwoord door JohnSmith in topic Surfacing global variables
Hi Neil,

I'm not saying that this is a good idea (for most people) or will never break (it DEFINITELY will with future developments) but you can roll your own Pub/Sub events, hook them into certain parts of the code and then listen for those events firing. This has the benefit that you place this code in the LO script (not individual pages):

Code:
// VERY simple Pub/Sub implementation modified from https://gist.github.com/addyosmani/1321768 var XENITH = (function($, X) { var topics = {}; X.Event = function( id ) { var callbacks, topic = id && topics[ id ]; if ( !topic ) { callbacks = $.Callbacks(); topic = { publish: callbacks.fire, subscribe: callbacks.add, unsubscribe: callbacks.remove }; if ( id ) topics[ id ] = topic; } return topic; }; return X; }(jQuery, XENITH || {})); // Patch Xenith code to publish page_changed event eval(x_changePageStep6.toString().split('').reverse().join('').replace("}", "").split('').reverse().join('') + "XENITH.Event( 'page_changed' ).publish(x_currentPage+1);\r\n}"); // Hook the new event and fire off some others that are hookable XENITH.Event( 'page_changed' ).subscribe(function(page_no){ if (x_pageInfo[x_currentPage].built === false) { XENITH.Event( 'page_first_loaded' ).publish(x_currentPage+1); } else { XENITH.Event( 'page_reloaded' ).publish(x_currentPage+1); } }); // Hook our custom 'page_first_loaded' event XENITH.Event( 'page_first_loaded' ).subscribe(function(page_no){ console.log("Page # " + page_no + " loaded for first time..." ); }); // Hook our custom 'page_reloaded' event XENITH.Event( 'page_reloaded' ).subscribe(function(page_no){ console.log("Page # " + page_no + " was reloaded..." ); });

Ideally, future developments will automatically publish a load of 'events' at various points in the lifecycle of the LO and you would be able to subscribe to these and do something. Things like 'glossary_window_open', 'fullscreen', etc etc. None of these are hooked 'YET' in the main code but they may be in the future. In the meantime i've used function.toString() to get the javascript in a function, modify it to publish a topic and then rewrite the function. Goes without saying that you should only do this once in the lifecycle of a LO otherwise if will publish the same topic multiple times.

Example above just console logs the page number and whether the page has first loaded or reloaded... should be easy to add your translation code in there somewhere. I'll leave that as an exercise for you but if you need help then just reply here and i'll see it.

Use at your own risk!

Regards,

John
Laatst bewerkt 5 jaren 3 weken geleden door JohnSmith.

Graag Inloggen of een account aanmaken deelnemen aan het gesprek.

  • Neil Pollock
  • Onderwerp Auteur
  • Offline
  • Premium lid
  • Premium lid
Meer
5 jaren 3 weken geleden #7498 door Neil Pollock
Beantwoord door Neil Pollock in topic Surfacing global variables
Thanks, John.

I'll try to find some time to play around with this.

Graag Inloggen of een account aanmaken deelnemen aan het gesprek.

Moderators: JohnSmith
Tijd voor maken pagina: 0.891 seconden
Copyright © 2026 The Xerte Project.
Xerte logo Apereo logo OSI Logo