Last active
December 17, 2018 11:30
-
-
Save VanDalkvist/310f9f62be3b5cbaecdf9a1a71596368 to your computer and use it in GitHub Desktop.
FormBuilder initialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (module, bus, _, logger, Q) { | |
module.createInstance = _createInstance; | |
function _createInstance(context, instanceConfig, settings) { | |
var initSequence = [ | |
_initState.bind(this, instanceConfig, settings, context), | |
_initViewModel, | |
_initApp, | |
_initEvents, | |
_getComponents, | |
_loadConfiguration, | |
_setConfiguration, | |
_initServices, | |
_returnApp | |
]; | |
var initializationPromise = initSequence.reduce(Q.when, Q.when()); | |
return initializationPromise.fail(_unhandledErrorHandler); | |
} | |
function _initState(instanceConfig, settings, context) { | |
var stateBuilder = new module.StateBuilder(); | |
var stateConfig = { | |
entryId: instanceConfig.entryId, | |
services: ['ConditionEngine', 'AddressFieldSetter', 'ItemBuilder', 'ErrorViewer'], | |
itemBuilders: ['variable', 'design-element', 'form-variable'] | |
}; | |
logger.debug("Configuring Services and Constants for application"); | |
var state = stateBuilder.build(stateConfig); | |
state.addConstant('ImageUrlFormat', settings.imageUrlFormat); | |
state.addConstant('EntryId', instanceConfig.entryId); | |
state.addConstant('EnterQuickFillLabel', settings.labels.enterQuickFill); | |
state.addConstant('ChooseQuickFillLabel', settings.labels.chooseQuickFill); | |
state.addConstant('SaveQuickFillLabel', settings.labels.saveQuickFill); | |
state.addConstant('UpdateLabel', settings.labels.updateView); | |
state.addConstant('ApproveLabel', settings.labels.approveForm); | |
state.addConstant('EntryImageUrl', instanceConfig.entryImageUrl); | |
state.context = context; | |
state.config = instanceConfig; | |
state.openingMode = settings.openingMode; | |
state.showPreviewFirst = settings.showPreviewFirst; | |
return Q.when(state); | |
} | |
function _initViewModel(state) { | |
var config = state.config; | |
module.BaseViewModel.State = function _state() { | |
return state; | |
}; | |
var vm = _createViewModel(config.entryId, config.catalogEntryId, config.quickFillAllowed, config.storeKey, state.showPreviewFirst); | |
vm.loading(true); | |
state.vm = vm; | |
return state; | |
} | |
function _initApp(state) { | |
var app = new module.Application(state, state.config, state.context, state.vm); | |
logger.info("Inner-form application was created with id = '" + app.id + "'."); | |
state.vm = null; | |
state.context = null; | |
app.start(); | |
return app; | |
} | |
function _initEvents(app) { | |
bus.subscribe(module.Events.Form.loaded, _setFormItems); | |
return app; | |
function _setFormItems() { | |
var itemsDictionary = app.configuration.itemsDictionary; | |
app.state.Services.ConditionEngine.addItems(itemsDictionary); | |
app.state.Services.AddressFieldSetter.registerItems(itemsDictionary); | |
bus.unsubscribe(module.Events.Form.loaded, _setFormItems); | |
} | |
} | |
function _getComponents(app) { | |
return module.ComponentLoader.run(module.Components).then(function () { | |
return app; | |
}); | |
} | |
function _createViewModel(entryId, catalogEntryId, quickFillAllowed, storeKey, showPreviewFirst) { | |
var formViewModel = new module.FormViewModel(); | |
var previewViewModel = new module.PreviewViewModel(entryId, showPreviewFirst); | |
var managementViewModel = new module.ManagementViewModel(catalogEntryId, quickFillAllowed, storeKey); | |
var imageUploader = new module.ImageUploader(entryId); | |
return new module.IFViewModel( | |
formViewModel, previewViewModel, managementViewModel, imageUploader); | |
} | |
function _loadConfiguration(app) { | |
var configurationId = app.config.configurationId; | |
return module.Api.FormConfiguration.getOne({id: configurationId}).then(function _setConfigurationOnLoaded(configuration) { | |
logger.info("Form configuration data was successfully loaded."); | |
app.configuration = configuration; | |
return app; | |
}, function (err) { | |
logger.error("Cannot load current configuration by id '" + configurationId + "'."); | |
}); | |
} | |
function _setConfiguration(app) { | |
var parsedConfiguration = JSON.parse(app.configuration.data); | |
var state = app.state; | |
state.Constants.CustomLabels = state.Services.StateBuilder.buildCustomLabels(parsedConfiguration); | |
return app.vm.setConfiguration(parsedConfiguration).then(function _setItems(itemsDictionary) { | |
app.configuration.itemsDictionary = itemsDictionary; | |
return app; | |
}); | |
} | |
function _initServices(app) { | |
bus.publish(module.Events.Form.loaded, app.itemsDictionary); | |
app.vm.loading(false); | |
return app; | |
} | |
function _returnApp(app) { | |
return { stop: app.stop, id: app.id, vm: app.vm }; | |
} | |
function _unhandledErrorHandler(err) { | |
logger.error("Unhandled error was occurred: ", err); | |
} | |
})(projectName.FormBuilder, projectName.FormBuilder.EventBus, lodash, projectName.FormBuilder.Logger, Q); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment