-
Notifications
You must be signed in to change notification settings - Fork 2
/
contentblocks.js
161 lines (146 loc) · 4.63 KB
/
contentblocks.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// Url to your REST CMS web service 'find' interface. This is the method that checks if a content block already exists.
// The service should accept GET/POST/PUT/UPDATE/DELETE with a JSON object. [id] will be replaced by the CMS content block id.
// Example: http://your.service.com/api/findById?id=[id]
//
var restUrl = 'http://localhost:3000/cms/find?q={"@subject": "[id]"}';
var apiKey = 'ykwLDRspn5XgZPkO'; // Unique key for the demo REST web service, red-ant.herokuapp.com. If you use the demo service, change this key to be unique. If using your own service, you can leave this blank.
var editor = 'aloha'; // Editor can be aloha or hallo.
var loaded = false;
$(document).ready(function () {
if (editor == 'aloha') {
// Enable Aloha editor.
$('body').midgardCreate({
// Use Aloha for all text editing
editorWidgets: {
'default': 'aloha'
},
editorOptions: {
aloha: {
widget: 'alohaWidget'
}
},
collectionWidgets: {
'default': null,
'feature': 'midgardCollectionAdd'
},
state: 'edit'
});
}
else {
// Enable Hallo editor.
$('body').midgardCreate({
metadata: {
midgardTags: {}
},
collectionWidgets: {
'default': 'midgardCollectionAdd',
'skos:related': null
},
state: 'edit'
});
}
// Set a simpler editor for title fields
$('body').midgardCreate('configureEditor', 'title', 'halloWidget', {
plugins: {
halloformat: {},
halloblacklist: {
tags: ['br']
}
}
});
$('body').midgardCreate('setEditorForProperty', 'dcterms:title', 'title');
// Disable editing of author fields
$('body').midgardCreate('setEditorForProperty', 'dcterms:author', null);
});
// Backbone.sync
Backbone.sync = function (method, model, options) {
// Include web service key.
CommonManager.addApiKey(model, apiKey);
if (method == 'create') {
$.ajax({
url: '/create',
type: 'POST',
data: CommonManager.encodeId(JSON.stringify(model)),
contentType: 'application/json',
dataType: 'json',
success: function (data) {
options.success(model);
}
});
}
else if (method == 'read') {
}
else if (method == 'update') {
$.ajax({
url: restUrl.replace('[id]', CommonManager.encodeId(model.id)),
type: 'GET',
dataType: 'json',
success: function (data) {
if (data.length == 0) {
// Create new entry.
$.ajax({
url: '/create',
type: 'POST',
data: CommonManager.encodeId(JSON.stringify(model)),
contentType: 'application/json',
dataType: 'json',
success: function (data) {
options.success(model);
}
});
}
else {
// Update existing entry.
$.ajax({
url: '/create/' + data[0]._id,
type: 'PUT',
data: CommonManager.encodeId(JSON.stringify(model)),
contentType: 'application/json',
dataType: 'json',
success: function (data) {
options.success(model);
}
});
}
}
});
}
else if (method == 'delete') {
$.ajax({
url: '/create/' + data[0]._id,
type: 'DELETE',
contentType: 'application/json',
dataType: 'json',
success: function (data) {
options.success(model);
}
});
}
};
CommonManager = {
encodeId: function (id) {
id = id.replace(/\./g, '_p').replace(/\//g, '_s');
return id;
},
decodeId: function (id) {
id = id.replace(/_p/g, '.').replace(/_s/g, '/');
return id;
},
addApiKey: function (model, apikey) {
if (apiKey.length > 0) {
model['apiKey'] = apiKey;
if (!loaded) {
// Copy original stringify method.
model.toJSONOld = model.toJSON;
// Override method to include apiKey.
model.toJSON = function () {
var j = model.toJSONOld();
j.apiKey = model.apiKey;
return j;
}
loaded = true;
}
}
}
};