Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this file you can register any number of question types.

...

Methods to implement in the JS Question Type Object:

  • init() - will run on page load and the function needs to initialise any JS-powered content in the question 

  • initEach() - will run on page load AND each time the configurator view gets refreshed via AJAX calls

  • onSystemSelectionChange(event, questionId, selection) - runs when the system changes a selection (auto-selects, selection removals from rule engine, etc). selection of null means deselection.

  • onQuestionActivation(event, questionId) - runs when the rule engine activates the question

  • onQuestionDeactivation(event, questionId) - runs when the rule engine deactivates the question

  • onAnswerActivation(event, questionId, answerId) - runs when the rule engine activates an answer of the question

  • onAnswerDeactivation(event, questionId, answerId) - runs when the rule engine deactivates an answer of the question

  • onValidationChange(event, questionId, validation) - runs when the validation engine alters validation settings for the question (calculation based min/max values)

  • onValidationMessageShown(event, questionId, message) - runs when the engine instructs to show a validation message for the question

  • onValidationMessageCleared(event, questionId) - runs when the engine instructs to hide a validation message for the question

...

Code Block
languagejs
define(['configbox/configurator'], function(configurator) {
	
	var customQuestionType = {

		/**
		 * Will run on page load and the function needs to initialise any JS-powered content in the question  
		 */
		init: function() {

		},

		/**
		 * Will run on page load AND each time the configurator view gets refreshed via AJAX calls
		 */
		initEach: function() {

		},

		/**
		 * Runs when the system changes a selection (auto-selects, selection removals from rule engine, etc)
		 * @param {Event}       event
		 * @param {Number}      questionId
		 * @param {String|NULL} selection - NULL on deselect, the internal selection value for the answer instead
		 */
		onSystemSelectionChange: function(event, questionId, selection) {

		},

		/**
		 * Runs when the rule engine activates the question
		 * @param {Event}       event
		 * @param {Number}      questionId
		 */
		onQuestionActivation: function(event, questionId) {

		},
		
		/**
		 * Runs when the rule engine deactivates the question
		 * @param {Event}       event
		 * @param {Number}      questionId
		 */
		onQuestionDeactivation: function(event, questionId) {

		},

		/**
		 * Runs when the rule engine activates an answer
		 * @param {Event}       event
		 * @param {Number}      questionId
		 * @param {Number}      answerId
		 */
		onAnswerActivation: function(event, questionId, answerId) {

		},

		/**
		 * Runs when the rule engine deactivates an answer
		 * @param {Event}       event
		 * @param {Number}      questionId
		 * @param {Number}      answerId
		 */
		onAnswerDeactivation: function(event, questionId, answerId) {

		},

		/**
		 * Runs when the validation engine changes validation for a question
		 * @param {Event}       event
		 * @param {Number}      questionId
		 * @param {Object}      validation - plain object with vars minval and maxval
		 *						values of each are either NULL for no validation or a number
		 */
		onValidationChange: function(event, questionId, validation) {

		},

		/**
		 * Runs when the engine instructs to show a validation message
		 * @param {Event}       event
		 * @param {Number}      questionId
		 * @param {String}      message - Plain text message to display
		 */
		onValidationMessageShown: function(event, questionId, message) {

		},

		/**
		 * Runs when the engine instructs to remove a validation message
		 * @param {Event}       event
		 * @param {Number}      questionId
		 */
		onValidationMessageCleared: function(event, questionId) {

		}

	}

	// Now we register the custom question type to the configurator engine
	configurator.registerQuestionTyperegisterQuestion('customtype', customQuestionType);

});

 

Mind that your callback functions are called even if the event is about another question type. Check the question type like this in the beginning of each function:

Code Block
var customQuestionType = {
  
  onValidationMessageShown: function(event, questionId, message) {
    if (cbj('#question-' + questionId).is('.type-customtype') === false) {
		return;
	}
  }
  //.. other methods
  
  configurator.registerQuestion('customtype', customQuestionType);
}

Compare with built-in question type JS Objects which are located in [applicaton assets folder]/javascript/questions.js.

...