Permalink
Browse files

Merge branch 'master' of github.com:PANmedia/Raptor

  • Loading branch information...
2 parents 538f452 + 9aeec9b commit b83f0e1e13b1d30e3a2788149b94e341f5b8b2c4 @faceleg faceleg committed Oct 31, 2012
View
7 CHANGES.md
@@ -0,0 +1,7 @@
+v0.0.27
+===
+
+API Changes:
+---
+
+ - Changed save REST plugin to no longer pass though and ID parameter to callbacks.
View
1 example/save-rest/.gitignore
@@ -0,0 +1 @@
+/content.json
View
12 example/save-rest/css/style.css
@@ -0,0 +1,12 @@
+* {
+ font-family: Arial, Helvetica, sans-serif;
+}
+
+body {
+ width: 970px;
+ margin: 0 auto;
+}
+
+h1 {
+ border-bottom: 1px dashed black;
+}
View
99 example/save-rest/example.php
@@ -0,0 +1,99 @@
+<?php
+ $file = __DIR__ . '/content.json';
+ $content = [];
+ if (file_exists(__DIR__ . '/content.json')) {
+ $content = file_get_contents($file);
+ $content = json_decode($content, true);
+ if ($content === false) {
+ $content = [];
+ }
+ }
+?>
+<!doctype html>
+<html>
+<head>
+ <meta charset="utf-8" />
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
+ <title>Raptor Editor - Save Rest Example</title>
+ <link rel="stylesheet" href="css/style.css" />
+ <?php $uri = '../../src/'; include '../../src/include.php'; ?>
+ <script type="text/javascript">
+ jQuery(function($) {
+ $('.editable').editor({
+ urlPrefix: '../../src/',
+ ui: {
+ save: {
+ plugin: 'saveRest'
+ }
+ },
+ plugins: {
+ saveRest: {
+ /**
+ * True if you want all editing elements to be saved at once.
+ */
+ multiple: true,
+ ajax: {
+ type: 'post',
+ dataType: 'json',
+ url: function(id) {
+ return 'save.php';
+ },
+ data: function(html) {
+ return {
+ _method: 'put',
+ id: this.editor.getElement().data('id'),
+ content: html
+ };
+ }
+ }
+ }
+ }
+ });
+ });
+ </script>
+</head>
+<body>
+ <div class="editable" data-id="header">
+ <?php ob_start(); ?>
+ <h1>Raptor Editor - Save Rest Example</h1>
+ <?php
+ $buffer = ob_get_clean();
+ if (isset($content['header'])) {
+ echo $content['header'];
+ } else {
+ echo $buffer;
+ }
+ ?>
+ </div>
+ <div class="editable" data-id="body">
+ <?php ob_start(); ?>
+ <p>
+ Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum
+ has been the industry's standard dummy text ever since the 1500s, when an unknown printer
+ took a galley of type and scrambled it to make a type specimen book. It has survived not
+ only five centuries, but also the leap into electronic typesetting, remaining essentially
+ unchanged. It was popularised in the 1960s with the release of Letraset sheets containing
+ Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker
+ including versions of Lorem Ipsum.
+ </p>
+ <p>
+ Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum
+ has been the industry's standard dummy text ever since the 1500s, when an unknown printer
+ took a galley of type and scrambled it to make a type specimen book. It has survived not
+ only five centuries, but also the leap into electronic typesetting, remaining essentially
+ unchanged. It was popularised in the 1960s with the release of Letraset sheets containing
+ Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker
+ including versions of Lorem Ipsum.
+ </p>
+ <?php
+ $buffer = ob_get_clean();
+ if (isset($content['body'])) {
+ echo $content['body'];
+ } else {
+ echo $buffer;
+ }
+ ?>
+ </div>
+
+</body>
+</html>
View
21 example/save-rest/save.php
@@ -0,0 +1,21 @@
+<?php
+if (isset($_POST['id']) && isset($_POST['content'])) {
+ $file = __DIR__ . '/content.json';
+ $content = [];
+ if (file_exists(__DIR__ . '/content.json')) {
+ $content = file_get_contents($file);
+ $content = json_decode($content, true);
+ if ($content === false) {
+ $content = [];
+ }
+ }
+ $content[$_POST['id']] = $_POST['content'];
+ $content = json_encode($content, JSON_PRETTY_PRINT);
+ if ($content !== false) {
+ if (file_put_contents($file, $content)) {
+ echo json_encode(true);
+ return;
+ }
+ }
+}
+echo json_encode(false);
View
36 src/plugins/save/jquery.ui.editor.saverest.js
@@ -85,13 +85,12 @@ $.ui.editor.registerPlugin('saveRest', /** @lends $.editor.plugin.saveRest.proto
this.requests = 0;
// Get all unified content
- var dirty = 0;
+ var plugin = this,
+ dirty = 0;
this.editor.unify(function(editor) {
if (editor.isDirty()) {
dirty++;
- var plugin = editor.getPlugin('saveRest');
- var content = plugin.editor.save();
- plugin.ajax(content);
+ editor.getPlugin('saveRest').ajax(editor.save(), plugin);
}
});
this.dirty = dirty;
@@ -169,38 +168,35 @@ $.ui.editor.registerPlugin('saveRest', /** @lends $.editor.plugin.saveRest.proto
},
/**
- * Handle the save AJAX request(s)
- * @param {String} contentData The element's content
- * @param {String} id Editing element's identfier
+ * Handle the save AJAX request(s).
+ *
+ * @param {String} contentData The element's content.
+ * @param {String} id Editing element's ID.
+ * @param {Object} counter Counter object used to confirm all content block have been saved.
*/
- ajax: function(contentData, id) {
- // Create POST data
- //var data = {};
-
- // Content is serialized to a JSON object, and sent as 1 post parameter
- //data[this.options.postName] = JSON.stringify(contentData);
-
+ ajax: function(contentData, counter) {
// Create the JSON request
var ajax = $.extend(true, {}, this.options.ajax);
+ // Get the data to send to the server
if ($.isFunction(ajax.data)) {
- ajax.data = ajax.data.apply(this, [id, contentData]);
+ ajax.data = ajax.data.apply(this, [contentData]);
} else if (this.options.postName) {
ajax.data = {};
ajax.data[this.options.postName] = JSON.stringify(contentData);
}
// Get the URL, if it is a callback
if ($.isFunction(ajax.url)) {
- ajax.url = ajax.url.apply(this, [id]);
+ ajax.url = ajax.url.apply(this);
}
// Send the data to the server
- this.requests++;
+ counter.requests++;
$.ajax(ajax)
- .done($.proxy(this.done, this))
- .fail($.proxy(this.fail, this))
- .always($.proxy(this.always, this));
+ .done($.proxy(counter.done, counter))
+ .fail($.proxy(counter.fail, counter))
+ .always($.proxy(counter.always, counter));
}
});

0 comments on commit b83f0e1

Please sign in to comment.