infopark_fiona7 1.2.0.2.3 → 1.5.2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +33 -0
  3. data/app/assets/javascripts/fiona7_ui.js +82 -23
  4. data/app/assets/javascripts/scrivito_patches/ajax.js +222 -0
  5. data/app/assets/javascripts/scrivito_patches/ajax_error_handling.js +24 -0
  6. data/app/assets/javascripts/scrivito_patches/attribute_serializer.js +259 -0
  7. data/app/assets/javascripts/scrivito_patches/base_obj_path.js +17 -0
  8. data/app/assets/javascripts/scrivito_patches/binary_utils.js +33 -0
  9. data/app/assets/javascripts/scrivito_patches/cms_rest_api.js +490 -0
  10. data/app/assets/javascripts/scrivito_patches/models/api/basic_obj.js +650 -0
  11. data/app/assets/javascripts/scrivito_patches/models/binary_field_element.js +53 -0
  12. data/app/assets/javascripts/scrivito_patches/models/obj.js +74 -239
  13. data/app/assets/javascripts/scrivito_patches/obj_serializer.js +91 -0
  14. data/app/assets/stylesheets/fiona7.css.scss +12 -0
  15. data/app/assets/stylesheets/fiona7_ui.css.scss +18 -0
  16. data/app/controllers/fiona7/api_controller.rb +20 -0
  17. data/app/controllers/fiona7/sessions_controller.rb +2 -10
  18. data/app/controllers/fiona7_login_page_controller.rb +0 -3
  19. data/app/controllers/scrivito/obj_class_controller.rb +58 -0
  20. data/app/controllers/scrivito/objs_controller.rb +2 -38
  21. data/app/helpers/fiona7_override_helper.rb +25 -0
  22. data/app/views/fiona7/release/preview.html.erb +1 -1
  23. data/app/views/fiona7_login_page/index.html.erb +0 -6
  24. data/app/views/scrivito/ui/index.html.erb +2 -1
  25. data/app/views/scrivito/webservice/_workspace.json.jbuilder +8 -0
  26. data/config/locales/errors.yml +18 -0
  27. data/config/locales/workflow.yml +24 -0
  28. data/config/precedence_routes.rb +7 -14
  29. data/infopark_fiona7.gemspec +6 -4
  30. data/lib/fiona7/assert.rb +2 -2
  31. data/lib/fiona7/attribute_readers/factory.rb +4 -0
  32. data/lib/fiona7/attribute_type_mapper.rb +4 -1
  33. data/lib/fiona7/attribute_writers/binary_as_binary.rb +2 -2
  34. data/lib/fiona7/attribute_writers/binary_as_linklist.rb +2 -2
  35. data/lib/fiona7/attribute_writers/factory.rb +4 -0
  36. data/lib/fiona7/builder/indirect_blob_builder.rb +2 -2
  37. data/lib/fiona7/builder/lazy_blob_copier.rb +1 -1
  38. data/lib/fiona7/builder/obj_builder.rb +45 -12
  39. data/lib/fiona7/controllers/rest_api/error_handler.rb +49 -0
  40. data/lib/fiona7/controllers/rest_api/obj_controller.rb +34 -20
  41. data/lib/fiona7/controllers/rest_api/session_controller.rb +13 -0
  42. data/lib/fiona7/engine.rb +7 -10
  43. data/lib/fiona7/facet_builder.rb +5 -1
  44. data/lib/fiona7/naive_search_engine.rb +9 -0
  45. data/lib/fiona7/routers/rest_api.rb +17 -0
  46. data/lib/fiona7/scrivito_patches/attribute_content.rb +28 -0
  47. data/lib/fiona7/scrivito_patches/cms_routing.rb +14 -31
  48. data/lib/fiona7/scrivito_patches/page_config.rb +1 -0
  49. data/lib/fiona7/scrivito_user.rb +3 -2
  50. data/lib/fiona7/type_register.rb +4 -0
  51. data/lib/fiona7/verity_search_engine.rb +6 -17
  52. data/lib/fiona7/version.rb +1 -1
  53. data/lib/fiona7/workspace.rb +2 -0
  54. metadata +28 -14
  55. data/app/assets/javascripts/scrivito_patches/models/ajax.js +0 -99
  56. data/app/assets/javascripts/scrivito_patches/models/blob.js +0 -50
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c5f40ca907638fd8408111bd3f4bd2b10fb9fd9
4
- data.tar.gz: 98c910149b086995516b632d25ca398cf9f89ba9
3
+ metadata.gz: 741127529a15e35abbb025c194986fca04edb0da
4
+ data.tar.gz: 87098673b467a0ed0a93cc5e84f298365397f5b5
5
5
  SHA512:
6
- metadata.gz: 0fdc4db91f3cb47803a089dbfee7058083caabb32e4d7600c7d0359ab10480bdf8e70d20249f823f4b8255cdd6488c74e50ba4f319de29c4a946f86a61513b8e
7
- data.tar.gz: 26a3205d4f7d209cf80c802404cd9cc5a88436ad000a41e37cfa343cbee5e85a34cbcfef3b4c47618634a7974450141806bd897c9f18fc3fdc6c3917692302d6
6
+ metadata.gz: aa8b4752d37991650a8d51d0bf84e968d2fbe93b9472c4045591c2a5a7ca6a34c8d5e98b1e9a8813c6231de42bc436b7946e1157ec2d4311343203649275f25e
7
+ data.tar.gz: 5adea82b2c79308ff97ccf118568545b314226081a2acf9ca3bfb4ae8624383e9b5938ad9b717935ed1bfe408b4e368f98d89afed36b71f3de36a41aaa884751
data/Rakefile CHANGED
@@ -15,7 +15,40 @@ RDoc::Task.new(:rdoc) do |rdoc|
15
15
  end
16
16
 
17
17
 
18
+ namespace "package" do
19
+ desc "Package javascript"
20
+ task "js" do
21
+ puts "Transpiling javascript files"
22
+ require 'babel/transpiler'
23
+ Dir["app/**/*"].each do |fname|
24
+ if /\.es6\.js$/ =~ fname
25
+ nfname = ::File.join(::File.dirname(fname), ::File.basename(fname, '.es6.js') + '.js')
26
+ puts "#{fname} -> #{nfname}"
27
+ ::File.open(nfname, 'w') do |f|
28
+ f.write Babel::Transpiler.transform(File.read(fname))["code"]
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ namespace "clean" do
36
+ desc "Clean packaged javascript"
37
+ task "js" do
38
+ puts "Cleaning transpiled javascript files"
39
+ Dir["app/**/*"].each do |fname|
40
+ if /\.es6\.js$/ =~ fname
41
+ nfname = ::File.join(::File.dirname(fname), ::File.basename(fname, '.es6.js') + '.js')
42
+ puts "#{nfname}"
43
+ ::FileUtils.rm(nfname) if ::File.exists?(nfname)
44
+ end
45
+ end
46
+ end
47
+ end
18
48
 
49
+ task "build" => ["clean:js", "package:js"] do
50
+ # dummy, needed for dependencies, the rest is added later by bundler
51
+ end
19
52
 
20
53
  Bundler::GemHelper.install_tasks
21
54
 
@@ -1,6 +1,13 @@
1
- //= require scrivito_patches/models/ajax
2
- //= require scrivito_patches/models/blob
1
+ //= require scrivito_patches/models/api/basic_obj
2
+ //= require scrivito_patches/models/binary_field_element
3
3
  //= require scrivito_patches/models/obj
4
+ //= require scrivito_patches/ajax
5
+ //= require scrivito_patches/attribute_serializer
6
+ //= require scrivito_patches/ajax_error_handling
7
+ //= require scrivito_patches/base_obj_path
8
+ //= require scrivito_patches/cms_rest_api
9
+ //= require scrivito_patches/obj_serializer
10
+ //= require scrivito_patches/binary_utils
4
11
  //= require_self
5
12
 
6
13
  (function() {
@@ -87,7 +94,7 @@
87
94
  // view loaded
88
95
  });
89
96
 
90
- scrivito.with_dialog_behaviour(view, deferred, {enter: accept, escape: cancel});
97
+ scrivito.withDialogBehaviour(view, deferred, {enter: accept, escape: cancel});
91
98
  },
92
99
  function(xhr, text_status, error) {
93
100
  });
@@ -226,14 +233,84 @@
226
233
  };
227
234
  var actions = ["edit", "forward", "reject","commit", "sign", "take"];
228
235
 
236
+ var releaseAction = function (objId) {
237
+ open_release_dialog(objId).done(function(objIds, comment) {
238
+ scrivito.withSavingOverlay(scrivito.ajax('PUT', 'objs/release', {data: {objs: objIds, comment: comment}}).then(function() {
239
+ scrivito.reload();
240
+ }));
241
+ });
242
+ };
243
+
244
+ var workflowAction = function (action, objId) {
245
+ open_confirmation_dialog(action, dialog_icons[action]).done(function(comment) {
246
+ scrivito.withSavingOverlay(scrivito.ajax('PUT', 'objs/workflow/'+action, {data: {id: objId, comment: comment}}).then(function() {
247
+ if (action == 'edit' || action == 'take') {
248
+ if (!scrivito.editing_context.is_editing_mode()) {
249
+ return scrivito.withSavingOverlay(
250
+ scrivito.change_editing_context({display_mode: 'editing', workspace_id: 'rtc'}));
251
+ } else {
252
+ return scrivito.withSavingOverlay(scrivito.reload());
253
+ }
254
+ } else {
255
+ return scrivito.withSavingOverlay(scrivito.reload());
256
+ }
257
+ }));
258
+ });
259
+ };
260
+
229
261
  scrivito.gui.on('document', function(cms_document) {
230
262
  var page_config = (cms_document.page_config() || {})['current_page'] || {};
231
263
  var obj = cms_document.page();
232
264
  var buttons = page_config['buttons'] || [];
233
265
  var workflow_enabled = page_config['workflow'];
234
266
 
267
+
235
268
  if (cms_document.browser_window().name !== 'scrivito_application') {
236
269
  // TODO: add special handling for scrivito_details_dialog and scrivito_inspector
270
+ var button;
271
+
272
+ // enable workflow buttons
273
+ $.each(buttons, function (idx, action) {
274
+ // release is handled differently
275
+ if (action === 'release') {
276
+ return;
277
+ }
278
+
279
+ // find the workflow button
280
+ button = $('[data-fiona7-private-workflow-button="' + action + '"]', cms_document.browser_window().document);
281
+
282
+ // enable given workflow button
283
+ button.removeAttr('disabled');
284
+ button.removeClass('disabled');
285
+ button.click(function () {
286
+ workflowAction(action, obj.id());
287
+ });
288
+
289
+ });
290
+
291
+ // remove redundant buttons
292
+ $.each(actions, function (idx, action) {
293
+ if (buttons.indexOf(action) === -1) {
294
+ $('[data-fiona7-private-workflow-button="' + action + '"]', cms_document.browser_window().document).remove();
295
+ }
296
+ });
297
+
298
+ // conditionally enable release button
299
+ // find the release button
300
+ button = $('[data-fiona7-private-workflow-button="release"]', cms_document.browser_window().document);
301
+ if (obj.modification()) {
302
+ if (obj.has_restriction()) {
303
+ // release impossibru
304
+ button.attr('title', obj.restriction_messages().join("; "));
305
+ } else {
306
+ button.removeAttr('disabled');
307
+ button.removeClass('disabled');
308
+ button.click(function () {
309
+ releaseAction(obj.id());
310
+ });
311
+ }
312
+ }
313
+
237
314
  return;
238
315
  }
239
316
 
@@ -260,11 +337,7 @@
260
337
  },
261
338
 
262
339
  execute: function() {
263
- open_release_dialog(obj.id()).done(function(objIds, comment) {
264
- scrivito.with_saving_overlay(scrivito.ajax('PUT', 'objs/release', {data: {objs: objIds, comment: comment}}).then(function() {
265
- scrivito.reload();
266
- }));
267
- });
340
+ return releaseAction(obj.id());
268
341
  }
269
342
  });
270
343
  /* TODO: enforce permissions */
@@ -291,21 +364,7 @@
291
364
  },
292
365
 
293
366
  execute: function() {
294
- open_confirmation_dialog(action, dialog_icons[action]).done(function(comment) {
295
- scrivito.with_saving_overlay(scrivito.ajax('PUT', 'objs/workflow/'+action, {data: {id: obj.id(), comment: comment}}).then(function() {
296
- if (action == 'edit' || action == 'take') {
297
-
298
- if (!scrivito.editing_context.is_editing_mode()) {
299
- return scrivito.with_saving_overlay(
300
- scrivito.change_editing_context({display_mode: 'editing', workspace_id: 'rtc'}));
301
- } else {
302
- return scrivito.with_saving_overlay(scrivito.reload());
303
- }
304
- } else {
305
- return scrivito.with_saving_overlay(scrivito.reload());
306
- }
307
- }));
308
- });
367
+ return workflowAction(action, obj.id());
309
368
  }
310
369
  });
311
370
  });
@@ -0,0 +1,222 @@
1
+ (function() {
2
+ var handle_task = function(task) {
3
+ switch (task.status) {
4
+ case 'success':
5
+ return $.Deferred().resolve(task.result);
6
+ case 'error':
7
+ return $.Deferred().reject({
8
+ message: task.message,
9
+ code: task.code
10
+ });
11
+ case 'open':
12
+ return scrivito.wait(2).then(function() {
13
+ return single_ajax('GET', 'tasks/' + task.id).then(function(data) {
14
+ return handle_task(data);
15
+ });
16
+ });
17
+ default:
18
+ throw { message: "Invalid task (unknown status)", task: task };
19
+ }
20
+ };
21
+
22
+ // -- PATCH BEGINS HERE --
23
+ var ajax_with_form_data_required = function(params) {
24
+ var objParams = params && params.obj;
25
+ var result = false;
26
+ var k;
27
+
28
+ if (!objParams) {
29
+ return result;
30
+ }
31
+
32
+ for (k in objParams) {
33
+ if (objParams.hasOwnProperty(k)) {
34
+ result = scrivito.BinaryUtils.isFile(objParams[k]) || scrivito.BinaryUtils.isBlob(objParams[k]) || scrivito.BinaryUtils.isBlobWithName(objParams[k]);
35
+ if (result) {
36
+ return result;
37
+ }
38
+ }
39
+ }
40
+ return result;
41
+ };
42
+
43
+ var params_to_form_data = function(params) {
44
+ var objParams = params && params.obj || {};
45
+ var fd = new FormData();
46
+
47
+ var k, attr, value, widgetId, widgetValue, widgetAttr, i, l, t;
48
+
49
+ for (k in objParams) {
50
+ if (objParams.hasOwnProperty(k)) {
51
+
52
+ attr = k;
53
+ value = objParams[k];
54
+
55
+ if (attr === '_widget_pool') {
56
+ for (w in value) {
57
+ if (value.hasOwnProperty(w)) {
58
+ for (widgetAttr in value[w]) {
59
+ if (value[w].hasOwnProperty(widgetAttr)) {
60
+ widgetValue = value[w][widgetAttr];
61
+
62
+ if (widgetValue.pop) {
63
+ // value is an array
64
+ for (i=0; i < widgetValue.length; ++i) {
65
+ fd.append("obj[" + attr + "][" + w + "][" + widgetAttr + "][]", widgetValue[i]);
66
+ }
67
+ } else if (scrivito.BinaryUtils.isFile(widgetValue) || scrivito.BinaryUtils.isBlob(widgetValue)) {
68
+ // upload
69
+ fd.append("obj[" + attr + "][" + w + "][" + widgetAttr + "]", widgetValue);
70
+ } else if (scrivito.BinaryUtils.isBlobWithName(widgetValue)) {
71
+ fd.append("obj[" + attr + "][" + w + "][" + widgetAttr + "]", widgetValue.blob_to_upload, widgetValue.filename);
72
+ } else if (typeof value === 'object') {
73
+ // blob/copy rename
74
+ for (l in widgetValue) {
75
+ if (widgetValue.hasOwnProperty(l)) {
76
+ fd.append("obj[" + attr + "][" + w + "][" + widgetAttr + "][" + l + "]", widgetValue[l]);
77
+ }
78
+ }
79
+ } else if (typeof value !== 'undefined') {
80
+ fd.append("obj[" + attr + "][" + w + "][" + widgetAttr + "]", widgetValue);
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
86
+ } else if (value.pop) {
87
+ // value is an array
88
+ for (i=0; i < value.length; ++i) {
89
+ fd.append("obj[" + attr + "][]", value[i]);
90
+ }
91
+ } else if (scrivito.BinaryUtils.isFile(value) || scrivito.BinaryUtils.isBlob(value)) {
92
+ // upload
93
+ fd.append("obj[" + attr + "]", value);
94
+ } else if (scrivito.BinaryUtils.isBlobWithName(value)) {
95
+ fd.append("obj[" + attr + "]", value.blob_to_upload, value.filename);
96
+ } else if (typeof value === 'object') {
97
+ // blob/copy rename
98
+ for (l in value) {
99
+ if (value.hasOwnProperty(l)) {
100
+ fd.append("obj[" + attr + "][" + l + "]", value[l]);
101
+ }
102
+ }
103
+ } else if (typeof value !== 'undefined') {
104
+ fd.append("obj[" + attr + "]", value);
105
+ }
106
+ }
107
+ }
108
+
109
+ return fd;
110
+ };
111
+
112
+ var single_ajax_with_form_data = function(type, path, options) {
113
+ var base_url = window.location.protocol + '//' + window.location.host + '/__scrivito/';
114
+ if (options && options.data) {
115
+ options.data = params_to_form_data(options.data);
116
+ }
117
+ return $.ajax(base_url + path, _.extend({
118
+ type: type,
119
+ contentType: false,
120
+ processData: false,
121
+ cache: false // Don't cache GET requests.
122
+ }, options || {})).then(
123
+ function(result, text_status, xhr) {
124
+ return $.Deferred().resolve(result);
125
+ }, function(xhr, text_status, xhr_error) {
126
+ try {
127
+ return $.Deferred().reject(JSON.parse(xhr.responseText));
128
+ } catch (SyntaxError) {
129
+ return $.Deferred().reject(xhr_error);
130
+ }
131
+ }
132
+ );
133
+ };
134
+ // -- PATCH ENDS HERE --
135
+
136
+ var single_ajax = function(type, path, options) {
137
+ // -- PATCH BEGINS HERE --
138
+ if (ajax_with_form_data_required(options && options.data || {})) {
139
+ return single_ajax_with_form_data(type, path, options);
140
+ }
141
+ // -- PATCH ENDS HERE --
142
+ var base_url = window.location.protocol + '//' + window.location.host + '/__scrivito/';
143
+ if (options && options.data) {
144
+ options.data = JSON.stringify(options.data);
145
+ }
146
+ return $.ajax(base_url + path, _.extend({
147
+ type: type,
148
+ dataType: 'json',
149
+ contentType: 'application/json; charset=utf-8',
150
+ cache: false // Don't cache GET requests.
151
+ }, options || {})).then(
152
+ function(result, text_status, xhr) {
153
+ return $.Deferred().resolve(result);
154
+ }, function(xhr, text_status, xhr_error) {
155
+ try {
156
+ return $.Deferred().reject(JSON.parse(xhr.responseText));
157
+ } catch (SyntaxError) {
158
+ return $.Deferred().reject(xhr_error);
159
+ }
160
+ }
161
+ );
162
+ };
163
+
164
+ _.extend(scrivito, {
165
+ silent_ajax: function(type, path, options) {
166
+ var is_write_request = type === 'PUT' || type === 'POST' || type === 'DELETE';
167
+ var skip_write_monitor = options && options.skip_write_monitor;
168
+
169
+ if (is_write_request) {
170
+ options = options || {};
171
+ options.timeout = 120000; // miliseconds // <-- PATCH HERE
172
+
173
+ if (!skip_write_monitor) {
174
+ scrivito.write_monitor.start_write();
175
+ }
176
+ }
177
+
178
+ var ajax_promise = single_ajax(type, path, options).then(function(result) {
179
+ if (result && result.task && _.size(result) === 1) {
180
+ return handle_task(result.task);
181
+ } else {
182
+ return $.Deferred().resolve(result);
183
+ }
184
+ });
185
+
186
+ if (is_write_request && !skip_write_monitor) {
187
+ ajax_promise.always(function() {
188
+ scrivito.write_monitor.end_write();
189
+ });
190
+ }
191
+
192
+ return ajax_promise;
193
+ },
194
+
195
+ ajax: function(type, path, options) {
196
+ return scrivito.silent_ajax(type, path, options).fail(function(error) {
197
+ scrivito.display_ajax_error(error);
198
+ });
199
+ },
200
+
201
+ display_ajax_error: function(error) {
202
+ var message, message_for_editor;
203
+
204
+ // -- PATCH BEGINS HERE --
205
+ if (_.isObject(error)) {
206
+ if (error.message) {
207
+ return scrivito.alertDialog(error.message);
208
+ } else {
209
+ return scrivito.alertDialog(scrivito.t('ajax_error.communication'));
210
+ }
211
+ } else if (_.contains(['abort', 'parsererror', 'timeout'], error)) {
212
+ message = scrivito.t('ajax_error.communication');
213
+ } else {
214
+ message = scrivito.t('ajax_error', error);
215
+ }
216
+ scrivito.logError(message);
217
+ scrivito.errorDialog(message_for_editor || scrivito.t('ajax_error.message_for_editor'),
218
+ [error.timestamp, message]);
219
+ // -- PATCH ENDS HERE --
220
+ }
221
+ });
222
+ }());
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ (function () {
4
+ _.extend(scrivito, {
5
+ handleAjaxError: function handleAjaxError(error) {
6
+ var message = undefined;
7
+
8
+ // -- PATCH BEGINS HERE --
9
+ if (typeof error === 'object' && error.message) {
10
+ scrivito.alertDialog(error.message);
11
+ } else {
12
+ message = scrivito.t('ajax_error.communication');
13
+ scrivito.errorDialog(scrivito.t('ajax_error.message_for_editor'), [message]);
14
+ }
15
+ // -- PATCH ENDS HERE --
16
+
17
+ scrivito.logError(error);
18
+ },
19
+
20
+ withAjaxErrorHandling: function withAjaxErrorHandling(promise) {
21
+ return promise['catch'](scrivito.handleAjaxError);
22
+ }
23
+ });
24
+ })();
@@ -0,0 +1,259 @@
1
+ 'use strict';
2
+
3
+ (function () {
4
+ scrivito.AttributeSerializer = {
5
+ serialize: function serialize(objClass, attributes) {
6
+ return _.mapObject(attributes, function (value, name) {
7
+ if (scrivito.Attribute.isSystemAttribute(name)) {
8
+ return value;
9
+ }
10
+
11
+ var attribute = objClass && objClass.attribute(name);
12
+
13
+ if (!attribute) {
14
+ throw new scrivito.ScrivitoError('Attribute "' + name + '" is not defined for obj class "' + objClass.name + '".');
15
+ }
16
+
17
+ return [serializeAttributeType(attribute.type, name), serializeAttributeValue(attribute, value, name)];
18
+ });
19
+ }
20
+ };
21
+
22
+ function serializeAttributeType(type, name) {
23
+ switch (type) {
24
+ case 'enum':
25
+ return 'string';
26
+ case 'float':
27
+ case 'integer':
28
+ return 'number';
29
+ case 'multienum':
30
+ return 'stringlist';
31
+ case 'binary':
32
+ case 'date':
33
+ case 'html':
34
+ case 'link':
35
+ case 'linklist':
36
+ case 'reference':
37
+ case 'referencelist':
38
+ case 'string':
39
+ case 'stringlist':
40
+ case 'widgetlist':
41
+ return type;
42
+ default:
43
+ throw new scrivito.ScrivitoError('Attribute "' + name + '" is of unsupported type "' + type + '".');
44
+ }
45
+ }
46
+
47
+ function serializeAttributeValue(attribute, value, name) {
48
+ if (value === null) {
49
+ return value;
50
+ }
51
+
52
+ switch (attribute.type) {
53
+ case 'binary':
54
+ return serializeBinaryAttributeValue(value, name);
55
+ case 'date':
56
+ return serializeDateAttributeValue(value, name);
57
+ case 'enum':
58
+ return serializeEnumAttributeValue(attribute, value, name);
59
+ case 'float':
60
+ return serializeFloatAttributeValue(value, name);
61
+ case 'integer':
62
+ return serializeIntegerAttributeValue(value, name);
63
+ case 'link':
64
+ return serializeLinkAttributeValue(value, name);
65
+ case 'linklist':
66
+ return serializeLinklistAttributeValue(value, name);
67
+ case 'multienum':
68
+ return serializeMultienumAttributeValue(attribute, value, name);
69
+ case 'reference':
70
+ return serializeReferenceAttributeValue(value, name);
71
+ case 'referencelist':
72
+ return serializeReferencelistAttributeValue(value, name);
73
+ case 'widgetlist':
74
+ return serializeWidgetlistAttributeValue(value);
75
+ default:
76
+ return value;
77
+ }
78
+ }
79
+
80
+ function invalidAttributeValue(value, name, expected) {
81
+ throw new scrivito.ScrivitoError('Unexpected value ' + scrivito.prettyPrint(value) + ' for' + (' attribute "' + name + '". Expected: ' + expected));
82
+ }
83
+
84
+ function serializeBinaryAttributeValue(_x, _x2) {
85
+ var _again = true;
86
+
87
+ _function: while (_again) {
88
+ var value = _x,
89
+ name = _x2;
90
+ _again = false;
91
+
92
+ // -- PATCH BEGINS HERE --
93
+ if (value instanceof scrivito.Binary) {
94
+ return { id: value.id };
95
+ } else if (value === null) {
96
+ return null;
97
+ } else if (value instanceof scrivito.UploadedBlob) {
98
+ _x = value.copy();
99
+ _x2 = name;
100
+ _again = true;
101
+ continue _function;
102
+ } else if (value instanceof scrivito.FutureBinary) {
103
+ var futureBinary = value;
104
+
105
+ if (futureBinary.idToCopy) {
106
+ var blob = {
107
+ id_to_copy: futureBinary.idToCopy
108
+ };
109
+ if (futureBinary.filename) blob.filename = futureBinary.filename;
110
+ if (futureBinary.contentType) blob.content_type = futureBinary.contentType;
111
+
112
+ return blob;
113
+ } else if (futureBinary.source) {
114
+ return { blob_to_upload: futureBinary.source, filename: futureBinary.filename };
115
+ }
116
+ } else if (scrivito.BinaryUtils.isFile(value) || scrivito.BinaryUtils.isBlob(value)) {
117
+ return value;
118
+ } else if (typeof value === 'object' && value.id_to_copy) {
119
+ return value;
120
+ } else {
121
+ invalidAttributeValue(value, name, 'A Binary.');
122
+ }
123
+ // -- PATCH ENDS HERE --
124
+ }
125
+ }
126
+
127
+ function serializeDateAttributeValue(value, name) {
128
+ if (_.isDate(value)) {
129
+ return scrivito.types.formatDateToString(value);
130
+ }
131
+ if (scrivito.types.isValidDateString(value)) {
132
+ return value;
133
+ }
134
+
135
+ invalidAttributeValue(value, name, 'A Date.');
136
+ }
137
+
138
+ function serializeEnumAttributeValue(attribute, value, name) {
139
+ var validValues = attribute.validValues();
140
+ if (!_.contains(validValues, value)) {
141
+ var e = 'Valid attribute values are contained in its "validValues" array [' + validValues + '].';
142
+ invalidAttributeValue(value, name, e);
143
+ }
144
+ return value;
145
+ }
146
+
147
+ function serializeFloatAttributeValue(value, name) {
148
+ if (!scrivito.types.isValidFloat(value)) {
149
+ var invalidValue = value;
150
+ if (_.isNumber(value)) {
151
+ invalidValue = String(value);
152
+ }
153
+ invalidAttributeValue(invalidValue, name, 'A Number, that is #isFinite().');
154
+ }
155
+ return value;
156
+ }
157
+
158
+ function serializeIntegerAttributeValue(value, name) {
159
+ if (!scrivito.types.isValidInteger(value)) {
160
+ invalidAttributeValue(value, name, 'A Number, that is #isSafeInteger().');
161
+ }
162
+ return value;
163
+ }
164
+
165
+ function serializeLinkAttributeValue(value, name) {
166
+ if (!validLinkObject(value)) {
167
+ invalidAttributeValue(value, name, 'A scrivito.Link instance.');
168
+ }
169
+
170
+ return convertLinkToCmsApi(value);
171
+ }
172
+
173
+ function serializeLinklistAttributeValue(value, name) {
174
+ if (!Array.isArray(value) || !_.all(value, validLinkObject)) {
175
+ invalidAttributeValue(value, name, 'An array of scrivito.Link instances.');
176
+ }
177
+
178
+ return _.map(value, convertLinkToCmsApi);
179
+ }
180
+
181
+ function validLinkObject(value) {
182
+ if (value instanceof scrivito.Link) {
183
+ return true;
184
+ }
185
+
186
+ // check if value is backend compatible
187
+ if (!_.isObject(value)) {
188
+ return false;
189
+ }
190
+ var invalidKeys = _.without(_.keys(value), 'fragment', 'obj_id', 'query', 'target', 'title', 'url');
191
+ return _.isEmpty(invalidKeys);
192
+ }
193
+
194
+ function convertLinkToCmsApi(value) {
195
+ var backendLink = {
196
+ fragment: value.fragment,
197
+ query: value.query,
198
+ target: value.target,
199
+ title: value.title,
200
+ url: value.url
201
+ };
202
+ backendLink.obj_id = value.objId || value.obj_id;
203
+
204
+ return _.mapObject(backendLink, function (v) {
205
+ return v || null;
206
+ });
207
+ }
208
+
209
+ function serializeMultienumAttributeValue(attribute, value, name) {
210
+ var validValues = attribute.validValues();
211
+ var errorMessage = 'An array with values from ' + scrivito.prettyPrint(validValues) + '.';
212
+
213
+ if (!Array.isArray(value) || !_.all(value, _.isString)) {
214
+ invalidAttributeValue(value, name, errorMessage);
215
+ }
216
+
217
+ var forbiddenValues = _.difference(value, validValues);
218
+ if (forbiddenValues.length) {
219
+ var e = errorMessage + ' Forbidden values: ' + scrivito.prettyPrint(forbiddenValues) + '.';
220
+ invalidAttributeValue(value, name, e);
221
+ }
222
+ return value;
223
+ }
224
+
225
+ function serializeReferenceAttributeValue(value, name) {
226
+ if (!isValidReference(value)) {
227
+ invalidAttributeValue(value, name, 'A BasicObj or a String ID.');
228
+ }
229
+ return serializeSingleReferenceValue(value);
230
+ }
231
+
232
+ function serializeReferencelistAttributeValue(value, name) {
233
+ if (!isValidReferencelistValue(value)) {
234
+ invalidAttributeValue(value, name, 'An array with BasicObjs or String IDs.');
235
+ }
236
+ return _.map(value, serializeSingleReferenceValue);
237
+ }
238
+
239
+ function serializeSingleReferenceValue(value) {
240
+ if (value instanceof scrivito.BasicObj) {
241
+ return value.id;
242
+ }
243
+ return value;
244
+ }
245
+
246
+ function isValidReference(value) {
247
+ return _.isString(value) || value instanceof scrivito.BasicObj;
248
+ }
249
+
250
+ function isValidReferencelistValue(value) {
251
+ return _.isArray(value) && _.every(value, function (v) {
252
+ return isValidReference(v);
253
+ });
254
+ }
255
+
256
+ function serializeWidgetlistAttributeValue(value) {
257
+ return _.pluck(value, 'id');
258
+ }
259
+ })();