UNPKG

@oat-sa/tao-test-runner-qti

Version:
325 lines (302 loc) 10.3 kB
/** * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; under version 2 * of the License (non-upgradable). * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Copyright (c) 2016 (original work) Open Assessment Technologies SA ; */ /** * Test Runner Tool Plugin : Calculator * * @author Sam <sam@taotesting.com> */ import $ from 'jquery'; import _ from 'lodash'; import __ from 'i18n'; import 'ui/hider'; import calculatorFactory from 'ui/calculator'; import basicCalculatorFactory from 'ui/maths/calculator/basicCalculator'; import scientificCalculatorFactory from 'ui/maths/calculator/scientificCalculator'; import shortcut from 'util/shortcut'; import namespaceHelper from 'util/namespace'; import pluginFactory from 'taoTests/runner/plugin'; import mapHelper from 'taoQtiTest/runner/helpers/map'; import calculatorTpl from 'taoQtiTest/runner/plugins/tools/calculator/calculator.tpl'; /** * Default config for calculator components * @type {Object} */ var defaultCalcConfig = { height: 380, width: 280, top: 50, left: 10, stackingScope: 'test-runner', proportionalResize: true }; /** * Default config for BODMAS calculator component * @type {Object} */ var bodmasCalcConfig = _.defaults( { height: 380, width: 280 }, defaultCalcConfig ); /** * Default config for scientific calculator component * @type {Object} */ var scientificCalcConfig = _.defaults( { width: 490, height: 420, calculator: { maths: { degree: true } } }, defaultCalcConfig ); /** * Returns the configured plugin */ export default pluginFactory({ name: 'calculator', /** * Initialize the plugin (called during runner's init) */ init: function init() { const self = this; const testRunner = this.getTestRunner(); const areaBroker = this.getAreaBroker(); const testRunnerOptions = testRunner.getOptions(); const config = this.getConfig(); const pluginShortcuts = (testRunnerOptions.shortcuts || {})[this.getName()] || {}; /** * Retrieve the calculators categories of the current item * @returns {Object} the calculator categories */ function getCalculatorCategories() { const testContext = testRunner.getTestContext(); const itemIdentifier= testContext.itemIdentifier; const testMap = testRunner.getTestMap(); return { calculator : mapHelper.hasItemCategory(testMap, itemIdentifier, 'calculator', true), bodmas : mapHelper.hasItemCategory(testMap, itemIdentifier, 'calculator-bodmas', true), scientific : mapHelper.hasItemCategory(testMap, itemIdentifier, 'calculator-scientific', true), }; } /** * Checks if the plugin is currently available * @returns {Boolean} */ function isEnabled() { //to be activated with a special category from: // - x-tao-option-calculator // - x-tao-option-calculator-bodmas // - x-tao-option-calculator-scientific const categories = getCalculatorCategories(); return categories.calculator || categories.bodmas || categories.scientific; } /** * Is calculator activated ? if not, then we hide the plugin */ function togglePlugin() { if (isEnabled()) { //allow calculator self.show(); } else { self.hide(); } } /** * Build the calculator component * @param {Function} [calcTpl] - An optional alternative template for the calculator. * Only compatible with the four-functions version */ function buildCalculator(calcTpl) { const categories = getCalculatorCategories(); let factory, calcConfig; if (categories.scientific) { factory = scientificCalculatorFactory; calcConfig = scientificCalcConfig; calcConfig.calculator.maths.degree = _.isUndefined(config.degree) ? scientificCalcConfig.calculator.maths.degree : config.degree; } else if (categories.bodmas) { factory = basicCalculatorFactory; calcConfig = bodmasCalcConfig; } else { factory = calculatorFactory; calcConfig = defaultCalcConfig; } self.calculator = factory( _.defaults( { renderTo: self.$calculatorContainer, replace: true, draggableContainer: areaBroker.getContainer(), alternativeTemplate: calcTpl || null }, calcConfig ) ) .on('show', function() { self.trigger('open'); self.button.turnOn(); }) .on('hide', function() { self.trigger('close'); self.button.turnOff(); }) .after('render', function() { this.show(); }); } /** * Show/hide the calculator */ function toggleCalculator() { if (self.getState('enabled') !== false) { if (self.calculator) { //just show/hide the calculator widget if (self.calculator.is('hidden')) { self.calculator.show(); } else { self.calculator.hide(); } } else { //build calculator widget if (config.template) { require([`tpl!${config.template.replace(/\.tpl$/, '')}`], function(calcTpl) { buildCalculator(calcTpl); }, function() { //in case of error, display the default calculator: buildCalculator(); }); } else { buildCalculator(); } } } } //build element (detached) this.button = this.getAreaBroker() .getToolbox() .createEntry({ control: 'calculator', title: __('Open Calculator'), icon: 'table', text: __('Calculator') }); this.$calculatorContainer = $(calculatorTpl()); //init calculator instance var, it will be created only necessary this.calculator = null; //attach behavior this.button.on('click', function(e) { //prevent action if the click is made inside the form which is a sub part of the button if ($(e.target).closest('.widget-calculator').length) { return; } e.preventDefault(); testRunner.trigger('tool-calculator'); }); if (testRunnerOptions.allowShortcuts) { if (pluginShortcuts.toggle) { shortcut.add( namespaceHelper.namespaceAll(pluginShortcuts.toggle, this.getName(), true), function() { testRunner.trigger('tool-calculator'); }, { avoidInput: true, allowIn: '.widget-calculator' } ); } } //start disabled togglePlugin(); this.disable(); //update plugin state based on changes testRunner .on('loaditem', togglePlugin) .on('enabletools renderitem', function() { self.enable(); }) .on('disabletools unloaditem', function() { self.disable(); if (self.calculator) { //destroy calculator to create a new instance of calculator each time self.calculator.destroy(); self.calculator = null; } }) .on('tool-calculator', function() { if (isEnabled()) { toggleCalculator(); } }); }, /** * Called during the runner's render phase */ render: function render() { var areaBroker = this.getAreaBroker(); areaBroker.getContainer().append(this.$calculatorContainer); }, /** * Called during the runner's destroy phase */ destroy: function destroy() { shortcut.remove(`.${this.getName()}`); this.$calculatorContainer.remove(); if (this.calculator) { this.calculator.destroy(); } }, /** * Enable the button */ enable: function enable() { this.button.enable(); }, /** * Disable the button */ disable: function disable() { this.button.disable(); if (this.calculator) { this.calculator.hide(); } }, /** * Show the button */ show: function show() { this.button.show(); }, /** * Hide the button */ hide: function hide() { this.button.hide(); if (this.calculator) { this.calculator.hide(); } } });