**********************************
**********************************
***** document
**********************************
**********************************

{{layout/zone-navigation}}

{{#layout/zone-sidebar}}
	{{document/document-sidebar document=model.document folder=model.folder pages=model.pages page=model.page isEditor=model.isEditor sections=model.sections
		onAddSection=(action 'onAddSection') onInsertBlock=(action 'onInsertBlock') onDeleteBlock=(action 'onDeleteBlock') changePageSequence=(action 'onPageSequenceChange') changePageLevel=(action 'onPageLevelChange') gotoPage=(action 'gotoPage')}}
{{/layout/zone-sidebar}}

{{#layout/zone-content}}
	{{document/document-toolbar document=model.document pages=model.pages tabs=model.tabs folder=model.folder isEditor=model.isEditor
		onSaveTemplate=(action 'onSaveTemplate') onSaveMeta=(action 'onSaveMeta')  onDocumentDelete=(action 'onDocumentDelete')}}

		{{outlet}}
{{/layout/zone-content}}

// Copyright 2016 Documize Inc. <legal@documize.com>. 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 <sales@documize.com>.
//
// https://documize.com

import Ember from 'ember';
import NotifierMixin from '../../mixins/notifier';

export default Ember.Controller.extend(NotifierMixin, {
	documentService: Ember.inject.service('document'),
	templateService: Ember.inject.service('template'),
	sectionService: Ember.inject.service('section'),
	page: null,
	folder: {},
	pages: [],

	// Jump to the right part of the document.
	scrollToPage(pageId) {
		Ember.run.schedule('afterRender', function () {
			let dest;
			let target = "#page-title-" + pageId;
			let targetOffset = $(target).offset();

			if (is.undefined(targetOffset)) {
				return;
			}

			dest = targetOffset.top > $(document).height() - $(window).height() ? $(document).height() - $(window).height() : targetOffset.top;
			// small correction to ensure we also show page title
			dest = dest > 50 ? dest - 74 : dest;

			$("html,body").animate({
				scrollTop: dest
			}, 500, "linear");
			$(".toc-index-item").removeClass("selected");
			$("#index-" + pageId).addClass("selected");
		});
	},

	actions: {
		gotoPage(pageId) {
			if (is.null(pageId)) {
				return;
			}

			this.scrollToPage(pageId);
		},

		onPageSequenceChange(changes) {
			this.get('documentService').changePageSequence(this.get('model.document.id'), changes).then(() => {
				_.each(changes, (change) => {
					let pageContent = _.findWhere(this.get('model.pages'), {
						id: change.pageId
					});

					if (is.not.undefined(pageContent)) {
						pageContent.set('sequence', change.sequence);
					}
				});

				this.set('model.pages', this.get('model.pages').sortBy('sequence'));
				this.get('target.router').refresh();
			});
		},

		onPageLevelChange(changes) {
			this.get('documentService').changePageLevel(this.get('model.document.id'), changes).then(() => {
				_.each(changes, (change) => {
					let pageContent = _.findWhere(this.get('model.pages'), {
						id: change.pageId
					});

					if (is.not.undefined(pageContent)) {
						pageContent.set('level', change.level);
					}
				});

				let pages = this.get('model.pages');
				pages = pages.sortBy('sequence');
				this.set('model.pages', []);
				this.set('model.pages', pages);
				this.get('target.router').refresh();
			});
		},

		onSaveTemplate(name, desc) {
			this.get('templateService').saveAsTemplate(this.get('model.document.id'), name, desc).then(function () {});
		},

		onSaveMeta(doc) {
			this.get('documentService').save(doc).then(() => {
				this.transitionToRoute('document.index');
			});
		},

		onAddSection(section) {
			this.audit.record("added-section-" + section.get('contentType'));

			let page = {
				documentId: this.get('model.document.id'),
				title: `${section.get('title')}`,
				level: 1,
				sequence: 0,
				body: "",
				contentType: section.get('contentType'),
				pageType: section.get('pageType')
			};

			let meta = {
				documentId: this.get('model.document.id'),
				rawBody: "",
				config: ""
			};

			let model = {
				page: page,
				meta: meta
			};

			this.get('documentService').addPage(this.get('model.document.id'), model).then((newPage) => {
				let data = this.get('store').normalize('page', newPage);
				this.get('store').push(data);

				this.get('documentService').getPages(this.get('model.document.id')).then((pages) => {
					this.set('model.pages', pages.filterBy('pageType', 'section'));
					this.set('model.tabs', pages.filterBy('pageType', 'tab'));

					this.get('documentService').getPageMeta(this.get('model.document.id'), newPage.id).then(() => {
						this.transitionToRoute('document.edit',
							this.get('model.folder.id'),
							this.get('model.folder.slug'),
							this.get('model.document.id'),
							this.get('model.document.slug'),
							newPage.id);
					});
				});
			});
		},

		onInsertBlock(block) {
			this.audit.record("added-content-block-" + block.get('contentType'));

			let page = {
				documentId: this.get('model.document.id'),
				title: `${block.get('title')}`,
				level: 1,
				sequence: 0,
				body: block.get('body'),
				contentType: block.get('contentType'),
				pageType: block.get('pageType'),
				blockId: block.get('id')
			};

			let meta = {
				documentId: this.get('model.document.id'),
				rawBody: block.get('rawBody'),
				config: block.get('config'),
				externalSource: block.get('externalSource')
			};

			let model = {
				page: page,
				meta: meta
			};

			this.get('documentService').addPage(this.get('model.document.id'), model).then((newPage) => {
				let data = this.get('store').normalize('page', newPage);
				this.get('store').push(data);

				this.get('documentService').getPages(this.get('model.document.id')).then((pages) => {
					this.set('model.pages', pages.filterBy('pageType', 'section'));
					this.set('model.tabs', pages.filterBy('pageType', 'tab'));

					this.get('documentService').getPageMeta(this.get('model.document.id'), newPage.id).then(() => {
						this.transitionToRoute('document.edit',
							this.get('model.folder.id'),
							this.get('model.folder.slug'),
							this.get('model.document.id'),
							this.get('model.document.slug'),
							newPage.id);
					});
				});
			});
		},

		onDeleteBlock(blockId) {
			this.get('sectionService').deleteBlock(blockId).then(() => {
				this.audit.record("deleted-block");
				this.send("showNotification", "Deleted");
				this.transitionToRoute('document.index');
			});
		},

		onDocumentDelete() {
			this.get('documentService').deleteDocument(this.get('model.document.id')).then(() => {
				this.audit.record("deleted-page");
				this.send("showNotification", "Deleted");
				this.transitionToRoute('folder', this.get('model.folder.id'), this.get('model.folder.slug'));
			});
		}
	}
});


**********************************
**********************************
***** document/index
**********************************
**********************************

{{document/document-view document=model.document links=model.links allPages=model.allPages tabs=model.tabs pages=model.pages folder=model.folder folders=model.folders isEditor=model.isEditor gotoPage=(action 'gotoPage') onAddBlock=(action 'onAddBlock') onCopyPage=(action 'onCopyPage') onMovePage=(action 'onMovePage') onDeletePage=(action 'onPageDeleted')}}


// Copyright 2016 Documize Inc. <legal@documize.com>. 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 <sales@documize.com>.
//
// https://documize.com

import Ember from 'ember';
import NotifierMixin from '../../../mixins/notifier';

export default Ember.Controller.extend(NotifierMixin, {
	documentService: Ember.inject.service('document'),
	sectionService: Ember.inject.service('section'),
	queryParams: ['page'],

	// Jump to the right part of the document.
	scrollToPage(pageId) {
		Ember.run.schedule('afterRender', function () {
			let dest;
			let target = "#page-title-" + pageId;
			let targetOffset = $(target).offset();

			if (is.undefined(targetOffset)) {
				return;
			}

			dest = targetOffset.top > $(document).height() - $(window).height() ? $(document).height() - $(window).height() : targetOffset.top;
			// small correction to ensure we also show page title
			dest = dest > 50 ? dest - 74 : dest;

			$("html,body").animate({
				scrollTop: dest
			}, 500, "linear");
			$(".toc-index-item").removeClass("selected");
			$("#index-" + pageId).addClass("selected");
		});
	},

	actions: {
		gotoPage(pageId) {
			if (is.null(pageId)) {
				return;
			}

			this.scrollToPage(pageId);
		},

		onPageSequenceChange(changes) {
			this.get('documentService').changePageSequence(this.get('model.document.id'), changes).then(() => {
				_.each(changes, (change) => {
					let pageContent = _.findWhere(this.get('model.pages'), {
						id: change.pageId
					});

					if (is.not.undefined(pageContent)) {
						pageContent.set('sequence', change.sequence);
					}
				});

				this.set('model.pages', this.get('model.pages').sortBy('sequence'));
				this.get('target.router').refresh();
			});
		},

		onPageLevelChange(changes) {
			this.get('documentService').changePageLevel(this.get('model.document.id'), changes).then(() => {
				_.each(changes, (change) => {
					let pageContent = _.findWhere(this.get('model.pages'), {
						id: change.pageId
					});

					if (is.not.undefined(pageContent)) {
						pageContent.set('level', change.level);
					}
				});

				let pages = this.get('model.pages');
				pages = pages.sortBy('sequence');
				this.set('model.pages', pages);

				this.get('target.router').refresh();
			});
		},

		onAddBlock(block) {
			this.get('sectionService').addBlock(block).then(() => {
				this.showNotification("Published");
			});
		},

		onCopyPage(pageId, targetDocumentId) {
			let documentId = this.get('model.document.id');
			this.get('documentService').copyPage(documentId, pageId, targetDocumentId).then(() => {
				this.showNotification("Copied");

				// refresh data if copied to same document
				if (documentId === targetDocumentId) {
					this.get('target.router').refresh();
				}
			});
		},

		onMovePage(pageId, targetDocumentId) {
			let documentId = this.get('model.document.id');

			this.get('documentService').copyPage(documentId, pageId, targetDocumentId).then(() => {
				this.showNotification("Moved");

				this.send('onPageDeleted', { id: pageId, children: false });
			});
		},

		onPageDeleted(deletePage) {
			let documentId = this.get('model.document.id');
			let pages = this.get('model.pages');
			let deleteId = deletePage.id;
			let deleteChildren = deletePage.children;
			let page = _.findWhere(pages, {
				id: deleteId
			});
			let pageIndex = _.indexOf(pages, page, false);
			let pendingChanges = [];

			this.audit.record("deleted-page");

			// select affected pages
			for (var i = pageIndex + 1; i < pages.get('length'); i++) {
				if (pages[i].get('level') <= page.get('level')) {
					break;
				}

				pendingChanges.push({
					pageId: pages[i].get('id'),
					level: pages[i].get('level') - 1
				});
			}

			if (deleteChildren) {
				// nuke of page tree
				pendingChanges.push({
					pageId: deleteId
				});

				this.get('documentService').deletePages(documentId, deleteId, pendingChanges).then(() => {
					// update our models so we don't have to reload from db
					for (var i = 0; i < pendingChanges.length; i++) {
						let pageId = pendingChanges[i].pageId;
						this.set('model.pages', _.reject(pages, function (p) { //jshint ignore: line
							return p.get('id') === pageId;
						}));
					}

					this.set('model.pages', _.sortBy(pages, "sequence"));
					this.get('target.router').refresh();
				});
			} else {
				// page delete followed by re-leveling child pages
				this.get('documentService').deletePage(documentId, deleteId).then(() => {
					this.set('model.pages', _.reject(pages, function (p) {
						return p.get('id') === deleteId;
					}));

					this.send('onPageLevelChange', pendingChanges);
				});
			}
		}
	}
});
