Created
January 20, 2016 18:13
-
-
Save pedrohugorm/414f538fbe663c5d7e30 to your computer and use it in GitHub Desktop.
This file contains 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
class ActionHandler | |
parent: null | |
handle: (action) -> | |
return null | |
class AlertHandler extends ActionHandler | |
class ConfirmHandler extends ActionHandler | |
class ChangeStateHandler extends ActionHandler | |
class RedirectHandler extends ActionHandler | |
class ValidationHandler extends ActionHandler | |
class ModalHandler extends ActionHandler | |
class EnvelopeInterpreter | |
constructor: (handleAlert, handleConfirm, handleChangeState, handleRedirect, handleValidation, handleModal) -> | |
@handleAlert = handleAlert or new DefaultAlertHandler() | |
@handleConfirm = handleConfirm or new DefaultConfirmHandler() | |
@handleChangeState = handleChangeState or new DefaultChangeStateHandler() | |
@handleRedirect = handleRedirect or new DefaultRedirectHandler() | |
@handleValidation = handleValidation or new DefaultValidationHandler() | |
@handleModal = handleModal or new DefaultModalHandler() | |
@clearResultList() | |
return | |
clearResultList: () -> | |
@resultList = [] | |
parse: (action) -> | |
@parseAction action | |
parseAction: (action) -> | |
if not action | |
return | |
@["handle" + action.Name].parent = @ | |
result = @["handle" + action.Name].handle action | |
@parseAction result | |
class DefaultAlertHandler extends AlertHandler | |
handle: (action) -> | |
window.alert action.Message | |
return action.OkAction | |
class DefaultConfirmHandler extends ConfirmHandler | |
handle: (action) -> | |
if window.confirm action.Message | |
return action.OkAction | |
else return action.CancelAction | |
class DefaultRedirectHandler extends RedirectHandler | |
handle: (action) -> | |
window.location action.RedirectUrl | |
class DefaultValidationHandler extends ValidationHandler | |
handle: (action) -> | |
return null if !parent | |
parent.resultList.push({ action: action.Name, result: action.ValidationErrors }) | |
class DefaultChangeStateHandler extends ChangeStateHandler | |
handle: (action) -> | |
return | |
class DefaultModalHandler extends ModalHandler | |
handle: (action) -> | |
return | |
### | |
Parsers | |
### | |
class UiParserWrapper | |
constructor: ($q, $state) -> | |
return (action) -> | |
parser = new EnvelopeInterpreter() | |
parser.clearResultList() | |
return $q.when(null) if !action | |
parser.parse action | |
return $q.when parser.resultList | |
class ResourceUiParserDecorator | |
constructor: (uiParserWrapper, $q, $log, _) -> | |
return ($resource) -> | |
getValidationErrorList = (result) -> | |
return _.find(result, (item) -> return item.action == "Validation").result | |
validationErrorsDefered = $q["defer"]() | |
resourceDecorated = { | |
resolveValidationErrors: (result) -> | |
if result.Action.Name == "Validation" | |
parseResult.then (validationResult) -> | |
errorList = getValidationErrorList validationResult | |
validationErrorsDefered.resolve errorList | |
return | |
getValidationErrorPromise: () -> | |
return validationErrorsDefered.promise; | |
query: $resource.query | |
get: $resource.get | |
save: (o) -> | |
defered = $resource.save(o) | |
defered.$promise.then (result) -> | |
return if !result || !result.Action | |
parseResult = uiParserWrapper(result.Action) | |
resourceDecorated.resolveValidationErrors result | |
return defered | |
delete: (o) -> | |
defered = $resource.save(o) | |
defered.$promise.then (result) -> | |
return if !result || !result.Action | |
parseResult = uiParserWrapper(result.Action) | |
resourceDecorated.resolveValidationErrors result | |
return defered | |
remove: (o) -> | |
resourceDecorated.delete o | |
onValidationErrors: (list) -> | |
$log.info "Validation Errors", list | |
} | |
return resourceDecorated | |
if angular | |
ngModule = angular.module "EnvelopeInterpreter", ["underscore"] | |
ngModule.factory "EnvelopeInterpreterFactory", [ | |
() -> | |
return EnvelopeInterpreter | |
] | |
ngModule.factory "UiParserWrapper", ["$q", "$state", UiParserWrapper] | |
ngModule.factory "ResourceUiParserDecorator", ["UiParserWrapper", "$q", "$log", "_", ResourceUiParserDecorator] | |
### | |
if module | |
module.exports = (config) -> | |
@EnvelopeInterpreter = EnvelopeInterpreter | |
@ActionHandler = ActionHandler | |
@AlertHandler = AlertHandler | |
@ConfirmHandler = ConfirmHandler | |
@ChangeStateHandler = ChangeStateHandler | |
@RedirectHandler = RedirectHandler | |
@ValidationHandler = ValidationHandler | |
return | |
### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment