diff --git a/common/lib/xmodule/xmodule/js/fixtures/lti_newpage_url_new.html b/common/lib/xmodule/xmodule/js/fixtures/lti.html
similarity index 90%
rename from common/lib/xmodule/xmodule/js/fixtures/lti_newpage_url_new.html
rename to common/lib/xmodule/xmodule/js/fixtures/lti.html
index 8fad7b05d2..14bea7d771 100644
--- a/common/lib/xmodule/xmodule/js/fixtures/lti_newpage_url_new.html
+++ b/common/lib/xmodule/xmodule/js/fixtures/lti.html
@@ -2,7 +2,7 @@
diff --git a/common/lib/xmodule/xmodule/js/fixtures/lti_iframe_url_default.html b/common/lib/xmodule/xmodule/js/fixtures/lti_iframe_url_default.html
deleted file mode 100644
index 1f8b8120cd..0000000000
--- a/common/lib/xmodule/xmodule/js/fixtures/lti_iframe_url_default.html
+++ /dev/null
@@ -1,36 +0,0 @@
-
-
-
-
-
-
- Please provide launch_url. Click "Edit", and fill in the
- required fields.
-
-
-
-
diff --git a/common/lib/xmodule/xmodule/js/fixtures/lti_iframe_url_new.html b/common/lib/xmodule/xmodule/js/fixtures/lti_iframe_url_new.html
deleted file mode 100644
index e5a909f0e1..0000000000
--- a/common/lib/xmodule/xmodule/js/fixtures/lti_iframe_url_new.html
+++ /dev/null
@@ -1,37 +0,0 @@
-
diff --git a/common/lib/xmodule/xmodule/js/fixtures/lti_newpage_url_default.html b/common/lib/xmodule/xmodule/js/fixtures/lti_newpage_url_default.html
deleted file mode 100644
index 90cd2f7541..0000000000
--- a/common/lib/xmodule/xmodule/js/fixtures/lti_newpage_url_default.html
+++ /dev/null
@@ -1,36 +0,0 @@
-
-
-
-
-
-
- Please provide launch_url. Click "Edit", and fill in the
- required fields.
-
-
-
-
diff --git a/common/lib/xmodule/xmodule/js/fixtures/lti_newpage_url_empty.html b/common/lib/xmodule/xmodule/js/fixtures/lti_newpage_url_empty.html
deleted file mode 100644
index 0a4cc0088d..0000000000
--- a/common/lib/xmodule/xmodule/js/fixtures/lti_newpage_url_empty.html
+++ /dev/null
@@ -1,36 +0,0 @@
-
-
-
-
-
-
- Please provide launch_url. Click "Edit", and fill in the
- required fields.
-
-
-
-
diff --git a/common/lib/xmodule/xmodule/js/spec/lti/constructor.js b/common/lib/xmodule/xmodule/js/spec/lti/constructor.js
index a5b2df1955..cda1d7988d 100644
--- a/common/lib/xmodule/xmodule/js/spec/lti/constructor.js
+++ b/common/lib/xmodule/xmodule/js/spec/lti/constructor.js
@@ -25,16 +25,48 @@
*/
(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) {
- loadFixtures(fixture);
+ function initialize(target, action) {
+ var tempEl;
+
+ loadFixtures('lti.html');
element = $('.lti-wrapper');
- if (hasLink) {
- link = element.find('a.link_lti_new_window');
+ container = element.find('.lti');
+ 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) {
+ $('', {
+ href: '#',
+ class: 'link_lti_new_window'
+ }).appendTo(container);
+
+ link = container.find('.link_lti_new_window');
+ } else {
+ $('', {
+ name: 'ltiLaunchFrame',
+ class: 'ltiLaunchFrame',
+ src: ''
+ }).appendTo(container);
+ }
}
- form = element.find('.ltiLaunchForm');
spyOnEvent(form, 'submit');
@@ -48,7 +80,7 @@
function () {
beforeEach(function () {
- initialize('lti_newpage_url_empty.html');
+ initialize(IN_NEW_WINDOW, EMPTY_URL);
});
it('form is not submitted', function () {
@@ -61,7 +93,7 @@
function () {
beforeEach(function () {
- initialize('lti_newpage_url_default.html');
+ initialize(IN_NEW_WINDOW, DEFAULT_URL);
});
it('form is not submitted', function () {
@@ -75,7 +107,7 @@
function () {
beforeEach(function () {
- initialize('lti_newpage_url_new.html', true);
+ initialize(IN_NEW_WINDOW, NEW_URL);
});
it('form is not submitted', function () {
@@ -94,7 +126,7 @@
function () {
beforeEach(function () {
- initialize('lti_iframe_url_empty.html');
+ initialize(IN_IFRAME, EMPTY_URL);
});
it('form is not submitted', function () {
@@ -107,7 +139,7 @@
function () {
beforeEach(function () {
- initialize('lti_iframe_url_default.html');
+ initialize(IN_IFRAME, DEFAULT_URL);
});
it('form is not submitted', function () {
@@ -121,7 +153,7 @@
function () {
beforeEach(function () {
- initialize('lti_iframe_url_new.html');
+ initialize(IN_IFRAME, NEW_URL);
});
it('form is submitted', function () {
diff --git a/common/lib/xmodule/xmodule/js/src/lti/lti.js b/common/lib/xmodule/xmodule/js/src/lti/lti.js
index e001fb8d67..9454b0c346 100644
--- a/common/lib/xmodule/xmodule/js/src/lti/lti.js
+++ b/common/lib/xmodule/xmodule/js/src/lti/lti.js
@@ -9,7 +9,7 @@
*
* 1.) Submit the form. The results will be shown on the current page in an
* 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.
*
* The 'open_in_a_new_page' data attribute of the LTI element dictates which of
@@ -32,8 +32,12 @@ window.LTI = (function () {
// Function initialize(element)
//
// Initialize the LTI module.
+ //
+ // @param element DOM element, or jQuery element object.
+ //
+ // @return undefined
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
// a DOM object.
@@ -43,37 +47,35 @@ window.LTI = (function () {
element = $(element);
form = element.find('.ltiLaunchForm');
+ formAction = form.attr('action');
- if (
- // Action is one of: null, undefined, 0, 000, '', false.
- !Boolean(form.attr('action')) ||
+ // If action is empty string, or action is the default URL that should
+ // not cause a form submit.
+ if (!formAction || formAction === 'http://www.example.com') {
- // Default URL that should not cause a form submit.
- form.attr('action') === 'http://www.example.com'
- ) {
- return; // Nothing to do - no valid action provided.
+ // Nothing to do - no valid action provided. Error message will be
+ // displaced in browser (HTML).
+ return;
}
// We want a Boolean 'true' or 'false'. First we will retrieve the data
// 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');
- try {
- 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;
- }
+ openInANewPage = element.find('.lti').data('open_in_a_new_page');
+ openInANewPage = JSON.parse(openInANewPage);
// 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
- // on a link (depending on instance settings) and make the frame shown.
- if (open_in_a_new_page === true) {
+ // submit), then we (depending on instance settings) submit the form
+ // when user will click on a link, or submit the form immediately.
+ if (openInANewPage === true) {
element.find('.link_lti_new_window').on('click', function () {
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();
}
}