Supporting each other

Community forums

Welcome, Guest
Username: Password: Remember me
The place for more technical questions for those who want to create new pagetypes or bespoke functionality.
  • Page:
  • 1

TOPIC: Surfacing global variables

Surfacing global variables 3 months 1 week ago #7490

  • Neil Pollock
  • Neil Pollock's Avatar Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 92
  • Thank you received: 4
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.

Please Log in or Create an account to join the conversation.

Surfacing global variables 3 months 1 week ago #7491

  • Fay
  • Fay's Avatar
  • Offline
  • Gold Boarder
  • Gold Boarder
  • Posts: 193
  • Thank you received: 66
{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.

Please Log in or Create an account to join the conversation.

Surfacing global variables 3 months 6 days ago #7492

  • tom
  • tom's Avatar
  • Away
  • Administrator
  • Administrator
  • Posts: 1085
  • Thank you received: 232
You might want to consider just replacing the template_id in the url.

Please Log in or Create an account to join the conversation.

Surfacing global variables 3 months 6 days ago #7493

  • Neil Pollock
  • Neil Pollock's Avatar Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 92
  • Thank you received: 4
My initial thought was to do something like this:
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.

Please Log in or Create an account to join the conversation.

Surfacing global variables 3 months 4 days ago #7497

  • jjs
  • jjs's Avatar
  • Offline
  • Moderator
  • Moderator
  • Posts: 383
  • Thank you received: 66
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):

//  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

Please Log in or Create an account to join the conversation.

Last edit: by jjs.

Surfacing global variables 3 months 2 days ago #7498

  • Neil Pollock
  • Neil Pollock's Avatar Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 92
  • Thank you received: 4
Thanks, John.

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

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Moderators: jjs
Time to create page: 0.048 seconds
Copyright © 2021 The Xerte Project.
Xerte logo Apereo logo OSI Logo

Search