public
Description: Monkeypatches script.aculo.us's InPlaceEditor to give it a preview mode
Homepage: http://dougmcinnes.com/2008/11/13/javascript-love/
Clone URL: git://github.com/latimes/inplace_preview.git
inplace_preview / inplace_preview.js
100644 65 lines (62 sloc) 2.178 kb
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
/**
* inplace_preview.js
* Version 1.0.1
* Doug McInnes <doug.mcinnes@latimes.com>
*
* See README.txt for more info
*/
 
Object.extend(Ajax.InPlaceEditor.prototype, {
  oldCreateForm: Ajax.InPlaceEditor.prototype.createForm,
  createForm: function() {
    this.oldCreateForm();
    if (this.options.previewLink) {
      this.previewLink = document.createElement("a");
      this.previewLink.href = "#";
      this.previewLink.appendChild(document.createTextNode(this.options.previewText || "Preview"));
      this.previewLink.onclick = this.onclickPreview.bind(this);
      this.previewLink.className = 'editor_preview editor_preview_link';
      new Insertion.Bottom(this.form, "&nbsp;");
      this.form.appendChild(this.previewLink);
      // create preview element
      this.preview = document.createElement(this.element.tagName);
      this.form.insertBefore(this.preview, this.editField);
      Element.hide(this.preview);
      this.preview.onclick = this.onclickPreview.bind(this);
    }
  },
  onclickPreview: function() {
    if (this.saving) return;
    if (Element.visible(this.preview)) {
      Element.hide(this.preview);
      Element.show(this.editField);
      Element.update(this.previewLink, this.options.previewText || "Preview");
    }
    else {
      Element.update(this.preview, this.options.previewLoadingText || "Preview Loading...");
      if (this.options.loadPreviewURL) {
        // make an Ajax call to render the content
        new Ajax.Updater(
          { success: this.preview,
            failure: null },
            this.options.loadPreviewURL,
            Object.extend({
              parameters: this.options.callback(this.form, this.editField.value)
            }, this.options.ajaxOptions));
      }
      else {
        // or just stuff it in the preview field
        this.preview.update(this.editField.value);
      }
      Element.hide(this.editField);
      Element.show(this.preview);
      Element.update(this.previewLink, this.options.editText || "Edit");
    }
    return false;
  },
  onLeaveEditMode: function() {
    if (this.preview) {
      this.preview.remove();
      this.preview = null;
    }
  }
});