diff --git a/core/database/database_test.go b/core/database/database_test.go index ceb0f17a..6ef48db4 100644 --- a/core/database/database_test.go +++ b/core/database/database_test.go @@ -13,24 +13,6 @@ package database import "testing" -// -// // go test github.com/documize/community/core/database -run TestVersionExtract -// func TestVersionExtract(t *testing.T) { -// ts(t, "5", 5) -// ts(t, "45-0ubuntu0-12.12.3", 45) -// ts(t, "untu0-12.12.3", 0) -// ts(t, "junk-string", 0) -// ts(t, "somethingstring", 0) -// } -// -// func ts(t *testing.T, in string, out int) { -// got := ExtractVersionNumber(in) -// -// if got != out { -// t.Errorf("version input `%s` got `%d` expected `%d`\n", in, got, out) -// } -// } - // go test github.com/documize/community/core/database -run TestGetVersion func TestGetVersion(t *testing.T) { ts2(t, "5.7.10", []int{5, 7, 10}) diff --git a/domain/page/endpoint.go b/domain/page/endpoint.go index d7880aa1..13f05453 100644 --- a/domain/page/endpoint.go +++ b/domain/page/endpoint.go @@ -191,7 +191,7 @@ func (h *Handler) GetPage(w http.ResponseWriter, r *http.Request) { // GetPages gets all pages for document. func (h *Handler) GetPages(w http.ResponseWriter, r *http.Request) { - method := "page.GetPage" + method := "page.GetPages" ctx := domain.GetRequestContext(r) documentID := request.Param(r, "documentID") @@ -219,6 +219,8 @@ func (h *Handler) GetPages(w http.ResponseWriter, r *http.Request) { pages = []page.Page{} } + page.Numberize(pages) + if err != nil { response.WriteServerError(w, method, err) h.Runtime.Log.Error(method, err) diff --git a/gui/app/components/section/code/type-editor.js b/gui/app/components/section/code/type-editor.js index 2c4760dc..980d484c 100644 --- a/gui/app/components/section/code/type-editor.js +++ b/gui/app/components/section/code/type-editor.js @@ -10,7 +10,6 @@ // https://documize.com import { computed } from '@ember/object'; - import Component from '@ember/component'; import TooltipMixin from '../../../mixins/tooltip'; @@ -102,7 +101,7 @@ export default Component.extend(TooltipMixin, { this.set('codeEditor', null); } - this.destroyTooltips(); + this.removeT0oltips(); }, diff --git a/gui/app/components/section/trello/type-editor.js b/gui/app/components/section/trello/type-editor.js index 61a900cf..9cda70ea 100644 --- a/gui/app/components/section/trello/type-editor.js +++ b/gui/app/components/section/trello/type-editor.js @@ -88,7 +88,7 @@ export default Component.extend(SectionMixin, NotifierMixin, TooltipMixin, { }, willDestroyElement() { - this.destroyTooltips(); + this.removeTooltips(); }, getBoardLists() { diff --git a/gui/app/models/page.js b/gui/app/models/page.js index 95b66e25..643c585f 100644 --- a/gui/app/models/page.js +++ b/gui/app/models/page.js @@ -21,13 +21,14 @@ export default Model.extend({ pageType: attr('string'), level: attr('number', { defaultValue: 1 }), sequence: attr('number', { defaultValue: 0 }), + numbering: attr('string'), revisions: attr('number', { defaultValue: 0 }), blockId: attr('string'), title: attr('string'), body: attr('string'), rawBody: attr('string'), meta: attr(), - + tagName: computed('level', function () { return "h2"; // return "h" + (this.get('level') + 1); diff --git a/gui/app/templates/components/document/page-heading.hbs b/gui/app/templates/components/document/page-heading.hbs index dc152581..43e68e67 100644 --- a/gui/app/templates/components/document/page-heading.hbs +++ b/gui/app/templates/components/document/page-heading.hbs @@ -3,7 +3,7 @@
diff --git a/model/page/numbering.go b/model/page/numbering.go new file mode 100644 index 00000000..0140927d --- /dev/null +++ b/model/page/numbering.go @@ -0,0 +1,74 @@ +// Copyright 2016 Documize Inc. . All rights reserved. +// +// This software (Documize Community Edition) is licensed under +// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html +// +// You can operate outside the AGPL restrictions by purchasing +// Documize Enterprise Edition and obtaining a commercial license +// by contacting . +// +// https://documize.com + +package page + +import ( + "fmt" +) + +// Numberize calculates numbers for pages, e.g. 1, 1.1, 2.2.1 +// that form a document's Table of Contents. +// +func Numberize(pages []Page) { + // index := 1 + var prevPageLevel uint64 + parts := []int{1} // we store 1, 1, 2, and then generate numbering as "1.1.2" + + for i, p := range pages { + // handle bad data + if p.Level == 0 { + p.Level = 1 + } + + // increment level numbers each time we encounter age level of 1 + // if p.Level == 1 { + // index++ + // parts = []int{index} + // } else if p.Level == prevPageLevel { + // parts[len(parts)-1]++ + // } + + if i != 0 { + // we ... + if p.Level > prevPageLevel { + parts = append(parts, 1) + } + + if p.Level == prevPageLevel { + parts[len(parts)-1]++ + } + + if p.Level < prevPageLevel { + end := (prevPageLevel - p.Level) + if int(end) > len(parts) { + end = uint64(len(parts)) + } + parts = parts[0 : len(parts)-int(end)] + parts[len(parts)-1]++ + } + } + + // generate numbering for page using parts array + numbering := "" + for i, v := range parts { + dot := "" + if i != len(parts)-1 { + dot = "." + } + numbering = fmt.Sprintf("%s%d%s", numbering, v, dot) + } + pages[i].Numbering = numbering + + // update state + prevPageLevel = p.Level + } +} diff --git a/model/page/numbering_test.go b/model/page/numbering_test.go new file mode 100644 index 00000000..f9606329 --- /dev/null +++ b/model/page/numbering_test.go @@ -0,0 +1,114 @@ +// Copyright 2016 Documize Inc. . All rights reserved. +// +// This software (Documize Community Edition) is licensed under +// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html +// +// You can operate outside the AGPL restrictions by purchasing +// Documize Enterprise Edition and obtaining a commercial license +// by contacting . +// +// https://documize.com + +package page + +import ( + "testing" +) + +func TestNumberize1(t *testing.T) { + pages := []Page{} + + pages = append(pages, Page{Level: 1, Sequence: 1000}) + pages = append(pages, Page{Level: 2, Sequence: 2000}) + pages = append(pages, Page{Level: 2, Sequence: 3000}) + pages = append(pages, Page{Level: 2, Sequence: 4000}) + pages = append(pages, Page{Level: 3, Sequence: 5000}) + pages = append(pages, Page{Level: 3, Sequence: 6000}) + pages = append(pages, Page{Level: 4, Sequence: 7000}) + pages = append(pages, Page{Level: 4, Sequence: 8000}) + pages = append(pages, Page{Level: 5, Sequence: 9000}) + pages = append(pages, Page{Level: 3, Sequence: 10000}) + pages = append(pages, Page{Level: 2, Sequence: 11000}) + + Numberize(pages) + + expecting := []string{ + "1", + "1.1", + "1.2", + "1.3", + "1.3.1", + "1.3.2", + "1.3.2.1", + "1.3.2.2", + "1.3.2.2.1", + "1.3.3", + "1.4", + } + + for i, p := range pages { + if p.Numbering != expecting[i] { + t.Errorf("(Test 1) Position %d: expecting %s got %s\n", i, expecting[i], p.Numbering) + } + } +} + +func TestNumberize2(t *testing.T) { + pages := []Page{} + + pages = append(pages, Page{Level: 1, Sequence: 1000}) + pages = append(pages, Page{Level: 1, Sequence: 2000}) + pages = append(pages, Page{Level: 1, Sequence: 3000}) + pages = append(pages, Page{Level: 1, Sequence: 4000}) + pages = append(pages, Page{Level: 1, Sequence: 5000}) + pages = append(pages, Page{Level: 1, Sequence: 6000}) + + Numberize(pages) + + expecting := []string{ + "1", + "2", + "3", + "4", + "5", + "6", + } + + for i, p := range pages { + if p.Numbering != expecting[i] { + t.Errorf("(Test 2) Position %d: expecting %s got %s\n", i, expecting[i], p.Numbering) + } + } +} + +func TestNumberize3(t *testing.T) { + pages := []Page{} + + pages = append(pages, Page{Level: 0, Sequence: 1000}) + pages = append(pages, Page{Level: 1, Sequence: 2000}) + pages = append(pages, Page{Level: 2, Sequence: 3000}) + pages = append(pages, Page{Level: 3, Sequence: 4000}) + pages = append(pages, Page{Level: 4, Sequence: 4000}) + pages = append(pages, Page{Level: 1, Sequence: 5000}) + pages = append(pages, Page{Level: 2, Sequence: 6000}) + + Numberize(pages) + + expecting := []string{ + "1", + "2", + "2.1", + "2.1.1", + "2.1.1.1", + "3", + "3.1", + } + + for i, p := range pages { + if p.Numbering != expecting[i] { + t.Errorf("(Test 3) Position %d: expecting %s got %s\n", i, expecting[i], p.Numbering) + } + } +} + +// go test github.com/documize/community/core/model -run TestNumberize diff --git a/model/page/page.go b/model/page/page.go index 20f60ffd..2b8ea341 100644 --- a/model/page/page.go +++ b/model/page/page.go @@ -29,6 +29,7 @@ type Page struct { BlockID string `json:"blockId"` Level uint64 `json:"level"` Sequence float64 `json:"sequence"` + Numbering string `json:"numbering"` Title string `json:"title"` Body string `json:"body"` Revisions uint64 `json:"revisions"` @@ -102,7 +103,6 @@ type NewPage struct { Meta Meta `json:"meta"` } - // PageSequenceRequest details a page ID and its sequence within the document. type PageSequenceRequest struct { PageID string `json:"pageId"` @@ -113,4 +113,4 @@ type PageSequenceRequest struct { type PageLevelRequest struct { PageID string `json:"pageId"` Level int `json:"level"` -} \ No newline at end of file +}