r79492 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79491‎ | r79492 | r79493 >
Date:14:54, 2 January 2011
Author:foxtrott
Status:deferred
Tags:
Comment:
support for external inputs; preview functionality

* external inputs can now register JS initialisation and validation methods
* preview works now with resource loader
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_GlobalFunctions.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)
  • /trunk/extensions/SemanticForms/libs/SF_ajax_form_preview.js (modified) (history)
  • /trunk/extensions/SemanticForms/libs/SemanticForms.js (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_GlobalFunctions.php
@@ -141,6 +141,9 @@
142142 'ext.semanticforms.autogrow' => $sfgResourceTemplate + array(
143143 'scripts' => 'libs/SF_autogrow.js',
144144 ),
 145+ 'ext.semanticforms.preview' => $sfgResourceTemplate + array(
 146+ 'scripts' => 'libs/SF_ajax_form_preview.js',
 147+ ),
145148 );
146149 }
147150
Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -178,6 +178,7 @@
179179 $wgOut->addModules( 'ext.semanticforms.main' );
180180 $wgOut->addModules( 'ext.semanticforms.fancybox' );
181181 $wgOut->addModules( 'ext.semanticforms.autogrow' );
 182+ $wgOut->addModules( 'ext.semanticforms.preview' );
182183 $wgOut->addModules( 'ext.smw.tooltips' );
183184 $wgOut->addModules( 'ext.smw.sorttable' );
184185 }
Index: trunk/extensions/SemanticForms/libs/SemanticForms.js
@@ -210,7 +210,109 @@
211211 };
212212 })( jQuery );
213213
 214+
214215 /*
 216+ * Functions to register/unregister methods for the initialisation/validation
 217+ * of inputs
 218+ */
 219+
 220+// Initialise data object to hold initialisation and validation data
 221+function setupSF() {
 222+
 223+ jQuery("#sfForm").data("SemanticForms",{
 224+ initialisation : new Array(),
 225+ validation : new Array
 226+ });
 227+
 228+}
 229+
 230+// Register a validation method
 231+//
 232+// More than one method may be registered for one input by subsequent calls to
 233+// registerValidation.
 234+//
 235+// @param valfunction The validation functions. Must take a string (the input's id) and an object as parameters
 236+// @param param The parameter object given to the validation function
 237+jQuery.fn.registerValidation = function(valfunction, param) {
 238+
 239+ if ( ! this.attr("id") ) return this;
 240+
 241+ if ( ! jQuery("#sfForm").data("SemanticForms") ) {
 242+ setupSF();
 243+ }
 244+
 245+ if ( ! jQuery("#sfForm").data("SemanticForms").validation[this.attr("id")] ) {
 246+ jQuery("#sfForm").data("SemanticForms").validation[this.attr("id")] = new Array();
 247+ }
 248+
 249+ jQuery("#sfForm").data("SemanticForms").validation[this.attr("id")].push({
 250+ valfunction : valfunction,
 251+ parameters : param
 252+ });
 253+
 254+ return this;
 255+};
 256+
 257+// Register an initialisation method
 258+//
 259+// More than one method may be registered for one input by subsequent calls to
 260+// registerInitialisation. This method also executes the inifunction if the
 261+// element referenced by /this/ is not part of a multipleTemplateStarter.
 262+//
 263+// @param inifunction The initialisation functions. Must take a string (the input's id) and an object as parameters
 264+// @param param The parameter object given to the initialisation function
 265+jQuery.fn.registerInitialisation = function( inifunction, param ) {
 266+
 267+ // return if element has no id
 268+ if ( ! this.attr("id") ) return this;
 269+
 270+ // setup data structure if necessary
 271+ if ( ! jQuery("#sfForm").data("SemanticForms") ) {
 272+ setupSF();
 273+ }
 274+
 275+ // if no initialisation function for this input registered yet, create entry
 276+ if ( ! jQuery("#sfForm").data("SemanticForms").initialisation[this.attr("id")] ) {
 277+ jQuery("#sfForm").data("SemanticForms").initialisation[this.attr("id")] = new Array();
 278+ }
 279+
 280+ // record initialisation function
 281+ jQuery("#sfForm").data("SemanticForms").initialisation[this.attr("id")].push({
 282+ inifunction : inifunction,
 283+ parameters : param
 284+ });
 285+
 286+ // execute initialisation if input is not part of multipleTemplateStarter
 287+ if ( this.closest(".multipleTemplateStarter").length == 0 ) {
 288+ var input = this;
 289+ // ensure inifunction is only exectued after doc structure is complete
 290+ jQuery(function(){inifunction ( input.attr("id"), param )});
 291+ }
 292+
 293+ return this;
 294+};
 295+
 296+// Unregister all validation methods for the element referenced by /this/
 297+jQuery.fn.unregisterValidation = function() {
 298+
 299+ if ( this.attr("id") && jQuery("#sfForm").data("SemanticForms") ) {
 300+ delete jQuery("#sfForm").data("SemanticForms").validation[this.attr("id")];
 301+ }
 302+
 303+ return this;
 304+}
 305+
 306+// Unregister all initialisation methods for the element referenced by /this/
 307+jQuery.fn.unregisterInitialisation = function() {
 308+
 309+ if ( this.attr("id") && jQuery("#sfForm").data("SemanticForms") ) {
 310+ delete jQuery("#sfForm").data("SemanticForms").initialisation[this.attr("id")];
 311+ }
 312+
 313+ return this;
 314+}
 315+
 316+/*
215317 * Functions for handling 'show on select'
216318 */
217319
@@ -409,7 +511,7 @@
410512 }
411513 }
412514
413 -function validateAll() {
 515+window.validateAll = function () {
414516 var num_errors = 0;
415517
416518 // Remove all old error messages.
@@ -447,6 +549,25 @@
448550 if (! jQuery(this).validateDateField() ) num_errors += 1;
449551 });
450552
 553+ // call registered validation functions
 554+ var sfdata = jQuery("#sfForm").data('SemanticForms');
 555+
 556+ if (sfdata) { // found data object?
 557+
 558+ // for every registered input
 559+ for ( var id in sfdata.validation ) {
 560+
 561+ // if input is not part of multipleTemplateStarter
 562+ if ( jQuery("#" + id).closest(".multipleTemplateStarter").length == 0 ) {
 563+
 564+ for ( var i in sfdata.validation[id]) { // every validation method for that input
 565+ if (! sfdata.validation[id][i].valfunction(id, sfdata.validation[id][i].parameters) )
 566+ num_errors += 1;
 567+ }
 568+ }
 569+ }
 570+ }
 571+
451572 if (num_errors > 0) {
452573 // add error header, if it's not there already
453574 if (jQuery("#form_error_header").size() == 0) {
@@ -489,10 +610,42 @@
490611 function() {
491612 if (this.name)
492613 this.name = this.name.replace(/\[num\]/g, '[' + num_elements + ']');
493 - if (this.id)
494 - this.id = this.id.replace(/input_/g, 'input_' + num_elements + '_')
 614+
 615+ if (this.id) {
 616+
 617+ var old_id = this.id;
 618+
 619+ this.id = this.id.replace(/input_/g, 'input_' + num_elements + '_');
 620+
 621+ // register initialisation and validation methods for new inputs
 622+
 623+ var sfdata = jQuery("#sfForm").data('SemanticForms');
 624+ if (sfdata) { // found data object?
 625+
 626+ // for every initialisation method for input with id old_id
 627+ for ( var i in sfdata.initialisation[old_id] ) {
 628+
 629+ // take initialisation method and register for new input
 630+ jQuery(this).registerInitialisation(
 631+ sfdata.initialisation[old_id][i].inifunction,
 632+ sfdata.initialisation[old_id][i].parameters
 633+ );
 634+ }
 635+
 636+ // for every validation method for input with id old_id
 637+ for ( i in sfdata.validation[old_id] ) {
 638+
 639+ // take validation method and register for new input
 640+ jQuery(this).registerValidation(
 641+ sfdata.validation[old_id][i].valfunction,
 642+ sfdata.validation[old_id][i].parameters
 643+ );
 644+ }
 645+ }
 646+ }
495647 }
496648 );
 649+
497650 new_div.find('a').attr('href', function() {
498651 return this.href.replace(/input_/g, 'input_' + num_elements + '_');
499652 });
@@ -515,6 +668,17 @@
516669
517670 // Enable the new remover
518671 new_div.find('.remover').click( function() {
 672+
 673+ // unregister initialisation and validation for deleted inputs
 674+ // probably unnecessary as the used id's will never be assigned a second
 675+ // time, but it's the clean solution (if only to free memory)
 676+ jQuery(this).parent().find("input, select, textarea").each(
 677+ function() {
 678+ jQuery(this).unregisterInitialisation();
 679+ jQuery(this).unregisterValidation();
 680+ }
 681+ );
 682+
519683 jQuery(this).parent().remove();
520684 });
521685
@@ -542,6 +706,27 @@
543707
544708 // Handle AutoGrow as well.
545709 new_div.find('.autoGrow').autoGrow();
 710+
 711+ // initialise new inputs
 712+ new_div.find("input, select, textarea").each(
 713+ function() {
 714+
 715+ if (this.id) {
 716+
 717+ var sfdata = jQuery("#sfForm").data('SemanticForms');
 718+ if (sfdata) { // if anything registered at all
 719+
 720+ for ( var i in sfdata.initialisation[this.id] ) { // every initialisation method for this input
 721+ sfdata.initialisation[this.id][i].inifunction(
 722+ this.id,
 723+ sfdata.initialisation[this.id][i].parameters
 724+ )
 725+ }
 726+ }
 727+ }
 728+ }
 729+ );
 730+
546731 }
547732
548733 var num_elements = 0;
Index: trunk/extensions/SemanticForms/libs/SF_ajax_form_preview.js
@@ -35,7 +35,7 @@
3636 }
3737
3838 function ajaxFormPreviewClick(){ajaxFormPreviewRun(this)}
39 -
 39+
4040 function ajaxFormPreviewRun(btn){
4141
4242 wkPreview = document.getElementById('wikiPreview');
@@ -181,7 +181,7 @@
182182 if (currentfr && !window.opera){
183183
184184 if (currentfr.contentDocument) { //ns6 syntax
185 - doc = currentfr.contentDocument;
 185+ doc = currentfr.contentDocument;
186186 } else if (currentfr.Document && currentfr.Document.body.scrollHeight) { //ie5+ syntax
187187 doc = currentfr.Document;
188188 }
@@ -199,14 +199,14 @@
200200 visible.style.border="0px";
201201
202202 pv = visible;
203 -
 203+
204204 while (pv.previousSibling) {
205205 pv = pv.previousSibling;
206206 if (pv.style) pv.style.display="none";
207207 }
208208
209209 pv = visible;
210 -
 210+
211211 while (pv.nextSibling) {
212212 pv = pv.nextSibling;
213213 if (pv.style) pv.style.display="none";
@@ -245,4 +245,5 @@
246246 }
247247
248248 if (wgAction=='formedit' || wgCanonicalSpecialPageName == 'FormEdit')
249 - addOnloadHook(ajaxFormPreviewInit);
 249+// addOnloadHook(ajaxFormPreviewInit);
 250+ jQuery(function(){ajaxFormPreviewInit()});

Status & tagging log