Refactor wiki views: Add tg-wiki-summary directive

stable
David Barragán Merino 2014-10-28 13:51:07 +01:00
parent b10178810f
commit 17475c59cc
3 changed files with 42 additions and 31 deletions

View File

@ -147,43 +147,64 @@ module.controller("WikiDetailController", WikiDetailController)
############################################################################# #############################################################################
## Wiki User Info Directive ## Wiki Summary Directive
############################################################################# #############################################################################
WikiUserInfoDirective = ($log) -> WikiSummaryDirective = ($log) ->
template = _.template(""" template = _.template("""
<figure class="avatar"> <ul>
<img src="<%= imgurl %>" alt="<%- name %>"> <li>
</figure> <span class="number"><%- totalEditions %></span>
<span class="description">last modification</span> <span class="description">times <br />edited</span>
<span class="username"><%- name %></span> </li>
<li>
<span class="number"><%- lastModifiedDate %></span>
<span class="description"> last <br />edit</span>
</li>
<li class="username-edition">
<figure class="avatar">
<img src="<%= user.imgUrl %>" alt="<%- user.name %>">
</figure>
<span class="description">last modification</span>
<span class="username"><%- user.name %></span>
</li>
</ul>
""") """)
link = ($scope, $el, $attrs) -> link = ($scope, $el, $attrs, $model) ->
if not $attrs.ngModel?
return $log.error "WikiUserDirective: no ng-model attr is defined"
render = (wiki) -> render = (wiki) ->
if not $scope.usersById? if not $scope.usersById?
$log.error "WikiUserDirective requires userById set in scope." $log.error "WikiSummaryDirective requires userById set in scope."
else else
user = $scope.usersById[wiki.last_modifier] user = $scope.usersById[wiki.last_modifier]
if user is undefined
ctx = {name: "unknown", imgurl: "/images/unnamed.png"}
else
ctx = {name: user.full_name_display, imgurl: user.photo}
if user is undefined
user = {name: "unknown", imgUrl: "/images/unnamed.png"}
else
user = {name: user.full_name_display, imgUrl: user.photo}
ctx = {
totalEditions: wiki.editions
lastModifiedDate: moment(wiki.modified_date).format("DD MMM YYYY HH:mm")
user: user
}
html = template(ctx) html = template(ctx)
$el.html(html) $el.html(html)
bindOnce($scope, $attrs.ngModel, render) $scope.$watch $attrs.ngModel, (wikiPage) ->
return if not wikiPage
render(wikiPage)
$scope.$on "$destroy", ->
$el.off()
return { return {
link: link link: link
restrict: "AE" restrict: "EA"
require: "ngModel"
} }
module.directive("tgWikiUserInfo", ["$tgRepo", "$log", "$tgLocation", "$tgConfirm", WikiUserInfoDirective]) module.directive("tgWikiSummary", ["$log", WikiSummaryDirective])
############################################################################# #############################################################################

View File

@ -1,9 +0,0 @@
div.summary.wiki-summary(ng-if="wiki.id")
ul
li
span.number(tg-bo-bind="wiki.editions")
span.description times <br />edited
li(ng-if="wiki.modified_date")
span.number(tg-bo-bind="wiki.modified_date|momentFormat:'DD MMM YYYY HH:mm'")
span.description last <br />edit
li.username-edition(tg-wiki-user-info, ng-model='wiki')

View File

@ -15,8 +15,7 @@ block content
span.green Wiki span.green Wiki
span.wiki-title(tg-bo-bind='wikiSlug|unslugify') span.wiki-title(tg-bo-bind='wikiSlug|unslugify')
include views/modules/wiki-summary div.summary.wiki-summary(tg-wiki-summary, ng-model="wiki", ng-if="wiki.id")
section.wiki-content(tg-editable-wiki-content, ng-model="wiki")
section.wiki-content(tg-editable-wiki-content ng-model="wiki")
tg-attachments(ng-model="wiki", type="wiki_page", ng-if="wiki.id") tg-attachments(ng-model="wiki", type="wiki_page", ng-if="wiki.id")