Commit df23a076 authored by alexeyomelchenko's avatar alexeyomelchenko
Browse files

Bug 1644675 & 1650414 Eliminate MochiKit from Mahara JS files: PieForms



behatnotneeded

Change-Id: Iaa60ca7fd407505da22ff271e5d505f75e34825f
Signed-off-by: default avataralexeyomelchenko <alexeyomelchenko@catalyst.net.nz>
parent 3745f36e
...@@ -44,7 +44,6 @@ var FileBrowser = (function($) { ...@@ -44,7 +44,6 @@ var FileBrowser = (function($) {
}; };
this.submitform = function () { this.submitform = function () {
signal(self.form, 'onsubmit'); // MochiKit to be removed after cleanup
$(self.form).triggerHandler('submit'); $(self.form).triggerHandler('submit');
self.form.submit(); self.form.submit();
}; };
......
...@@ -30,48 +30,49 @@ window.pieformHandlers = {}; ...@@ -30,48 +30,49 @@ window.pieformHandlers = {};
* Handles things that work the same across all pieforms, such as plugin * Handles things that work the same across all pieforms, such as plugin
* management and events * management and events
*/ */
function PieformManager() {//{{{ var PieformManager = (function($) {
return function() {
var self = this; var self = this;
this.init = function() {//{{{ this.init = function() {
self.connect('onload', null, self.setFocus); self.connect('onload', null, self.setFocus);
self.signal('onload', null); self.signal('onload', null);
}//}}} };
/** /**
* When called, ensures the focus is set correctly for all pieforms on the * When called, ensures the focus is set correctly for all pieforms on the
* page * page
*/ */
this.setFocus = function() {//{{{ this.setFocus = function() {
var check = getElementsByTagAndClassName('form', 'pieform'); var check = $('form.pieform');
var formsWithError = filter(function(i) { return hasElementClass(i, 'error'); }, check); var formsWithError = check.filter(function() { return $(this).hasClass('error'); });
if (formsWithError.length > 0) { if (formsWithError.length > 0) {
check = formsWithError; check = formsWithError;
} }
forEach(check, function(form) { check.each(function() {
var element = getFirstElementByTagAndClassName(null, 'autofocus', form); var element = $(this).find('.autofocus')[0];
if (element && (typeof(element.focus) == 'function' || (element.focus && element.focus.call))) { if (element && (typeof(element.focus) == 'function' || (element.focus && element.focus.call))) {
var type = getNodeAttribute(element, 'type'); var type = $(element).prop('type');
if (type && type == 'hidden') { if (type && type == 'hidden') {
return; return;
} }
try { // If element is invisible, IE will throw an error try { // If element is invisible, IE will throw an error
element.focus(); element.focus();
if (hasElementClass(element, 'autoselect') if ($(element).hasClass('autoselect')
&& (typeof(element.select) == 'function' || (element.focus && element.select.call))) { && (typeof(element.select) == 'function' || (element.focus && element.select.call))) {
element.select(); element.select();
} }
} }
catch (e) {} catch (e) {}
throw MochiKit.Iter.StopIteration; return false;
} }
}); });
}//}}} };
/** /**
* Loads a javascript plugin file * Loads a javascript plugin file
*/ */
this.loadPlugin = function(type, name) {//{{{ this.loadPlugin = function(type, name) {
if (type != 'element' && type != 'renderer' && type != 'rule') { if (type != 'element' && type != 'renderer' && type != 'rule') {
throw 'Plugin type ' + type + ' is not valid'; throw 'Plugin type ' + type + ' is not valid';
} }
...@@ -79,34 +80,34 @@ function PieformManager() {//{{{ ...@@ -79,34 +80,34 @@ function PieformManager() {//{{{
return; return;
} }
var script = createDOM('script', { var script = $('<script>', {
'type': 'application/javascript', 'type': 'application/javascript',
'src' : self.pieformPath + type + 's/' + name + '.js' 'src' : self.pieformPath + type + 's/' + name + '.js'
}); });
appendChildNodes(self.head, script); script.append(self.head);
self.loadCache[type][name] = 1; self.loadCache[type][name] = 1;
}//}}} };
/** /**
* Registers an observer for a given event type * Registers an observer for a given event type
*/ */
this.connect = function(slot, form, callback) {//{{{ this.connect = function(slot, form, callback) {
if (typeof(self.observers[slot]) == 'undefined') { if (typeof(self.observers[slot]) == 'undefined') {
throw 'Slot ' + slot + ' does not exist'; throw 'Slot ' + slot + ' does not exist';
} }
self.observers[slot].push({'form': form, 'callback': callback}); self.observers[slot].push({'form': form, 'callback': callback});
}//}}} };
this.signal = function(slot, form) {//{{{ this.signal = function(slot, form) {
forEach(self.observers[slot], function(observer) { $.each(self.observers[slot], function(id, observer) {
if (form == null || observer.form == null || form == observer['form']) { if (form == null || observer.form == null || form == observer['form']) {
observer.callback(form); observer.callback(form);
} }
}); });
}//}}} };
this.head = getFirstElementByTagAndClassName('head'); this.head = $('head')[0];
if (typeof(pieformPath) == 'string') { if (typeof(pieformPath) == 'string') {
this.pieformPath = pieformPath; this.pieformPath = pieformPath;
...@@ -126,39 +127,39 @@ function PieformManager() {//{{{ ...@@ -126,39 +127,39 @@ function PieformManager() {//{{{
'onreply' : [] // when a response is received 'onreply' : [] // when a response is received
}; };
addLoadEvent(self.init); $(document).ready(self.init);
}//}}} };
}(jQuery));
PieformManager = new PieformManager(); PieformManager = new PieformManager();
/** /**
* Handles the javascript side of pieforms - submitting the form via a hidden * Handles the javascript side of pieforms - submitting the form via a hidden
* iframe and dealing with the result * iframe and dealing with the result
*/ */
function Pieform(data) {//{{{ var Pieform = (function($) {
return function(data) {
var self = this; var self = this;
this.init = function() {//{{{ this.init = function() {
if (self.data.checkDirtyChange) { if (self.data.checkDirtyChange) {
formchangemanager.add(self.data.name); formchangemanager.add(self.data.name);
} }
connect(self.data.name, 'onsubmit', self.processForm); $('#' + self.data.name).on('submit', self.processForm);
self.connectSubmitButtons(); self.connectSubmitButtons();
// Hook for pieform elements that need to execute Javascript // Hook for pieform elements that need to execute Javascript
// *after* the Pieform has been initialized. // *after* the Pieform has been initialized.
jQuery(document).triggerHandler('pieform_postinit', self); $(document).triggerHandler('pieform_postinit', self);
}//}}} };
this.processForm = function(e) {//{{{ this.processForm = function(e) {
PieformManager.signal('onsubmit', self.data.name); PieformManager.signal('onsubmit', self.data.name);
// Call the presubmit callback, if there is one // Call the presubmit callback, if there is one
if (typeof(self.data.preSubmitCallback) == 'string' if (typeof(self.data.preSubmitCallback) == 'string'
&& self.data.preSubmitCallback != "") { && self.data.preSubmitCallback != "") {
window[self.data.preSubmitCallback]($(self.data.name), self.clickedButton, e); window[self.data.preSubmitCallback]($('#' + self.data.name)[0], self.clickedButton, e);
} }
// If the form actually isn't a jsform - i.e. only a presubmithandler // If the form actually isn't a jsform - i.e. only a presubmithandler
...@@ -170,15 +171,13 @@ function Pieform(data) {//{{{ ...@@ -170,15 +171,13 @@ function Pieform(data) {//{{{
// Ensure the iframe exists and make sure the form targets it // Ensure the iframe exists and make sure the form targets it
// self.data.newIframes = true; // self.data.newIframes = true;
var iframeName = self.setupIframe(); var iframeName = self.setupIframe();
$(self.data.name).target = iframeName; $('#' + self.data.name)[0].target = iframeName;
appendChildNodes(self.data.name, $('#' + self.data.name).append($('<input>', {
INPUT({
'type': 'hidden', 'type': 'hidden',
'name': 'pieform_jssubmission', 'name': 'pieform_jssubmission',
'value': 1 'value': 1
}) }));
);
window.pieformHandlers[self.data.name] = function(data) { window.pieformHandlers[self.data.name] = function(data) {
// If canceling the form, redirect away // If canceling the form, redirect away
...@@ -190,30 +189,30 @@ function Pieform(data) {//{{{ ...@@ -190,30 +189,30 @@ function Pieform(data) {//{{{
if (typeof(data.replaceHTML) == 'string') { if (typeof(data.replaceHTML) == 'string') {
PieformManager.signal('onreply', self.data.name); PieformManager.signal('onreply', self.data.name);
var tmp = DIV(); var tmp = $('<div>');
tmp.innerHTML = data.replaceHTML; tmp.html(data.replaceHTML);
// Work out whether the new form tag has the error class on it, for // Work out whether the new form tag has the error class on it, for
// updating the form in the document // updating the form in the document
if (hasElementClass(tmp.childNodes[0], 'error')) { if (tmp.children().first().hasClass('error')) {
addElementClass(self.data.name, 'error'); $('#' + self.data.name).addClass('error');
} }
else { else {
removeElementClass(self.data.name, 'error'); $('#' + self.data.name).removeClass('error');
} }
// The first child node is the form tag. We replace the children of // The first child node is the form tag. We replace the children of
// the current form tag with the new children. This prevents // the current form tag with the new children. This prevents
// javascript references being lost // javascript references being lost
replaceChildNodes($(self.data.name), tmp.childNodes[0].childNodes); $('#' + self.data.name).empty().append(tmp.children().first().children());
// data.replaceHTML may contain inline javascript code which need to be evaluated // data.replaceHTML may contain inline javascript code which need to be evaluated
// Append any inline js code to data.javascript and evaluate them // Append any inline js code to data.javascript and evaluate them
var temp = jQuery('<div>').append(data.replaceHTML); var temp = $('<div>').append(data.replaceHTML);
data.javascript = ''; data.javascript = '';
temp.find('*').each(function() { temp.find('*').each(function() {
if (jQuery(this).prop('nodeName') === 'SCRIPT' && jQuery(this).prop('src') === '') { if ($(this).prop('nodeName') === 'SCRIPT' && $(this).prop('src') === '') {
data.javascript += jQuery(this).prop('innerHTML'); data.javascript += $(this).prop('innerHTML');
} }
}); });
eval(data.javascript); eval(data.javascript);
...@@ -230,7 +229,7 @@ function Pieform(data) {//{{{ ...@@ -230,7 +229,7 @@ function Pieform(data) {//{{{
// Call the defined success callback, if there is one // Call the defined success callback, if there is one
if (typeof(self.data.jsSuccessCallback) == 'string' if (typeof(self.data.jsSuccessCallback) == 'string'
&& self.data.jsSuccessCallback != "") { && self.data.jsSuccessCallback != "") {
window[self.data.jsSuccessCallback]($(self.data.name), data); window[self.data.jsSuccessCallback]($('#' + self.data.name)[0], data);
} }
else { else {
// TODO: work out what I'm going to do here... // TODO: work out what I'm going to do here...
...@@ -242,12 +241,12 @@ function Pieform(data) {//{{{ ...@@ -242,12 +241,12 @@ function Pieform(data) {//{{{
else if (data.returnCode == -1) { else if (data.returnCode == -1) {
if (typeof(self.data.jsErrorCallback) == 'string' if (typeof(self.data.jsErrorCallback) == 'string'
&& self.data.jsErrorCallback != '') { && self.data.jsErrorCallback != '') {
window[self.data.jsErrorCallback]($(self.data.name), data); window[self.data.jsErrorCallback]($('#' + self.data.name)[0], data);
} }
} }
else if (typeof(self.data.globalJsErrorCallback) == 'string' else if (typeof(self.data.globalJsErrorCallback) == 'string'
&& self.data.globalJsErrorCallback != '') { && self.data.globalJsErrorCallback != '') {
window[self.data.globalJsErrorCallback]($(self.data.name), data); window[self.data.globalJsErrorCallback]($('#' + self.data.name)[0], data);
} }
else { else {
alert('Developer: got error code ' + data.returnCode alert('Developer: got error code ' + data.returnCode
...@@ -259,12 +258,12 @@ function Pieform(data) {//{{{ ...@@ -259,12 +258,12 @@ function Pieform(data) {//{{{
// not for if it cancels) // not for if it cancels)
if (typeof(self.data.postSubmitCallback) == 'string' if (typeof(self.data.postSubmitCallback) == 'string'
&& self.data.postSubmitCallback != '') { && self.data.postSubmitCallback != '') {
window[self.data.postSubmitCallback]($(self.data.name), self.clickedButton, e); window[self.data.postSubmitCallback]($('#' + self.data.name)[0], self.clickedButton, e);
} }
} }
}//}}} };
this.setupIframe = function() {//{{{ this.setupIframe = function() {
var iframeName = self.data.name + '_iframe'; var iframeName = self.data.name + '_iframe';
if (self.data.newIframeOnSubmit) { if (self.data.newIframeOnSubmit) {
if (!self.data.nextIframe) { if (!self.data.nextIframe) {
...@@ -273,28 +272,28 @@ function Pieform(data) {//{{{ ...@@ -273,28 +272,28 @@ function Pieform(data) {//{{{
iframeName += '_' + self.data.nextIframe; iframeName += '_' + self.data.nextIframe;
self.data.nextIframe++; self.data.nextIframe++;
} }
if ($(iframeName)) { if ($('#' + iframeName).length) {
self.iframe = $(iframeName); self.iframe = $('#' + iframeName)[0];
} }
else { else {
self.iframe = createDOM('iframe', { self.iframe = $('<iframe>', {
'name': iframeName, 'name': iframeName,
'id' : iframeName, 'id' : iframeName,
'style': 'position: absolute; visibility: hidden; height: 0px; width: 0px;' 'style': 'position: absolute; visibility: hidden; height: 0px; width: 0px;'
}); })[0];
insertSiblingNodesAfter(self.data.name, self.iframe); $(self.iframe).insertAfter($('#' + self.data.name));
} }
return iframeName; return iframeName;
}//}}} };
this.connectSubmitButtons = function() {//{{{ this.connectSubmitButtons = function() {
forEach(self.data.submitButtons, function(buttonName) { $.each(self.data.submitButtons, function(id, buttonName) {
var btn = $(self.data.name + '_' + buttonName); var btn = $('#' + self.data.name + '_' + buttonName)[0];
if (btn) { if (btn) {
connect(btn, 'onclick', function() { self.clickedButton = this; }); $(btn).on('click', function() { self.clickedButton = this; });
} }
}); });
}//}}} };
// A reference to the iframe that submissions are made through // A reference to the iframe that submissions are made through
this.iframe = null; this.iframe = null;
...@@ -305,6 +304,7 @@ function Pieform(data) {//{{{ ...@@ -305,6 +304,7 @@ function Pieform(data) {//{{{
// Form configuration data passed from PHP // Form configuration data passed from PHP
this.data = data; this.data = data;
addLoadEvent(self.init); $(document).ready(self.init);
}//}}} };
}(jQuery));
Supports Markdown
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