forked from aaronmars/martian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft.js
82 lines (73 loc) · 2.96 KB
/
draft.js
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
81
82
import { Plug } from 'mindtouch-http.js/plug.js';
import { Settings } from './lib/settings.js';
import { utility } from './lib/utility.js';
import { modelParser } from './lib/modelParser.js';
import { PageBase } from './pageBase.js';
import { pageModel } from './models/page.model.js';
/**
* A class for managing a single unpublished draft page.
*/
export class Draft extends PageBase {
/**
* Construct a Draft object.
* @param {Number|String} [id=home] - The id of the draft to construct.
* @param {Settings} [settings] - The {@link Settings} information to use in construction. If not supplied, the default settings are used.
*/
constructor(id = 'home', settings = new Settings()) {
super(id);
this._settings = settings;
this._plug = new Plug(settings.host, settings.plugConfig).at('@api', 'deki', 'drafts', this._id);
}
/**
* Deactivate the current draft and revert to the published page.
* @returns {Promise.<pageModel>} - A Promise that, when resolved, yields a {@link pageModel} for the deactivated page.
*/
deactivate() {
let pageModelParser = modelParser.createParser(pageModel);
return this._plug.at('deactivate').post().then((r) => r.json()).then(pageModelParser);
}
/**
* Publish the draft.
* @returns {Promise} - A Promise that, when resolved, indicates a successful publish operation.
*/
publish() {
return this._plug.at('publish').post();
}
/**
* Unpublish a live page and create a draft out of it.
* @returns {Promise.<pageModel>} - A Promise that, when resolved, yields a {@link pageModel} for the unpublished page.
*/
unpublish() {
return this._plug.at('unpublish').post().then(modelParser.createParser(pageModel));
}
}
/**
* A class for managing unpublished draft pages.
*/
export class DraftManager {
/**
* Create a new DraftManager.
* @param {Settings} [settings] - The {@link Settings} information to use in construction. If not supplied, the default settings are used.
*/
constructor(settings = new Settings()) {
this._settings = settings;
}
/**
* Create a new draft on the site where a page does not already exist.
* @param {String} newPath - The path of the new draft.
* @returns {Promise.<pageModel>} - A Promise that, when resolved, yields a {@link pageModel} for the newly-created draft.
*/
createDraft(newPath) {
let plug = new Plug(this._settings.host, this._settings.plugConfig).at('@api', 'deki', 'drafts', utility.getResourceId(newPath), 'create');
let pageModelParser = modelParser.createParser(pageModel);
return plug.post().then((r) => r.json()).then(pageModelParser);
}
/**
* Fetch a new Draft object by ID.
* @param {Number|String} [id=home] - The id of the draft to return.
* @returns {Draft} - A new {@link Draft} object.
*/
getDraft(id) {
return new Draft(id, this._settings);
}
}