Commit 67463da0 by Valera Rozuvan Committed by polesye

Updated Jasmine tests as per Anton's and Alex's suggestions.

parent a03c5be6
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<div id="lti_id" class="lti" data-open_in_a_new_page="true"> <div id="lti_id" class="lti" data-open_in_a_new_page="true">
<form <form
action="http://www.example.com/some_book" action=""
name="ltiLaunchForm" name="ltiLaunchForm"
class="ltiLaunchForm" class="ltiLaunchForm"
method="post" method="post"
...@@ -27,7 +27,5 @@ ...@@ -27,7 +27,5 @@
<input type="submit" value="Press to Launch" /> <input type="submit" value="Press to Launch" />
</form> </form>
<a href="#" class='link_lti_new_window'>Click to view LTI in new window</a>
</div> </div>
</div> </div>
<div class="lti-wrapper">
<div id="lti_id" class="lti" data-open_in_a_new_page="false">
<form
action="http://www.example.com"
name="ltiLaunchForm"
class="ltiLaunchForm"
method="post"
target="ltiLaunchFrame"
enctype="application/x-www-form-urlencoded"
>
<input name="launch_presentation_return_url" value="" />
<input name="lti_version" value="LTI-1p0" />
<input name="user_id" value="student" />
<input name="oauth_nonce" value="28347958723982798572" />
<input name="oauth_timestamp" value="2389479832" />
<input name="oauth_consumer_key" value="" />
<input name="lis_result_sourcedid" value="" />
<input name="oauth_signature_method" value="HMAC-SHA1" />
<input name="oauth_version" value="1.0" />
<input name="role" value="student" />
<input name="lis_outcome_service_url" value="" />
<input name="oauth_signature" value="89ru3289r3ry283y3r82ryr38yr" />
<input name="lti_message_type" value="basic-lti-launch-request" />
<input name="oauth_callback" value="about:blank" />
<input type="submit" value="Press to Launch" />
</form>
<h3 class="error_message">
Please provide launch_url. Click "Edit", and fill in the
required fields.
</h3>
</div>
</div>
<div class="lti-wrapper">
<div id="lti_id" class="lti" data-open_in_a_new_page="false">
<form
action="http://www.example.com/some_book"
name="ltiLaunchForm"
class="ltiLaunchForm"
method="post"
target="ltiLaunchFrame"
enctype="application/x-www-form-urlencoded"
>
<input name="launch_presentation_return_url" value="" />
<input name="lti_version" value="LTI-1p0" />
<input name="user_id" value="student" />
<input name="oauth_nonce" value="28347958723982798572" />
<input name="oauth_timestamp" value="2389479832" />
<input name="oauth_consumer_key" value="" />
<input name="lis_result_sourcedid" value="" />
<input name="oauth_signature_method" value="HMAC-SHA1" />
<input name="oauth_version" value="1.0" />
<input name="role" value="student" />
<input name="lis_outcome_service_url" value="" />
<input name="oauth_signature" value="89ru3289r3ry283y3r82ryr38yr" />
<input name="lti_message_type" value="basic-lti-launch-request" />
<input name="oauth_callback" value="about:blank" />
<input type="submit" value="Press to Launch" />
</form>
<iframe
name="ltiLaunchFrame"
class="ltiLaunchFrame"
src=""
></iframe>
</div>
</div>
<div class="lti-wrapper">
<div id="lti_id" class="lti" data-open_in_a_new_page="true">
<form
action="http://www.example.com"
name="ltiLaunchForm"
class="ltiLaunchForm"
method="post"
target="_blank"
enctype="application/x-www-form-urlencoded"
>
<input name="launch_presentation_return_url" value="" />
<input name="lti_version" value="LTI-1p0" />
<input name="user_id" value="student" />
<input name="oauth_nonce" value="28347958723982798572" />
<input name="oauth_timestamp" value="2389479832" />
<input name="oauth_consumer_key" value="" />
<input name="lis_result_sourcedid" value="" />
<input name="oauth_signature_method" value="HMAC-SHA1" />
<input name="oauth_version" value="1.0" />
<input name="role" value="student" />
<input name="lis_outcome_service_url" value="" />
<input name="oauth_signature" value="89ru3289r3ry283y3r82ryr38yr" />
<input name="lti_message_type" value="basic-lti-launch-request" />
<input name="oauth_callback" value="about:blank" />
<input type="submit" value="Press to Launch" />
</form>
<h3 class="error_message">
Please provide launch_url. Click "Edit", and fill in the
required fields.
</h3>
</div>
</div>
<div class="lti-wrapper">
<div id="lti_id" class="lti" data-open_in_a_new_page="true">
<form
action=""
name="ltiLaunchForm"
class="ltiLaunchForm"
method="post"
target="_blank"
enctype="application/x-www-form-urlencoded"
>
<input name="launch_presentation_return_url" value="" />
<input name="lti_version" value="LTI-1p0" />
<input name="user_id" value="student" />
<input name="oauth_nonce" value="28347958723982798572" />
<input name="oauth_timestamp" value="2389479832" />
<input name="oauth_consumer_key" value="" />
<input name="lis_result_sourcedid" value="" />
<input name="oauth_signature_method" value="HMAC-SHA1" />
<input name="oauth_version" value="1.0" />
<input name="role" value="student" />
<input name="lis_outcome_service_url" value="" />
<input name="oauth_signature" value="89ru3289r3ry283y3r82ryr38yr" />
<input name="lti_message_type" value="basic-lti-launch-request" />
<input name="oauth_callback" value="about:blank" />
<input type="submit" value="Press to Launch" />
</form>
<h3 class="error_message">
Please provide launch_url. Click "Edit", and fill in the
required fields.
</h3>
</div>
</div>
...@@ -25,16 +25,48 @@ ...@@ -25,16 +25,48 @@
*/ */
(function () { (function () {
var element, form, frame, link; var element, container, form, link,
IN_NEW_WINDOW = 'true',
IN_IFRAME = 'false',
EMPTY_URL = '',
DEFAULT_URL = 'http://www.example.com',
NEW_URL = 'http://www.example.com/some_book';
function initialize(fixture, hasLink) { function initialize(target, action) {
loadFixtures(fixture); var tempEl;
loadFixtures('lti.html');
element = $('.lti-wrapper'); element = $('.lti-wrapper');
if (hasLink) { container = element.find('.lti');
link = element.find('a.link_lti_new_window'); form = container.find('.ltiLaunchForm');
if (target === IN_IFRAME) {
container.data('open_in_a_new_page', 'false');
form.attr('target', 'ltiLaunchFrame');
}
form.attr('action', action);
// If we have a new proper action (non-default), we create either
// a link that will submit the form, or an iframe that will contain
// the answer of auto submitted form.
if (action !== EMPTY_URL && action !== DEFAULT_URL) {
if (target === IN_NEW_WINDOW) {
$('<a />', {
href: '#',
class: 'link_lti_new_window'
}).appendTo(container);
link = container.find('.link_lti_new_window');
} else {
$('<iframe />', {
name: 'ltiLaunchFrame',
class: 'ltiLaunchFrame',
src: ''
}).appendTo(container);
}
} }
form = element.find('.ltiLaunchForm');
spyOnEvent(form, 'submit'); spyOnEvent(form, 'submit');
...@@ -48,7 +80,7 @@ ...@@ -48,7 +80,7 @@
function () { function () {
beforeEach(function () { beforeEach(function () {
initialize('lti_newpage_url_empty.html'); initialize(IN_NEW_WINDOW, EMPTY_URL);
}); });
it('form is not submitted', function () { it('form is not submitted', function () {
...@@ -61,7 +93,7 @@ ...@@ -61,7 +93,7 @@
function () { function () {
beforeEach(function () { beforeEach(function () {
initialize('lti_newpage_url_default.html'); initialize(IN_NEW_WINDOW, DEFAULT_URL);
}); });
it('form is not submitted', function () { it('form is not submitted', function () {
...@@ -75,7 +107,7 @@ ...@@ -75,7 +107,7 @@
function () { function () {
beforeEach(function () { beforeEach(function () {
initialize('lti_newpage_url_new.html', true); initialize(IN_NEW_WINDOW, NEW_URL);
}); });
it('form is not submitted', function () { it('form is not submitted', function () {
...@@ -94,7 +126,7 @@ ...@@ -94,7 +126,7 @@
function () { function () {
beforeEach(function () { beforeEach(function () {
initialize('lti_iframe_url_empty.html'); initialize(IN_IFRAME, EMPTY_URL);
}); });
it('form is not submitted', function () { it('form is not submitted', function () {
...@@ -107,7 +139,7 @@ ...@@ -107,7 +139,7 @@
function () { function () {
beforeEach(function () { beforeEach(function () {
initialize('lti_iframe_url_default.html'); initialize(IN_IFRAME, DEFAULT_URL);
}); });
it('form is not submitted', function () { it('form is not submitted', function () {
...@@ -121,7 +153,7 @@ ...@@ -121,7 +153,7 @@
function () { function () {
beforeEach(function () { beforeEach(function () {
initialize('lti_iframe_url_new.html'); initialize(IN_IFRAME, NEW_URL);
}); });
it('form is submitted', function () { it('form is submitted', function () {
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* 1.) Submit the form. The results will be shown on the current page in an * 1.) Submit the form. The results will be shown on the current page in an
* iframe. * iframe.
* 2.) attach a handler function to a link which will submit the form. The * 2.) Attach a handler function to a link which will submit the form. The
* results will be shown in a new window. * results will be shown in a new window.
* *
* The 'open_in_a_new_page' data attribute of the LTI element dictates which of * The 'open_in_a_new_page' data attribute of the LTI element dictates which of
...@@ -32,8 +32,12 @@ window.LTI = (function () { ...@@ -32,8 +32,12 @@ window.LTI = (function () {
// Function initialize(element) // Function initialize(element)
// //
// Initialize the LTI module. // Initialize the LTI module.
//
// @param element DOM element, or jQuery element object.
//
// @return undefined
function initialize(element) { function initialize(element) {
var form, open_in_a_new_page; var form, openInANewPage, formAction;
// In cms (Studio) the element is already a jQuery object. In lms it is // In cms (Studio) the element is already a jQuery object. In lms it is
// a DOM object. // a DOM object.
...@@ -43,37 +47,35 @@ window.LTI = (function () { ...@@ -43,37 +47,35 @@ window.LTI = (function () {
element = $(element); element = $(element);
form = element.find('.ltiLaunchForm'); form = element.find('.ltiLaunchForm');
formAction = form.attr('action');
if ( // If action is empty string, or action is the default URL that should
// Action is one of: null, undefined, 0, 000, '', false. // not cause a form submit.
!Boolean(form.attr('action')) || if (!formAction || formAction === 'http://www.example.com') {
// Default URL that should not cause a form submit. // Nothing to do - no valid action provided. Error message will be
form.attr('action') === 'http://www.example.com' // displaced in browser (HTML).
) { return;
return; // Nothing to do - no valid action provided.
} }
// We want a Boolean 'true' or 'false'. First we will retrieve the data // We want a Boolean 'true' or 'false'. First we will retrieve the data
// attribute, and then we will parse it via native JSON.parse(). // attribute, and then we will parse it via native JSON.parse().
open_in_a_new_page = element.find('.lti').data('open_in_a_new_page'); openInANewPage = element.find('.lti').data('open_in_a_new_page');
try { openInANewPage = JSON.parse(openInANewPage);
open_in_a_new_page = JSON.parse(open_in_a_new_page);
} catch (e) {
console.log('ERROR: Parsing data attribute "open_in_a_new_page".');
console.log('*** error = "' + e.toString() + '".');
open_in_a_new_page = null;
}
// If the Form's action attribute is set (i.e. we can perform a normal // If the Form's action attribute is set (i.e. we can perform a normal
// submit), then we submit the form immediately or when user will click // submit), then we (depending on instance settings) submit the form
// on a link (depending on instance settings) and make the frame shown. // when user will click on a link, or submit the form immediately.
if (open_in_a_new_page === true) { if (openInANewPage === true) {
element.find('.link_lti_new_window').on('click', function () { element.find('.link_lti_new_window').on('click', function () {
form.submit(); form.submit();
}); });
} else if (open_in_a_new_page === false) { } else {
// At this stage the form exists on the page and has a valid
// action. We are safe to submit it, even if `openInANewPage` is
// set to some weird value.
//
// Best case scenario is that `openInANewPage` is set to `true`.
form.submit(); form.submit();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment