Refactored edit description directive
parent
bb0b9fb86e
commit
5b681cf82f
|
@ -515,69 +515,63 @@ module.directive("tgEditableSubject", ["$rootScope", "$tgRepo", "$tgConfirm", "$
|
||||||
#############################################################################
|
#############################################################################
|
||||||
|
|
||||||
EditableDescriptionDirective = ($rootscope, $repo, $confirm, $compile, $loading) ->
|
EditableDescriptionDirective = ($rootscope, $repo, $confirm, $compile, $loading) ->
|
||||||
viewTemplate = _.template("""
|
template = """
|
||||||
<section class="us-content wysiwyg"><%= descriptionHtml %></section>
|
<div class="view-description">
|
||||||
<% if (canEdit) { %>
|
<section class="us-content wysiwyg" tg-bind-html="item.description_html"></section>
|
||||||
<span class="edit icon icon-edit" href="" title="Edit" />
|
<span class="edit icon icon-edit" href="" title="Edit" />
|
||||||
<% } %>
|
</div>
|
||||||
""")
|
<div class="edit-description">
|
||||||
|
|
||||||
editTemplate = _.template("""
|
|
||||||
<textarea placeholder="Write a description of your user story"
|
<textarea placeholder="Write a description of your user story"
|
||||||
ng-model="item.description"
|
ng-model="item.description"
|
||||||
tg-markitup="tg-markitup"><%- item.description %></textarea>
|
tg-markitup="tg-markitup"></textarea>
|
||||||
<a class="save icon icon-floppy" href="" title="Save" />
|
<a class="save icon icon-floppy" href="" title="Save" />
|
||||||
""")
|
</div>
|
||||||
|
"""
|
||||||
|
|
||||||
link = ($scope, $el, $attrs, $model) ->
|
link = ($scope, $el, $attrs, $model) ->
|
||||||
editing = false
|
|
||||||
scope = $scope.$new()
|
|
||||||
|
|
||||||
render = ->
|
|
||||||
if editing
|
|
||||||
$el.html($compile(editTemplate({item: scope.item}))(scope))
|
|
||||||
else
|
|
||||||
canEdit = $scope.project.my_permissions.indexOf($attrs.requiredPerm) != -1
|
|
||||||
$el.html(viewTemplate({descriptionHtml: $model.$modelValue.description_html, canEdit: canEdit}))
|
|
||||||
|
|
||||||
$scope.$watch $attrs.ngModel, (item) ->
|
|
||||||
return if not item
|
|
||||||
render()
|
|
||||||
|
|
||||||
$scope.$on "$destroy", ->
|
$scope.$on "$destroy", ->
|
||||||
$el.off()
|
$el.off()
|
||||||
|
|
||||||
$el.on "click", ".edit", ->
|
$el.on "click", ".edit", ->
|
||||||
if not editing and $scope.project.my_permissions.indexOf($attrs.requiredPerm) != -1
|
$el.find('div.edit-description').show()
|
||||||
editing = true
|
$el.find('div.view-description').hide()
|
||||||
scope.item = {description: $model.$modelValue.description}
|
|
||||||
render()
|
|
||||||
$el.find('textarea').focus()
|
$el.find('textarea').focus()
|
||||||
|
|
||||||
$el.on "click", ".save", ->
|
$el.on "click", ".save", ->
|
||||||
$model.$modelValue.description = scope.item.description
|
$model.$modelValue.description = $scope.item.description
|
||||||
|
|
||||||
$loading.start($el.find('.save-container'))
|
$loading.start($el.find('.save-container'))
|
||||||
promise = $repo.save($model.$modelValue)
|
promise = $repo.save($model.$modelValue)
|
||||||
promise.then ->
|
promise.then ->
|
||||||
$confirm.notify("success")
|
$confirm.notify("success")
|
||||||
$rootscope.$broadcast("history:reload")
|
$rootscope.$broadcast("history:reload")
|
||||||
editing = false
|
$el.find('div.edit-description').hide()
|
||||||
$loading.finish($el.find('.save-container'))
|
$el.find('div.view-description').show()
|
||||||
render()
|
|
||||||
promise.then null, ->
|
promise.then null, ->
|
||||||
$confirm.notify("error")
|
$confirm.notify("error")
|
||||||
|
promise.finally ->
|
||||||
$loading.finish($el.find('.save-container'))
|
$loading.finish($el.find('.save-container'))
|
||||||
|
|
||||||
$el.on "keyup", "textarea", ->
|
$el.on "keyup", "textarea", ->
|
||||||
if event.keyCode == 27
|
if event.keyCode == 27
|
||||||
editing = false
|
$scope.item.revert()
|
||||||
$model.$modelValue.revert()
|
$el.find('div.edit-description').hide()
|
||||||
render()
|
$el.find('div.view-description').show()
|
||||||
|
|
||||||
|
$scope.$watch $attrs.ngModel, (value) ->
|
||||||
|
return if not value
|
||||||
|
$scope.item = value
|
||||||
|
if $scope.project.my_permissions.indexOf($attrs.requiredPerm) != -1
|
||||||
|
$el.find('div.view-description span.edit').show()
|
||||||
|
|
||||||
|
$el.find('div.edit-description').hide()
|
||||||
|
$el.find('div.view-description span.edit').hide()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
link: link
|
link: link
|
||||||
restrict: "EA"
|
restrict: "EA"
|
||||||
require: "ngModel"
|
require: "ngModel"
|
||||||
|
template: template
|
||||||
}
|
}
|
||||||
|
|
||||||
module.directive("tgEditableDescription", ["$rootScope", "$tgRepo", "$tgConfirm", "$compile", "$tgLoading",
|
module.directive("tgEditableDescription", ["$rootScope", "$tgRepo", "$tgConfirm", "$compile", "$tgLoading",
|
||||||
|
|
Loading…
Reference in New Issue