From 267fe09baabbb72e83311028689a1a764a1f36df Mon Sep 17 00:00:00 2001 From: Tom Greenfield Date: Thu, 9 Nov 2017 12:29:20 +0000 Subject: [PATCH 001/105] Initial pass on scaffold refactor --- frontend/src/modules/scaffold/index.js | 704 ++++++++---------- .../scaffold/views/scaffoldAssetView.js | 4 +- .../scaffold/views/scaffoldCodeEditorView.js | 2 +- 3 files changed, 294 insertions(+), 416 deletions(-) diff --git a/frontend/src/modules/scaffold/index.js b/frontend/src/modules/scaffold/index.js index 3f216bf134..8e4c88d23a 100644 --- a/frontend/src/modules/scaffold/index.js +++ b/frontend/src/modules/scaffold/index.js @@ -1,424 +1,302 @@ -// LICENCE https://github.com/adaptlearning/adapt_authoring/blob/master/LICENSE -define(function(require) { - - var Origin = require('core/origin'); - var _ = require('underscore'); - var BackboneForms = require('backbone-forms'); - var BackboneFormsLists = require('backbone-forms-lists'); - var ScaffoldItemsModalView = require('./views/scaffoldItemsModalView'); - var ScaffoldImageView = require('./views/scaffoldAssetView'); - var ScaffoldBooleanView = require('./views/scaffoldBooleanView'); - var ScaffoldTagsView = require('./views/scaffoldTagsView'); - var ScaffoldQuestionButtonView = require('./views/scaffoldQuestionButtonView'); - var ScaffoldColorPickerView = require('./views/scaffoldColorPickerView'); - var ScaffoldDisplayTitleView = require('./views/scaffoldDisplayTitleView'); - var ScaffoldCodeEditorView = require('./views/scaffoldCodeEditorView'); - var ScaffoldTextAreaView = require('./views/scaffoldTextAreaView'); - var Schemas = require('./schemas'); - var Overrides = require('./backboneFormsOverrides'); - var Helpers = require('core/helpers'); - var ActiveItemsModal = 0; - var isOverlayActive = false; - - var Scaffold = {}; - // Used to pass model data around the current form - var currentModel; - var currentForm; - - // Used to pass an alternative model and alternative attribute to save - var alternativeModel; - var alternativeAttribute; - - // Used to store already build schemas - var builtSchemas = {}; - - Backbone.Form.editors.List.Modal.ModalAdapter = ScaffoldItemsModalView; - - var defaultValidators = _.keys(Backbone.Form.validators); - var types = ['string', 'number', 'boolean', 'object', 'objectid', 'date', 'array']; - var customValidators = []; - var customTemplates = []; - - Origin.on('scaffold:updateSchemas', function(callback, context) { - Origin.trigger('schemas:loadData', function() { - resetSchemas(); - callback.apply(context); - }); - }); - - var resetSchemas = function() { - builtSchemas = {}; - } - - var setupSchemaFields = function(field, key, schema, scaffoldSchema) { - if (field.type === 'array') { - - if (field.items && field.items.properties) { - // If Array type has inputType - should use the inputType to render a fieldObject - if (field.inputType) { - var fieldObject = { - type: field.inputType, - help: field.help, - default: field.default, - fieldType: field.inputType, - subSchema: field.items.properties - }; - - if (_.isObject(field.inputType)) { - fieldObject = _.extend(fieldObject, field.inputType); - } - scaffoldSchema[key] = fieldObject; - } else { - scaffoldSchema[key] = { - type: (Backbone.Form.editors[field.items.inputType]) ? field.items.inputType : 'List', - itemType: 'Object', - subSchema: field.items.properties, - confirmDelete: Origin.l10n.t('app.confirmdelete'), - fieldType: 'List' - } - } - - } else { - if (field.inputType) { - var fieldObject = { - type: field.inputType, - help: field.help, - default: field.default, - fieldType: field.inputType - }; - - if (_.isObject(field.inputType)) { - fieldObject = _.extend(fieldObject, field.inputType); - } - scaffoldSchema[key] = fieldObject; - } else { - scaffoldSchema[key] = { - type: 'List', - itemType:field.items.inputType, - subSchema: field.items, - fieldType: field.items.inputType - } - } - } - - var objectSchema = (schema[key].properties || schema[key].subSchema); - var scaffoldObjectSchema = scaffoldSchema[key].subSchema; - - _.each(objectSchema, function(field, key) { - setupSchemaFields(field, key, objectSchema, scaffoldObjectSchema); - }); - - } else if (field.type != 'object' || field.inputType) { - - var validators = []; - // Go through each validator checking whether this is a default - // or a custom validator - _.each(field.validators, function(validator) { - if (!_.isArray(validator) && _.isObject(validator)) { - // If validator is an object - must be a default - validators.push(validator); - } else { - // Check whether this is a defaultValidator - if (_.contains(defaultValidators, validator)) { - validators.push(validator); - } else { - // If custom - search custom validators - if (validator !== '') { - var customValidator = _.findWhere(customValidators, {name: validator}); - if (!customValidator) { - console.log('No validator of that sort - please register: "' + validator + '" by using Origin.scaffold.addCustomValidator(name, validatorMethod)'); - } - // If match is found - add the method - validators.push(customValidator.validatorMethod); - } - } - } - }) - - // Check if inputType is an object - // if so extend - - var fieldObject = { - type: field.inputType, - validators: validators, - help: field.help, - default: field.default, - fieldType: field.inputType - }; - - if (_.isObject(field.inputType)) { - fieldObject = _.extend(fieldObject, field.inputType); - } - scaffoldSchema[key] = fieldObject; - - } else { - - scaffoldSchema[key] = { - type: 'Object', - subSchema: field.properties - } - - var objectSchema = (schema[key].properties || schema[key].subSchema); - var scaffoldObjectSchema = scaffoldSchema[key].subSchema; - - _.each(objectSchema, function(field, key) { - setupSchemaFields(field, key, objectSchema, scaffoldObjectSchema); - }); - - } - - if (field.title) { - scaffoldSchema[key].title = field.title; - - } else if (field.type === 'object' || field.type === 'array') { - scaffoldSchema[key].title = ''; - scaffoldSchema[key].legend = field.legend; - } - } - - var buildSchema = function(schema, options, type) { - - try { - // These types of schemas change frequently and cannot be cached. - var volatileTypes = ['course', 'config', 'article', 'block', 'component']; - - if (_.indexOf(volatileTypes, type) == -1 && builtSchemas[type]) { - return builtSchemas[type]; - } - - var scaffoldSchema = {}; - - _.each(schema, function(field, key) { - // Build schema - setupSchemaFields(field, key, schema, scaffoldSchema); - }); - - // Only cache non-volatile types. - if (_.indexOf(volatileTypes, type) == -1) { - builtSchemas[type] = scaffoldSchema; - } - - return scaffoldSchema; - } catch (e) { - alert('buildSchema - ' + e.message); +define([ + 'core/origin', + 'core/helpers', + './schemas', + 'backbone-forms', + 'backbone-forms-lists', + './backboneFormsOverrides', + './views/scaffoldAssetView', + './views/scaffoldBooleanView', + './views/scaffoldCodeEditorView', + './views/scaffoldColorPickerView', + './views/scaffoldDisplayTitleView', + './views/scaffoldItemsModalView', + './views/scaffoldQuestionButtonView', + './views/scaffoldTagsView', + './views/scaffoldTextAreaView' +], function(Origin, Helpers, Schemas, BackboneForms, BackboneFormsLists, Overrides, ScaffoldAssetView, ScaffoldBooleanView, ScaffoldCodeEditorView, ScaffoldColorPickerView, ScaffoldDisplayTitleView, ScaffoldItemsModalView, ScaffoldQuestionButtonView, ScaffoldTagsView, ScaffoldTextAreaView) { + + var Scaffold = {}; + var builtSchemas = {}; + var alternativeModel; + var alternativeAttribute; + var currentModel; + var currentForm; + var ActiveItemsModal = 0; + var isOverlayActive = false; + var defaultValidators = _.keys(Backbone.Form.validators); + var customValidators = []; + var customTemplates = []; + + Backbone.Form.editors.List.Modal.ModalAdapter = ScaffoldItemsModalView; + + function onScaffoldUpdateSchemas(callback, context) { + Origin.trigger('schemas:loadData', function() { + builtSchemas = {}; + callback.apply(context); + }); + } + + function generateFieldObject(field, key) { + var isFieldTypeObject = field.type === 'object'; + var inputType = field.inputType; + var items = field.items; + var itemsProperties = items && items.properties; + var itemsInputType = items && items.inputType; + var confirmDelete = Origin.l10n.t('app.confirmdelete'); + + var getTitle = function() { + var title = field.title; + + if (title) { + return title; + } + + if (!isFieldTypeObject) { + return Backbone.Form.Field.prototype.createTitle.call({ key: key }); + } + }; + + var getType = function() { + if (inputType) { + return inputType; + } + + if (isFieldTypeObject) { + return 'Object'; + } + + if (itemsProperties) { + return Backbone.Form.editors[itemsInputType] ? itemsInputType : 'List'; + } + + return itemsInputType; + }; + + var getValidators = function() { + var validators = field.validators || []; + + for (var i = 0, j = validators.length; i < j; i++) { + var validator = validators[i]; + + if (!validator) continue; + + var isDefaultValidator = !_.isArray(validator) && _.isObject(validator) || + _.contains(defaultValidators, validator); + + if (isDefaultValidator) continue; + + var customValidator = _.findWhere(customValidators, { name: validator }); + + if (customValidator) { + validators[i] = customValidator.validatorMethod; + continue; } - } - - var buildFieldsets = function(schema, options) { - - // Setup default fieldsets - var fieldsets = { - general: { - legend: Origin.l10n.t('app.general'), - fields: [] - }, - // ::TODO - // I want to remove this please - properties: { - legend: Origin.l10n.t('app.properties'), - fields: [] - }, - settings :{ - legend: Origin.l10n.t('app.settings'), - fields: [] - }, - extensions: { - legend: Origin.l10n.t('app.extensions'), - fields: ['_extensions'] - } - }; - - _.each(schema, function(value, key) { - // Build main fieldSets - // Check if field is a settings - if (key === '_extensions') { - return; - } - - if (value.isSetting) { - - fieldsets.settings.fields.push(key); - - } else if (value.type === 'object') { - // If value is an object - should give it some rights - // and add it as a field set - but not _extensions - if (fieldsets[key]) { - - fieldsets[key].fields.push(key); - - } else { - - fieldsets[key] = { - legend: Helpers.keyToTitleString(key), - fields: [key] - }; - - } - - } else { - - // All general ones here please - fieldsets.general.fields.push(key); - - } - }); - - // Should check if no settings and no extensions are available - if (!schema._extensions) { - delete fieldsets.extensions; - } - - // Delete empty field sets - if (fieldsets.settings.fields.length === 0) { - delete fieldsets.settings; - } - - if (fieldsets.properties.fields.length === 0) { - delete fieldsets.properties; - } - - // We only want the values - return _.values(fieldsets); - - } - - Scaffold.buildForm = function(options) { - try { - var type = options.model.get('_type') || options.schemaType || options.model._type; - var initialType = type; - - switch (type) { - case 'component': - type = options.model.get('_component'); - break; - - case 'page': - case 'menu': - type = 'contentobject'; - break; - - case '_courseStyle': - type = 'course'; - break; - - case 'theme': - type = options.schemaType; - break; + + validators[i] = ''; + + console.log('No validator of that sort – please register "' + validator + + '" by using Origin.scaffold.addCustomValidator("' + validator + + '", validatorMethod);'); + } + + return _.compact(validators); + }; + + var fieldObject = { + confirmDelete: itemsProperties ? confirmDelete : field.confirmDelete, + default: field.default, + editorAttrs: field.editorAttrs, + editorClass: field.editorClass, + fieldAttrs: field.fieldAttrs, + fieldClass: field.fieldClass, + help: field.help, + itemType: itemsProperties ? 'Object' : itemsInputType, + inputType: inputType, + legend: field.legend, + subSchema: isFieldTypeObject ? field.properties : itemsProperties || items, + title: getTitle(), + titleHTML: field.titleHTML, + type: getType(), + validators: getValidators() + }; + + if (_.isObject(inputType)) { + // merge nested inputType attributes into fieldObject + fieldObject = _.extend(fieldObject, inputType); + } + + return fieldObject; + } + + function setUpSchemaFields(field, key, schema, scaffoldSchema) { + scaffoldSchema[key] = generateFieldObject(field, key); + + var objectSchema = schema[key].properties || schema[key].subSchema; + var scaffoldObjectSchema = scaffoldSchema[key].subSchema; + + for (var i in objectSchema) { + if (objectSchema.hasOwnProperty(i)) { + setUpSchemaFields(objectSchema[i], i, objectSchema, scaffoldObjectSchema); + } + } + } + + function buildSchema(schema, options, type) { + // these types of schemas change frequently and cannot be cached + var isVolatileType = _.contains([ + 'course', + 'config', + 'article', + 'block', + 'component' + ], type); + + var builtSchema = builtSchemas[type]; + + if (!isVolatileType && builtSchema) { + return builtSchema; + } + + var scaffoldSchema = {}; + + for (var key in schema) { + if (schema.hasOwnProperty(key)) { + setUpSchemaFields(schema[key], key, schema, scaffoldSchema); } + } + + // only cache non-volatile types + if (!isVolatileType) { + builtSchemas[type] = scaffoldSchema; + } + + return scaffoldSchema; + } + + function buildFieldsets(schema, options) { + var fieldsets = { + general: { legend: Origin.l10n.t('app.general'), fields: [] }, + properties: { legend: Origin.l10n.t('app.properties'), fields: [] }, + settings: { legend: Origin.l10n.t('app.settings'), fields: [] }, + extensions: { legend: Origin.l10n.t('app.extensions'), fields: [ '_extensions' ] } + }; + + for (var key in schema) { + if (!schema.hasOwnProperty(key) || key === '_extensions') continue; - var schema = new Schemas(type); + var value = schema[key]; - // Support ommission of attributes for certain types - switch (initialType) { - case '_courseStyle': - schema = _.pick(schema, 'customStyle'); - break; + if (value.isSetting) { + fieldsets.settings.fields.push(key); + continue; } - options.model.schema = buildSchema(schema, options, type); - options.fieldsets = buildFieldsets(schema, options); - alternativeModel = options.alternativeModelToSave; - alternativeAttribute = options.alternativeAttributeToSave; - currentModel = options.model; + if (value.type !== 'object') { + fieldsets.general.fields.push(key); + continue; + } + + // if value is an object, give it some rights and add it as field set + if (fieldsets[key]) { + fieldsets[key].fields.push(key); + } else { + fieldsets[key] = { legend: Helpers.keyToTitleString(key), fields: [ key ] }; + } + } + + if (!schema._extensions) { + delete fieldsets.extensions; + } + + if (!fieldsets.settings.fields.length) { + delete fieldsets.settings; + } - var form = new Backbone.Form(options).render(); - currentForm = form; + if (!fieldsets.properties.fields.length) { + delete fieldsets.properties; + } + + return _.values(fieldsets); + } + + Scaffold.buildForm = function(options) { + var model = options.model; + var type = model.get('_type') || model._type; + + switch (type) { + case 'menu': + case 'page': + type = 'contentobject'; + break; + case 'component': + type = model.get('_component'); + } + + var schema = new Schemas(type); + + options.model.schema = buildSchema(schema, options, type); + options.fieldsets = buildFieldsets(schema, options); + alternativeModel = options.alternativeModelToSave; + alternativeAttribute = options.alternativeAttributeToSave; + currentModel = options.model; + currentForm = new Backbone.Form(options).render(); + + return currentForm; + }; + + Scaffold.addCustomField = function(fieldName, view, overwrite) { + if (Backbone.Form.editors[fieldName] && !overwrite) { + console.log('Sorry, the custom field you’re trying to add already exists'); + } else { + Backbone.Form.editors[fieldName] = view; + } + }; + + Scaffold.addCustomTemplate = function(templateName, template, overwrite) { + if (!templateName || !template) { + return console.log('Custom templates need a name and template'); + } + + if (customTemplates[templateName] && !overwrite) { + console.log('Sorry, the custom template you’re trying to add already exists'); + } else { + customTemplates[templateName] = template; + } + }; - return form; - } catch (e) { - alert(e.message); + Scaffold.addCustomValidator = function(name, validatorMethod) { + if (!name || !validatorMethod) { + console.log('Custom validators need a name and validatorMethod'); + } else { + customValidators.push({ name: name, validatorMethod: validatorMethod }); } + }; + + // example of customValidator + /*Scaffold.addCustomValidator('title', function(value, formValues) { + var err = { + type: 'username', + message: 'Usernames must be at least three characters long' + }; + + if (value.length < 3) return err; + });*/ + + Scaffold.getCurrentModel = function() { return currentModel; }; + Scaffold.getCurrentForm = function() { return currentForm; }; + Scaffold.getAlternativeModel = function() { return alternativeModel; }; + Scaffold.getAlternativeAttribute = function() { return alternativeAttribute; }; + Scaffold.getCurrentActiveModals = function() { return ActiveItemsModal; }; + Scaffold.isOverlayActive = function() { return isOverlayActive; }; + Scaffold.setOverlayActive = function(booleanValue) { isOverlayActive = booleanValue; }; + + Origin.on({ + 'scaffold:updateSchemas': onScaffoldUpdateSchemas, + 'scaffold:increaseActiveModals': function() { ActiveItemsModal++; }, + 'scaffold:decreaseActiveModals': function() { ActiveItemsModal--; }, + }); + + Origin.scaffold = Scaffold; - } - - Scaffold.addCustomField = function(fieldName, view, overwrite) { - // Check if field already exists - if (Backbone.Form.editors[fieldName] && !overwrite) { - return console.log("Sorry, the custom field you're trying to add already exists") - } else { - Backbone.Form.editors[fieldName] = view; - } - - } - - Scaffold.addCustomValidator = function(name, validatorMethod) { - var customValidator = {}; - if (!name) { - return console.log('Custom Validators need a name'); - } - if (!validatorMethod) { - return console.log('Custom Validators need a validatorMethod'); - } - customValidator.name = name; - customValidator.validatorMethod = validatorMethod; - customValidators.push(customValidator); - }; - - Scaffold.addCustomTemplate = function(templateName, template, overwrite) { - // Check if arguments are correct - if (!templateName) { - return console.log('Custom Templates need a name'); - } - if (!template) { - return console.log('Custom Templates need a template'); - } - // Check if custom template already exists - if (customTemplates[templateName] && !overwrite) { - return console.log("Sorry, the custom template you're trying to add already exists"); - } else { - customTemplates[templateName] = template; - } - }; - - Scaffold.getCurrentModel = function() { - return currentModel; - } - - Scaffold.getCurrentForm = function() { - return currentForm; - } - - Scaffold.getAlternativeModel = function() { - return alternativeModel - } - - Scaffold.getAlternativeAttribute = function() { - return alternativeAttribute - } - - // Listen to modal views - Origin.on('scaffold:increaseActiveModals', function() { - ActiveItemsModal++; - }); - - Origin.on('scaffold:decreaseActiveModals', function() { - ActiveItemsModal--; - }); - - Scaffold.getCurrentActiveModals = function() { - return ActiveItemsModal; - } - - Scaffold.isOverlayActive = function() { - return isOverlayActive; - } - - Scaffold.setOverlayActive = function(booleanValue) { - isOverlayActive = booleanValue; - } - - // Example of customValidator - /*Scaffold.addCustomValidator('title', function(value, formValues) { - - var err = { - type: 'username', - message: 'Usernames must be at least 3 characters long' - }; - - if (value.length < 3) return err; - - });*/ - - - Origin.scaffold = Scaffold; }); diff --git a/frontend/src/modules/scaffold/views/scaffoldAssetView.js b/frontend/src/modules/scaffold/views/scaffoldAssetView.js index 79aa971020..f82908669a 100644 --- a/frontend/src/modules/scaffold/views/scaffoldAssetView.js +++ b/frontend/src/modules/scaffold/views/scaffoldAssetView.js @@ -54,7 +54,7 @@ define(function(require) { }, render: function() { - var assetType = this.schema.fieldType.replace('Asset:', ''); + var assetType = this.schema.inputType.replace('Asset:', ''); this.$el.html(Handlebars.templates[this.constructor.template]({value: this.value, type: assetType})); this.setValue(this.value); // Should see if the field contains anything on render @@ -142,7 +142,7 @@ define(function(require) { event.preventDefault(); Origin.trigger('modal:open', AssetManagementModalView, { collection: new AssetCollection, - assetType: this.schema.fieldType, + assetType: this.schema.inputType, onUpdate: function(data) { if (data) { diff --git a/frontend/src/modules/scaffold/views/scaffoldCodeEditorView.js b/frontend/src/modules/scaffold/views/scaffoldCodeEditorView.js index 6d19ab9190..a5322ccb4d 100644 --- a/frontend/src/modules/scaffold/views/scaffoldCodeEditorView.js +++ b/frontend/src/modules/scaffold/views/scaffoldCodeEditorView.js @@ -18,7 +18,7 @@ define(function(require) { Backbone.Form.editors.Base.prototype.initialize.call(this, options); // Check which mode the code editor should be in - var fieldType = options.schema.fieldType; + var fieldType = options.schema.inputType; var mode = fieldType.mode; if (mode) { From 9ba4bda73026f31d1f038b7986f4b87b774d0442 Mon Sep 17 00:00:00 2001 From: Tom Greenfield Date: Tue, 21 Nov 2017 10:43:57 +0000 Subject: [PATCH 002/105] Clean up overrides and core schema files --- .../scaffold/backboneFormsOverrides.js | 522 ++++++++---------- frontend/src/modules/scaffold/less/field.less | 4 +- frontend/src/modules/scaffold/less/forms.less | 42 +- .../modules/scaffold/models/schemasModel.js | 20 +- frontend/src/modules/scaffold/schemas.js | 155 +++--- .../src/modules/scaffold/templates/field.hbs | 39 +- .../modules/scaffold/templates/fieldset.hbs | 10 +- .../src/modules/scaffold/templates/form.hbs | 6 + .../src/modules/scaffold/templates/list.hbs | 4 + .../modules/scaffold/templates/listItem.hbs | 4 + routes/lang/en-application.json | 2 + 11 files changed, 392 insertions(+), 416 deletions(-) create mode 100644 frontend/src/modules/scaffold/templates/form.hbs create mode 100644 frontend/src/modules/scaffold/templates/list.hbs create mode 100644 frontend/src/modules/scaffold/templates/listItem.hbs diff --git a/frontend/src/modules/scaffold/backboneFormsOverrides.js b/frontend/src/modules/scaffold/backboneFormsOverrides.js index 522930be80..49bd0fa016 100644 --- a/frontend/src/modules/scaffold/backboneFormsOverrides.js +++ b/frontend/src/modules/scaffold/backboneFormsOverrides.js @@ -1,292 +1,258 @@ -// LICENCE https://github.com/adaptlearning/adapt_authoring/blob/master/LICENSE -define(function(require) { - - var Backbone = require('backbone'); - var BackboneForms = require('backbone-forms'); - var BackboneFormsLists = require('backbone-forms-lists'); - var Handlebars = require('handlebars'); - var templateSettings = Backbone.Form.templateSettings; - - // Setup templates - Backbone.Form.prototype.constructor.template = _.template('\ -
\ -
\ - <% if (submitButton) { %>\ - \ - <% } %>\ -
\ - ', null, templateSettings); - Backbone.Form.Fieldset.prototype.template = Handlebars.templates['fieldset']; - Backbone.Form.Field.prototype.template = Handlebars.templates['field']; - Backbone.Form.NestedField.prototype.template = Handlebars.templates['field']; - Backbone.Form.editors.List.prototype.constructor.template = _.template('\ -
\ -
\ - \ -
\ - ', null, templateSettings) - Backbone.Form.editors.List.Item.prototype.constructor.template = _.template('\ -
\ - \ - \ -
\ - ', null, templateSettings); - - // Overwrites - Backbone.Form.Field.prototype.templateData = function() { - var schema = this.schema; - - return { - help: schema.help || '', - title: schema.title, - titleHTML: schema.titleHTML, - fieldAttrs: schema.fieldAttrs, - editorAttrs: schema.editorAttrs, - key: this.key, - editorId: this.editor.id, - legend: schema.legend, - fieldType: this.schema.fieldType - }; - }; - - Backbone.Form.editors.List.Modal.prototype.itemToString = function(value) { - var createTitle = function(key) { - var context = { key: key }; - - return Backbone.Form.Field.prototype.createTitle.call(context); - }; - - value = value || {}; - - //Pretty print the object keys and values - var parts = []; - _.each(this.nestedSchema, function(schema, key) { - - var desc = schema.title ? schema.title : createTitle(key), - val = value[key]; - - // If value is an array then print out how many items it contains - if (_.isArray(val)) { - var itemsString = ' items'; - if (val.length === 1) { - itemsString = ' item'; - } - val = val.length + itemsString; - } else if (_.isObject(val)) { - // If the value is an object print out the keys and values - valueText = ''; - _.each(val, function(value, key) { - valueText += '
' + key + ' - ' + value; - }); - val = valueText; - } - - if (_.isUndefined(val) || _.isNull(val)) val = ''; - - parts.push('' + desc + ': ' + val); - - }); - - return parts.join('
'); +define([ + 'core/origin', + 'backbone-forms', + 'backbone-forms-lists', +], function(Origin, BackboneForms, BackboneFormsLists) { + + var templates = Handlebars.templates; + var fieldTemplate = templates.field; + var templateData = Backbone.Form.Field.prototype.templateData; + var textInitialize = Backbone.Form.editors.Text.prototype.initialize; + var textAreaRender = Backbone.Form.editors.TextArea.prototype.render; + + Backbone.Form.prototype.constructor.template = templates.form; + Backbone.Form.Fieldset.prototype.template = templates.fieldset; + Backbone.Form.Field.prototype.template = fieldTemplate; + Backbone.Form.NestedField.prototype.template = fieldTemplate; + Backbone.Form.editors.List.prototype.constructor.template = templates.list; + Backbone.Form.editors.List.Item.prototype.constructor.template = templates.listItem; + + // add legend to data + Backbone.Form.Field.prototype.templateData = function() { + var data = templateData.call(this); + + data.legend = this.schema.legend; + + return data; + }; + + // process nested items + Backbone.Form.editors.List.Modal.prototype.itemToString = function(value) { + var createTitle = function(key) { + var context = { key: key }; + + return Form.Field.prototype.createTitle.call(context); }; - Backbone.Form.editors.List.prototype.removeItem = function(item) { - //Confirm delete - var confirmMsg = this.schema.confirmDelete; + value = value || {}; + + //Pretty print the object keys and values + var itemString = Origin.l10n.t('app.item'); + var itemsString = Origin.l10n.t('app.items'); + var parts = []; + _.each(this.nestedSchema, function(schema, key) { + var desc = schema.title ? schema.title : createTitle(key), + val = value[key], + pairs = ''; + + if (Array.isArray(val)) { + // print length + val = val.length += ' ' + val.length === 1 ? itemString : itemsString; + } else if (typeof val === 'object') { + // print nested name/value pairs + for (var name in val) { + if (val.hasOwnProperty(name)) { + pairs += '
' + name + ' - ' + val[name]; + } + } - var remove = _.bind(function(isConfirmed) { - if (isConfirmed === false) return; + val = pairs; + } - var index = _.indexOf(this.items, item); + if (_.isUndefined(val) || _.isNull(val)) val = ''; - this.items[index].remove(); - this.items.splice(index, 1); + // embolden key + parts.push('' + desc + ': ' + val); + }); - if (item.addEventTriggered) { - this.trigger('remove', this, item.editor); - this.trigger('change', this); - } + return parts.join('
'); + }; - if (!this.items.length && !this.Editor.isAsync) this.addItem(); - }, this); + // accomodate sweetalert in item removal + Backbone.Form.editors.List.prototype.removeItem = function(item) { + //Confirm delete + var confirmMsg = this.schema.confirmDelete; - if (confirmMsg) { - window.confirm({ title: confirmMsg, type: 'warning', callback: remove }); - } else { - remove(); - } - }; + var remove = function(isConfirmed) { + if (isConfirmed === false) return; - // Used to setValue with defaults + var index = _.indexOf(this.items, item); - Backbone.Form.editors.Base.prototype.setValue = function(value) { - if (!value && typeof this.schema.default !== 'undefined') { - value = this.schema.default; - } - this.value = value; - } - - var textInitialize = Backbone.Form.editors.Text.prototype.initialize; - - Backbone.Form.editors.Text.prototype.initialize = function(options) { - textInitialize.call(this, options); - - // HACK to disable auto-completion - this.$el.attr('autocomplete', 'off'); - }; - - Backbone.Form.editors.Text.prototype.setValue = function(value) { - var schemaDefault = this.schema.default; - - if (!value && typeof schemaDefault !== 'undefined' && - !(schemaDefault instanceof Array)) { - value = schemaDefault; - } - - this.$el.val(value); - } - - Backbone.Form.editors.TextArea.prototype.render = function() { - - // Place value - this.setValue(this.value); - _.defer(_.bind(function() { - // Initialize the editor - var textarea = this.$el[0]; - this.editor = CKEDITOR.replace(textarea, { - toolbar: [ - { name: 'document', groups: [ 'mode', 'document', 'doctools' ], items: [ 'Source', '-', 'ShowBlocks' ] }, - { name: 'clipboard', groups: [ 'clipboard', 'undo' ], items: [ 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ] }, - { name: 'editing', groups: [ 'find', 'selection', 'spellchecker' ], items: [ 'Find', 'Replace', '-', 'SelectAll' ] }, - { name: 'paragraph', groups: [ 'list', 'indent', 'blocks', 'align', 'bidi' ], items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv' ] }, - {name: 'direction', items: ['BidiLtr', 'BidiRtl']}, - '/', - { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'] }, - { name: 'styles', items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock']}, - { name: 'links', items: [ 'Link', 'Unlink' ] }, - { name: 'colors', items: [ 'TextColor', 'BGColor' ] }, - { name: 'insert', items: [ 'SpecialChar', 'Table' ] }, - { name: 'tools', items: [ ] }, - { name: 'others', items: [ '-' ] } - ], - extraAllowedContent: 'span(*)', - disableNativeSpellChecker: false - }); - - }, this)); - - return this; - } - - Backbone.Form.editors.TextArea.prototype.setValue = function(value) { - if (!value && typeof this.schema.default !== 'undefined') { - value = this.schema.default; + this.items[index].remove(); + this.items.splice(index, 1); + + if (item.addEventTriggered) { + this.trigger('remove', this, item.editor); + this.trigger('change', this); + } + + if (!this.items.length && !this.Editor.isAsync) this.addItem(); + }.bind(this); + + if (confirmMsg) { + window.confirm({ title: confirmMsg, type: 'warning', callback: remove }); + } else { + remove(); } - - this.$el.val(value); - } - - Backbone.Form.editors.TextArea.prototype.getValue = function() { - return this.editor.getData().replace(/[\t\n]/g, ''); - } - + }; + + // disable automatic completion on text fields if not specified + Backbone.Form.editors.Text.prototype.initialize = function(options) { + textInitialize.call(this, options); + + if (!this.$el.attr('autocomplete')) { + this.$el.attr('autocomplete', 'off'); + } + }; + + // render ckeditor in textarea + Backbone.Form.editors.TextArea.prototype.render = function() { + textAreaRender.call(this); + + _.defer(function() { + this.editor = CKEDITOR.replace(this.$el[0], { + dataIndentationChars: '', + disableNativeSpellChecker: false, + extraAllowedContent: 'span(*)', + on: { + instanceReady: function() { + var writer = this.dataProcessor.writer; + + writer.lineBreakChars = ''; + + writer.setRules( 'p', { + indent: false, + breakBeforeOpen: false, + breakAfterOpen: false, + breakAfterClose: false + }); + } + }, + toolbar: [ + { name: 'document', groups: [ 'mode', 'document', 'doctools' ], items: [ 'Source', '-', 'ShowBlocks' ] }, + { name: 'clipboard', groups: [ 'clipboard', 'undo' ], items: [ 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ] }, + { name: 'editing', groups: [ 'find', 'selection', 'spellchecker' ], items: [ 'Find', 'Replace', '-', 'SelectAll' ] }, + { name: 'paragraph', groups: [ 'list', 'indent', 'blocks', 'align', 'bidi' ], items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv' ] }, + { name: 'direction', items: [ 'BidiLtr', 'BidiRtl' ] }, + '/', + { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'] }, + { name: 'styles', items: [ 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' ] }, + { name: 'links', items: [ 'Link', 'Unlink' ] }, + { name: 'colors', items: [ 'TextColor', 'BGColor' ] }, + { name: 'insert', items: [ 'SpecialChar', 'Table' ] }, + { name: 'tools', items: [] }, + { name: 'others', items: [ '-' ] } + ] + }); + }.bind(this)); + + return this; + }; + + // get data from ckeditor in textarea + Backbone.Form.editors.TextArea.prototype.getValue = function() { + return this.editor.getData(); + }; + + // ckeditor removal Backbone.Form.editors.TextArea.prototype.remove = function() { this.editor.removeAllListeners(); CKEDITOR.remove(this.editor); - } - - Backbone.Form.prototype.validate = function(options) { - var self = this, - fields = this.fields, - model = this.model, - errors = {}; - - options = options || {}; - - //Collect errors from schema validation - // !!!OVERRIDE Passing in validate:false will stop the validation of the backbone forms validators - if (!options.skipModelValidate) { - _.each(fields, function(field) { - var error = field.validate(); - if (error) { - if(field.schema.title) { - error.title = field.schema.title; - } - errors[field.key] = error; - } - }); - } - - //Get errors from default Backbone model validator - if (!options.skipModelValidate && model && model.validate) { - var modelErrors = model.validate(this.getValue()); - - if (modelErrors) { - var isDictionary = _.isObject(modelErrors) && !_.isArray(modelErrors); - - //If errors are not in object form then just store on the error object - if (!isDictionary) { - errors._others = errors._others || []; - errors._others.push(modelErrors); - } - - //Merge programmatic errors (requires model.validate() to return an object e.g. { fieldKey: 'error' }) - if (isDictionary) { - _.each(modelErrors, function(val, key) { - //Set error on field if there isn't one already - if (fields[key] && !errors[key]) { - fields[key].setError(val); - errors[key] = val; - } - - else { - //Otherwise add to '_others' key - errors._others = errors._others || []; - var tmpErr = {}; - tmpErr[key] = val; - errors._others.push(tmpErr); - } - }); - } - } - } - - return _.isEmpty(errors) ? null : errors; - } - - Backbone.Form.editors.Number.prototype.onKeyPress = function(event) { - var self = this, - delayedDetermineChange = function() { - setTimeout(function() { - self.determineChange(); - }, 0); - }; - - //Allow backspace - if (event.charCode === 0) { - delayedDetermineChange(); - return; - } - - //Get the whole new value so that we can prevent things like double decimals points etc. - var newVal = this.$el.val() - if( event.charCode != undefined ) { - newVal = newVal + String.fromCharCode(event.charCode); - } - - // Allow support for negative numbers. - var numeric = /^-?[0-9]*\.?[0-9]*?$/.test(newVal); - - if (numeric) { - delayedDetermineChange(); - } - else { - event.preventDefault(); - } - }; - -}); \ No newline at end of file + }; + + // add override to allow prevention of validation + Backbone.Form.prototype.validate = function(options) { + var self = this, + fields = this.fields, + model = this.model, + errors = {}; + + options = options || {}; + + //Collect errors from schema validation + // passing in validate: false will stop validation of the backbone forms validators + if (!options.skipModelValidate) { + _.each(fields, function(field) { + var error = field.validate(); + + if (!error) return; + + var title = field.schema.title; + + if (title) { + error.title = title; + } + + errors[field.key] = error; + }); + } + + //Get errors from default Backbone model validator + if (!options.skipModelValidate && model && model.validate) { + var modelErrors = model.validate(this.getValue()); + + if (modelErrors) { + var isDictionary = _.isObject(modelErrors) && !_.isArray(modelErrors); + + //If errors are not in object form then just store on the error object + if (!isDictionary) { + errors._others = errors._others || []; + errors._others.push(modelErrors); + } + + //Merge programmatic errors (requires model.validate() to return an object e.g. { fieldKey: 'error' }) + if (isDictionary) { + _.each(modelErrors, function(val, key) { + //Set error on field if there isn't one already + if (fields[key] && !errors[key]) { + fields[key].setError(val); + errors[key] = val; + } + + else { + //Otherwise add to '_others' key + errors._others = errors._others || []; + var tmpErr = {}; + tmpErr[key] = val; + errors._others.push(tmpErr); + } + }); + } + } + } + + return _.isEmpty(errors) ? null : errors; + }; + + // allow hyphen to be typed in number fields + Backbone.Form.editors.Number.prototype.onKeyPress = function(event) { + var self = this, + delayedDetermineChange = function() { + setTimeout(function() { + self.determineChange(); + }, 0); + }; + + //Allow backspace + if (event.charCode === 0) { + delayedDetermineChange(); + return; + } + + //Get the whole new value so that we can prevent things like double decimals points etc. + var newVal = this.$el.val() + if( event.charCode != undefined ) { + newVal = newVal + String.fromCharCode(event.charCode); + } + + var numeric = /^-?[0-9]*\.?[0-9]*?$/.test(newVal); + + if (numeric) { + delayedDetermineChange(); + } + else { + event.preventDefault(); + } + }; + +}); diff --git a/frontend/src/modules/scaffold/less/field.less b/frontend/src/modules/scaffold/less/field.less index 4c3a0fad0e..b1e9ae7545 100644 --- a/frontend/src/modules/scaffold/less/field.less +++ b/frontend/src/modules/scaffold/less/field.less @@ -1,3 +1,3 @@ .field-error { - color:red; -} \ No newline at end of file + color: red; +} diff --git a/frontend/src/modules/scaffold/less/forms.less b/frontend/src/modules/scaffold/less/forms.less index 0656669cba..cd76639abd 100644 --- a/frontend/src/modules/scaffold/less/forms.less +++ b/frontend/src/modules/scaffold/less/forms.less @@ -8,9 +8,9 @@ fieldset { margin: 0 0 30px 20px; - background-color: #fff; + background-color: @white; form form & { - padding-bottom: 0px; + padding-bottom: 0; } label { cursor: auto; @@ -28,23 +28,23 @@ form .error { display: block; legend { padding: 25px 0 10px 30px; - font-size:16px; + font-size: 16px; font-weight: @font-weight-bold; } .fieldset-object & { - color:#3e4960; + color: #3e4960; text-decoration: underline; legend { - padding:0; - margin-bottom:0px; + padding: 0; + margin-bottom: 0; } } } .fieldset-object .field { - padding:18px 0px; + padding: 18px 0; &-object { - padding:18px 20px; + padding: 18px 20px; } } @@ -53,18 +53,18 @@ form .error { } .field { - padding:18px 30px; + padding: 18px 30px; &-object { - padding:8px 10px; - border:1px solid #aaa; - border-radius:3px; - margin-bottom:20px; + padding: 8px 10px; + border: 1px solid #aaa; + border-radius: 3px; + margin-bottom: 20px; } i { color: #3e4960; - transition:color 0.3s; + transition: color 0.3s; &:hover { - color:#15a4fa; + color: #15a4fa; } } } @@ -80,21 +80,21 @@ form .error { border-color: #696969; } span { - display:inline-block; - float:left; - width:88%; + display: inline-block; + float: left; + width: 88%; } button { - float:right; + float: right; padding: 1px 8px; font-size: 140%; } } .collapsed { - display:none; + display: none; } .expanded { - display:block; + display: block; } diff --git a/frontend/src/modules/scaffold/models/schemasModel.js b/frontend/src/modules/scaffold/models/schemasModel.js index e8106bc2bf..f2d7c91475 100644 --- a/frontend/src/modules/scaffold/models/schemasModel.js +++ b/frontend/src/modules/scaffold/models/schemasModel.js @@ -1,30 +1,34 @@ -// LICENCE https://github.com/adaptlearning/adapt_authoring/blob/master/LICENSE -define(function(require) { - var Origin = require('core/origin'); +define([ 'core/origin' ], function(Origin) { var SchemasModel = Backbone.Model.extend({ + url: 'api/content/schema', initialize: function() { this.listenTo(Origin, 'schemas:loadData', this.loadData); }, - loadData:function(callback) { - if(!Origin.sessionModel.get('isAuthenticated')) { - return callback.call(); + loadData: function(callback) { + if (!Origin.sessionModel.get('isAuthenticated')) { + return callback(); } + // authenticated, so fetch the schemas model this.fetch({ success: function(model) { Origin.schemas = model; - callback.call(); + callback(); }, error: function() { - Origin.Notify.alert({ type: 'error', text: Origin.l10n.t('app.errorgettingschemas') }); + Origin.Notify.alert({ + type: 'error', + text: Origin.l10n.t('app.errorgettingschemas') + }); } }); } }); return new SchemasModel; + }); diff --git a/frontend/src/modules/scaffold/schemas.js b/frontend/src/modules/scaffold/schemas.js index 2b3be7f4cd..d4ab6f9480 100644 --- a/frontend/src/modules/scaffold/schemas.js +++ b/frontend/src/modules/scaffold/schemas.js @@ -1,84 +1,79 @@ -// LICENCE https://github.com/adaptlearning/adapt_authoring/blob/master/LICENSE -define(function(require) { - var Origin = require('core/origin'); - var SchemasModel = require('./models/schemasModel'); - - var Schemas = function(schemaName) { - var configModel = Origin.editor.data.config; - - if (configModel) { - // Remove any extensions and components that are not enabled on this course - var enabledExtensions = configModel.get('_enabledExtensions'); - var enabledExtensionsKeys = []; - - // Grab the targetAttribute - _.each(enabledExtensions, function(value, key) { - enabledExtensionsKeys.push(value.targetAttribute); - }); - // Get the schema - var schema = JSON.parse(JSON.stringify(Origin.schemas.get(schemaName))); - if(!schema) { - throw new Error('No schema found for \'' + schemaName + '\''); - } - // Compare the enabledExtensions against the current schemas - if (schema._extensions) { - _.each(schema._extensions.properties, function(value, key) { - if (!_.contains(enabledExtensionsKeys, key)) { - delete schema._extensions.properties[key]; - } - }); - } - - if (schemaName == 'course') { - // Remove unrequired globals from the course - if (schema._globals && schema._globals.properties._extensions) { - _.each(schema._globals.properties._extensions.properties, function(value, key) { - if (!_.contains(enabledExtensionsKeys, key)) { - delete schema._globals.properties._extensions.properties[key]; - } - }); - } - - // Go through each _enabledComponents and find it in the schemas - if (schema._globals && schema._globals.properties._components) { - - var enabledComponents = configModel.get('_enabledComponents'); - - var enabledComponentsKeys = _.pluck(enabledComponents, '_component'); - _.each(schema._globals.properties._components.properties, function(value, key) { - if (!_.contains(enabledComponentsKeys, key)) { - delete schema._globals.properties._components.properties[key]; - } - }); - - } - - // trim off the empty globals objects - _.each(schema._globals.properties, function(value, key) { - if(_.isEmpty(value.properties)) { - delete schema._globals.properties[key]; - } - }); - - } - - // trim off any empty fieldsets - _.each(schema, function(value, key) { - if(value.hasOwnProperty('properties') && _.isEmpty(value.properties)) { - delete schema[key]; - } - }); - - // Return the modified schema - return schema; - } else { - var schema = JSON.parse(JSON.stringify(Origin.schemas.get(schemaName))); - delete schema._extensions; - // Remove globals as these are appended to the course model - delete schema.globals; - return schema; +define([ 'core/origin', './models/schemasModel' ], function(Origin, SchemasModel) { + + var Schemas = function(schemaName) { + var schema = JSON.parse(JSON.stringify(Origin.schemas.get(schemaName))); + + if (!schema) { + throw new Error('No schema found for "' + schemaName + '"'); + } + + var config = Origin.editor.data.config; + + if (!config) { + delete schema._extensions; + + return schema; + } + + // remove any extensions and components that are not enabled on this course + var enabledExtensions = _.pluck(config.get('_enabledExtensions'), 'targetAttribute'); + var schemaExtensions = schema._extensions; + var schemaExtensionsProperties = schemaExtensions && schemaExtensions.properties; + + + for (var key in schemaExtensionsProperties) { + if (schemaExtensionsProperties.hasOwnProperty(key) && + !_.contains(enabledExtensions, key)) { + delete schemaExtensionsProperties[key]; + } + } + + if (schemaName === 'course') { + // remove unrequired globals from the course + var globals = schema._globals; + + if (globals) globals = globals.properties; + + var extensionGlobals = globals && globals._extensions.properties; + var componentGlobals = globals && globals._components.properties; + var enabledComponents = _.pluck(config.get('_enabledComponents'), '_component'); + + for (key in extensionGlobals) { + if (extensionGlobals.hasOwnProperty(key) && + !_.contains(enabledExtensions, key)) { + delete extensionGlobals[key]; + } + } + + for (key in componentGlobals) { + if (componentGlobals.hasOwnProperty(key) && + !_.contains(enabledComponents, key)) { + delete componentGlobals[key]; + } + } + + // trim off the empty globals objects + for (key in globals) { + if (globals.hasOwnProperty(key) && _.isEmpty(globals[key].properties)) { + delete globals[key]; } - }; + } + } + + // trim off any empty fieldsets + for (key in schema) { + if (!schema.hasOwnProperty(key)) continue; + + var properties = schema[key].properties; + + if (properties && _.isEmpty(properties)) { + delete schema[key]; + } + } + + return schema; + }; return Schemas; + }); diff --git a/frontend/src/modules/scaffold/templates/field.hbs b/frontend/src/modules/scaffold/templates/field.hbs index 27f82eec7e..4c8a19e643 100644 --- a/frontend/src/modules/scaffold/templates/field.hbs +++ b/frontend/src/modules/scaffold/templates/field.hbs @@ -1,23 +1,18 @@ -
- {{#if title}} - - {{else}} - {{#if legend}} -
{{legend}}
+
+ {{#if title}} + + {{else}} + {{#if legend}} +
+ {{legend}} +
+ {{/if}} + {{/if}} +
\ No newline at end of file + +
+ diff --git a/frontend/src/modules/scaffold/templates/fieldset.hbs b/frontend/src/modules/scaffold/templates/fieldset.hbs index 335f748d78..054c6d7cc0 100644 --- a/frontend/src/modules/scaffold/templates/fieldset.hbs +++ b/frontend/src/modules/scaffold/templates/fieldset.hbs @@ -1,7 +1,7 @@
- {{#if legend}} -
+ {{#if legend}} +
{{legend}} -
- {{/if}} -
\ No newline at end of file + + {{/if}} + diff --git a/frontend/src/modules/scaffold/templates/form.hbs b/frontend/src/modules/scaffold/templates/form.hbs new file mode 100644 index 0000000000..21911118c4 --- /dev/null +++ b/frontend/src/modules/scaffold/templates/form.hbs @@ -0,0 +1,6 @@ +
+
+ {{#if submitButton}} + + {{/if}} +
diff --git a/frontend/src/modules/scaffold/templates/list.hbs b/frontend/src/modules/scaffold/templates/list.hbs new file mode 100644 index 0000000000..89729dca7a --- /dev/null +++ b/frontend/src/modules/scaffold/templates/list.hbs @@ -0,0 +1,4 @@ +
+
+ +
diff --git a/frontend/src/modules/scaffold/templates/listItem.hbs b/frontend/src/modules/scaffold/templates/listItem.hbs new file mode 100644 index 0000000000..00b3d1d748 --- /dev/null +++ b/frontend/src/modules/scaffold/templates/listItem.hbs @@ -0,0 +1,4 @@ +
+ + +
diff --git a/routes/lang/en-application.json b/routes/lang/en-application.json index 5730633337..494ea765c3 100644 --- a/routes/lang/en-application.json +++ b/routes/lang/en-application.json @@ -196,6 +196,8 @@ "app.options": "Options", "app.remove": "Remove", "app.add": "Add", + "app.item": "item", + "app.items": "items", "app.enabledextensions": "Enabled extensions", "app.availableextensions": "Available extensions", "app.noextensionsenabled": "There are currently no extensions enabled.", From a2630f249410cb4cec8c9f482259aead4d4a41d4 Mon Sep 17 00:00:00 2001 From: Tom Greenfield Date: Tue, 21 Nov 2017 12:05:26 +0000 Subject: [PATCH 003/105] Replace custom Boolean view with Checkbox --- frontend/src/modules/scaffold/index.js | 4 +- .../scaffold/views/scaffoldBooleanView.js | 58 ------------------- 2 files changed, 2 insertions(+), 60 deletions(-) delete mode 100644 frontend/src/modules/scaffold/views/scaffoldBooleanView.js diff --git a/frontend/src/modules/scaffold/index.js b/frontend/src/modules/scaffold/index.js index 8e4c88d23a..4918b0f66a 100644 --- a/frontend/src/modules/scaffold/index.js +++ b/frontend/src/modules/scaffold/index.js @@ -6,7 +6,6 @@ define([ 'backbone-forms-lists', './backboneFormsOverrides', './views/scaffoldAssetView', - './views/scaffoldBooleanView', './views/scaffoldCodeEditorView', './views/scaffoldColorPickerView', './views/scaffoldDisplayTitleView', @@ -14,7 +13,7 @@ define([ './views/scaffoldQuestionButtonView', './views/scaffoldTagsView', './views/scaffoldTextAreaView' -], function(Origin, Helpers, Schemas, BackboneForms, BackboneFormsLists, Overrides, ScaffoldAssetView, ScaffoldBooleanView, ScaffoldCodeEditorView, ScaffoldColorPickerView, ScaffoldDisplayTitleView, ScaffoldItemsModalView, ScaffoldQuestionButtonView, ScaffoldTagsView, ScaffoldTextAreaView) { +], function(Origin, Helpers, Schemas, BackboneForms, BackboneFormsLists, Overrides, ScaffoldAssetView, ScaffoldCodeEditorView, ScaffoldColorPickerView, ScaffoldDisplayTitleView, ScaffoldItemsModalView, ScaffoldQuestionButtonView, ScaffoldTagsView, ScaffoldTextAreaView) { var Scaffold = {}; var builtSchemas = {}; @@ -290,6 +289,7 @@ define([ Scaffold.getCurrentActiveModals = function() { return ActiveItemsModal; }; Scaffold.isOverlayActive = function() { return isOverlayActive; }; Scaffold.setOverlayActive = function(booleanValue) { isOverlayActive = booleanValue; }; + Scaffold.addCustomField('Boolean', Backbone.Form.editors.Checkbox); Origin.on({ 'scaffold:updateSchemas': onScaffoldUpdateSchemas, diff --git a/frontend/src/modules/scaffold/views/scaffoldBooleanView.js b/frontend/src/modules/scaffold/views/scaffoldBooleanView.js deleted file mode 100644 index 1d7e96daa9..0000000000 --- a/frontend/src/modules/scaffold/views/scaffoldBooleanView.js +++ /dev/null @@ -1,58 +0,0 @@ -// LICENCE https://github.com/adaptlearning/adapt_authoring/blob/master/LICENSE -define(function(require) { - - var Backbone = require('backbone'); - var BackboneForms = require('backbone-forms'); - var Origin = require('core/origin'); - - var ScaffoldBooleanView = Backbone.Form.editors.Select.extend({ - - getValue: function() { - if (this.$el.val() === 'true' || this.$el.val() === 'True') { - return true; - } else { - return false; - } - }, - - setValue: function(value) { - if ((typeof value == 'undefined' || value == null) && typeof this.schema.default !== 'undefined') { - value = this.schema.default; - } - this.$('option[value="' + value + '"]').attr("selected", "selected"); - }, - - _arrayToHtml: function(array) { - var html = $(); - - //Generate HTML - _.each(array, function(option) { - if (_.isObject(option)) { - if (option.group) { - var optgroup = $("") - .attr("label",option.group) - .html( this._getOptionsHtml(option.options) ); - html = html.add(optgroup); - } else { - var val = (option.val || option.val === 0) ? option.val : ''; - html = html.add( $('