forked from andreaskoch/allmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
80 lines (59 loc) · 1.59 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2014 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package viewmodel
import (
"sort"
)
type Model struct {
Base
Content string `json:"content"`
Markdown string `json:"markdown"`
Publisher Publisher `json:"publisher"`
Author Author `json:"author"`
Children []Base `json:"children"`
ToplevelNavigation ToplevelNavigation `json:"toplevelNavigation"`
BreadcrumbNavigation BreadcrumbNavigation `json:"breadcrumbNavigation"`
ItemNavigation ItemNavigation `json:"itemNavigation"`
Tags []Tag `json:"tags"`
TagCloud TagCloud `json:"tagCloud"`
Files []File `json:"files"`
Images []Image `json:"images"`
GeoLocation GeoLocation `json:"geoLocation"`
Analytics Analytics `json:"-"`
Hash string `json:"hash"`
IsRepositoryItem bool
}
func Error(title, content, route string) Model {
return Model{
Base: Base{
Level: 0,
Title: title,
Route: route,
Type: "error",
BaseURL: "/",
},
Content: content,
}
}
type SortModelBy func(model1, model2 Model) bool
func (by SortModelBy) Sort(models []Model) {
sorter := &modelSorter{
models: models,
by: by,
}
sort.Sort(sorter)
}
type modelSorter struct {
models []Model
by SortModelBy
}
func (sorter *modelSorter) Len() int {
return len(sorter.models)
}
func (sorter *modelSorter) Swap(i, j int) {
sorter.models[i], sorter.models[j] = sorter.models[j], sorter.models[i]
}
func (sorter *modelSorter) Less(i, j int) bool {
return sorter.by(sorter.models[i], sorter.models[j])
}