/
plugin.js
193 lines (165 loc) · 6.04 KB
/
plugin.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*!
* Aloha Editor Midgard integration
* Author & Copyright (c) 2010 The Midgard Project
* dev@lists.midgard-project.org
* Licensed unter the terms of http://www.aloha-editor.com/license.html
*/
/**
* @author bergie
*/
if (typeof midgardproject == 'undefined') {
midgardproject = {};
}
midgardproject.SavePlugin = new GENTICS.Aloha.Plugin('org.midgardproject.aloha.plugins.Save');
/**
* Configure the available languages
*/
midgardproject.SavePlugin.languages = ['en','fi'];
/**
* Holder for Midgard objects (array indexed by GUID) that have been made editable on the page
*/
midgardproject.SavePlugin.objects = [];
midgardproject.SavePlugin.enablerButton = {};
midgardproject.SavePlugin.disablerButton = {};
/**
* Initialize the plugin and set initialize flag on true
*/
midgardproject.SavePlugin.init = function () {
// remember reference to this class for callback
var that = this;
// create save button to ribbon
var saveButton = new GENTICS.Aloha.ui.Button({
label : this.i18n('save'),
onclick : function() {
that.save();
}
});
midgardproject.SavePlugin.enablerButton = new GENTICS.Aloha.ui.Button({
label : this.i18n('enable editing'),
onclick : function() {
that.enableEditables();
}
});
midgardproject.SavePlugin.disablerButton = new GENTICS.Aloha.ui.Button({
label : this.i18n('disable editing'),
onclick : function() {
that.disableEditables();
}
});
// add buttons to ribbon
GENTICS.Aloha.Ribbon.addButton(midgardproject.SavePlugin.enablerButton);
GENTICS.Aloha.Ribbon.addButton(midgardproject.SavePlugin.disablerButton);
GENTICS.Aloha.Ribbon.addButton(saveButton);
if (Modernizr.sessionstorage) {
// Check if user is in editing state
var editorState = sessionStorage.getItem('midgardmvc_ui_create_state');
if (editorState == 'edit')
{
midgardproject.SavePlugin.enableEditables();
}
}
};
midgardproject.SavePlugin.enableEditable = function(objectContainer) {
var editableObject = {};
editableObject.identifier = objectContainer.attr('about');
editableObject.type = objectContainer.attr('typeof');
editableObject.baseurl = objectContainer.attr('mgd:baseurl');
editableObject.container = objectContainer;
// Seek editable properties
editableObject.properties = {};
var objectProperties = jQuery('*', objectContainer).filter(function() {
return jQuery(this).attr('property');
});
jQuery.each(objectProperties, function(index, objectProperty)
{
var objectProperty = jQuery(objectProperty);
var propertyName = objectProperty.attr('property');
editableObject.properties[propertyName] = {
element: objectProperty,
aloha: new GENTICS.Aloha.Editable(objectProperty)
};
});
midgardproject.SavePlugin.objects[midgardproject.SavePlugin.objects.length] = editableObject;
};
midgardproject.SavePlugin.enableEditables = function() {
midgardproject.ContainersPlugin.find();
var objectContainers = jQuery('[typeof]');
jQuery.each(objectContainers, function(index, objectContainer)
{
var objectContainer = jQuery(objectContainer);
if (typeof objectContainer.attr('about') == 'undefined') {
// No identifier set, therefore not editable
return true;
}
midgardproject.SavePlugin.enableEditable(objectContainer);
});
if (Modernizr.sessionstorage) {
// Set session to editing state
sessionStorage.setItem('midgardmvc_ui_create_state', 'edit');
}
};
midgardproject.SavePlugin.disableEditables = function() {
if (Modernizr.sessionstorage) {
// Remove editing state
sessionStorage.removeItem('midgardmvc_ui_create_state');
}
jQuery.each(midgardproject.SavePlugin.objects, function(index, editableObject) {
jQuery.each(editableObject.properties, function(propertyName, editableProperty) {
editableProperty = jQuery(editableProperty.element);
editableProperty.mahalo();
});
});
midgardproject.SavePlugin.objects = [];
};
/**
* collect data and save
*/
midgardproject.SavePlugin.save = function () {
// iterate all Midgard objects which have been made Aloha editable
jQuery.each(midgardproject.SavePlugin.objects, function(objectIndex, editableObject) {
var saveObject = {
type: editableObject.type,
identifier: editableObject.identifier,
baseurl: editableObject.baseurl
};
var objectModified = false;
jQuery.each(editableObject.properties, function(index, editableProperty) {
if (editableProperty.aloha.isModified())
{
saveObject[index] = editableProperty.aloha.getContents();
objectModified = true;
}
});
if (!objectModified)
{
return true;
}
// Send the edited fields to the form handler backend
var url = '/mgd:create/save/json';
jQuery.ajax({
url: url,
dataType: 'json',
data: saveObject,
type: 'POST',
success: function (response) {
jQuery.each(editableObject.properties, function(index, editableProperty) {
editableProperty.aloha.setUnmodified();
editableObject.identifier = response.status.identifier;
editableObject.container.attr('about', response.status.identifier);
});
},
error: function (xhr, ajaxOptions, thrownError) {
try
{
response = jQuery.parseJSON(xhr.responseText);
message = response.status.message;
}
catch (e)
{
message = xhr.statusText;
}
GENTICS.Aloha.Log.error("Midgard saving plugin", message);
},
});
});
} ;