aboutsummaryrefslogtreecommitdiff
path: root/mod/blog/views/default/js
diff options
context:
space:
mode:
Diffstat (limited to 'mod/blog/views/default/js')
-rw-r--r--mod/blog/views/default/js/blog/save_draft.php67
1 files changed, 67 insertions, 0 deletions
diff --git a/mod/blog/views/default/js/blog/save_draft.php b/mod/blog/views/default/js/blog/save_draft.php
new file mode 100644
index 000000000..8cd07ff5d
--- /dev/null
+++ b/mod/blog/views/default/js/blog/save_draft.php
@@ -0,0 +1,67 @@
+<?php
+/**
+ * Save draft through ajax
+ *
+ * @package Blog
+ */
+?>
+elgg.provide('elgg.blog');
+
+/*
+ * Attempt to save and update the input with the guid.
+ */
+elgg.blog.saveDraftCallback = function(data, textStatus, XHR) {
+ if (textStatus == 'success' && data.success == true) {
+ var form = $('form[id=blog-post-edit]');
+
+ // update the guid input element for new posts that now have a guid
+ form.find('input[name=guid]').val(data.guid);
+
+ oldDescription = form.find('textarea[name=description]').val();
+
+ var d = new Date();
+ var mins = d.getMinutes() + '';
+ if (mins.length == 1) {
+ mins = '0' + mins;
+ }
+ $(".blog-save-status-time").html(d.toLocaleDateString() + " @ " + d.getHours() + ":" + mins);
+ } else {
+ $(".blog-save-status-time").html(elgg.echo('error'));
+ }
+};
+
+elgg.blog.saveDraft = function() {
+ if (typeof(tinyMCE) != 'undefined') {
+ tinyMCE.triggerSave();
+ }
+
+ // only save on changed content
+ var form = $('form[id=blog-post-edit]');
+ var description = form.find('textarea[name=description]').val();
+ var title = form.find('input[name=title]').val();
+
+ if (!(description && title) || (description == oldDescription)) {
+ return false;
+ }
+
+ var draftURL = elgg.config.wwwroot + "action/blog/auto_save_revision";
+ var postData = form.serializeArray();
+
+ // force draft status
+ $(postData).each(function(i, e) {
+ if (e.name == 'status') {
+ e.value = 'draft';
+ }
+ });
+
+ $.post(draftURL, postData, elgg.blog.saveDraftCallback, 'json');
+};
+
+elgg.blog.init = function() {
+ // get a copy of the body to compare for auto save
+ oldDescription = $('form[id=blog-post-edit]').find('textarea[name=description]').val();
+
+ setInterval(elgg.blog.saveDraft, 60000);
+};
+
+elgg.register_hook_handler('init', 'system', elgg.blog.init); \ No newline at end of file