First pass at adding Ajax editing to menmo's note block.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 23 Jul 2010 23:13:54 +0000 (19:13 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 23 Jul 2010 23:15:55 +0000 (19:15 -0400)
Can probably still tweak some things, but basic functionality is there.
Just click any where on the note to activate the editor.

horde/js/inplaceeditor.js
mnemo/lib/Ajax/Imple/EditNote.php [new file with mode: 0644]
mnemo/lib/Block/note.php

index ca9fdc9..8ae9096 100644 (file)
@@ -63,6 +63,7 @@ InPlaceEditor = Class.create(
             stripLoadedTextTags: false,
             submitOnBlur: false,
             width: null,
+            autoWidth: false,
 
             /** Default Callbacks **/
             callback: function(form)
@@ -191,8 +192,11 @@ InPlaceEditor = Class.create(
         fld.value = text; // No HTML breaks conversion anymore
         fld.className = 'editor_field';
         if (this.options.width) {
-            fld.setStyle({ width: this.options.width + 'px' });
+            var w = this.options.width + 'px';
+        } else if (this.options.autoWidth) {
+            var w = this.element.up().getWidth() + 'px';
         }
+        fld.setStyle({ width: w });
         if (this.options.submitOnBlur) {
             fld.observe('blur', this._boundSubmitHandler);
         }
diff --git a/mnemo/lib/Ajax/Imple/EditNote.php b/mnemo/lib/Ajax/Imple/EditNote.php
new file mode 100644 (file)
index 0000000..bf15431
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/**
+ * Mnemo_Ajax_Imple_EditNote:: class for performing Ajax note editing.
+ *
+ * Copyright 2008-2010 The Horde Project (http://www.horde.org/)
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @package Mnemo
+ */
+class Mnemo_Ajax_Imple_EditNote extends Horde_Core_Ajax_Imple
+{
+    public function __construct($params)
+    {
+        /* Set up some defaults */
+        if (empty($params['rows'])) {
+            $params['rows'] = 2;
+        }
+        if (empty($params['cols'])) {
+            $params['cols'] = 20;
+        }
+        parent::__construct($params);
+    }
+
+    public function attach()
+    {
+        Horde::addScriptFile('effects.js', 'horde');
+        Horde::addScriptFile('inplaceeditor.js', 'horde');
+
+        $params = array('input' => 'value',
+                        'id' => $this->_params['id']);
+
+        $url = $this->_getUrl('EditNote', 'mnemo', $params);
+        $loadTextUrl = $this->_getUrl('EditNote', 'mnemo', array_merge($params, array('action' => 'load')));
+        $js = array();
+
+        $js[] = "new InPlaceEditor('" . $this->_params['domid'] . "', '" . $url . "', {"
+                . "   callback: function(form, value) {"
+                . "       return 'value=' + encodeURIComponent(value);},"
+                . "   loadTextURL: '". $loadTextUrl . "',"
+                . "   rows: 4, "
+                . "   autoWidth: true,"
+                . "   emptyText: '" . _("Click to add text...") . "',"
+                . "   onComplete: function(ipe, opts) { ipe.checkEmpty() },"
+                . "   cancelText: '" . _("Cancel") . "',"
+                . "   okText: '" . _("Ok") . "',"
+                . "   cancelClassName: ''"
+                . "  });";
+
+        Horde::addInlineScript($js, 'dom');
+    }
+
+    public function handle($args, $post)
+    {
+        if ($GLOBALS['registry']->getAuth()) {
+            /* Are we requesting the unformatted text? */
+            if (!empty($args['action']) && $args['action'] == 'load') {
+                $id = $args['id'];
+                $storage = Mnemo_Driver::singleton();
+                $memo = $storage->getByUID($id);
+                return $memo['body'];
+            }
+            if (empty($args['input']) ||
+                is_null($pref_value = Horde_Util::getPost($args['input'], null)) ||
+                empty($args['id'])) {
+
+                    return '';
+            }
+
+            $storage = Mnemo_Driver::singleton();
+            $memo = $storage->getByUID($args['id']);
+            $share = $GLOBALS['mnemo_shares']->getShare($memo['memolist_id']);
+            if (!$share->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
+                throw new Horde_Exception_PermissionDenied(_("You do not have permission to edit this note."));
+            }
+
+            $storage->modify($memo['memo_id'], $memo['desc'], $pref_value);
+            return $GLOBALS['injector']->getInstance('Horde_Text_Filter')->filter(
+                $pref_value,
+                'text2html',
+                array('parselevel' => Horde_Text_Filter_Text2html::MICRO));
+        }
+    }
+
+}
index 1a7d693..7ce08ae 100644 (file)
@@ -39,13 +39,16 @@ class Horde_Block_Mnemo_note extends Horde_Block
     protected function _content()
     {
         $memo = $this->_getNote();
-        $html = '<div class="noteBody">';
+        $html = '<div id="noteBody' . $memo['memo_id'] . '" class="noteBody">';
         $body = $GLOBALS['injector']->getInstance('Horde_Text_Filter')->filter($memo['body'], 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO));
         try {
             $body = Horde::callHook('format_description', array($body), 'mnemo', $body);
         } catch (Horde_Exception_HookNotSet $e) {}
         $html .= $body . '</div>';
-
+        $GLOBALS['injector']->getInstance('Horde_Ajax_Imple')->getImple(array('mnemo', 'EditNote'), array(
+            'domid' => "noteBody" . $memo['memo_id'],
+            'id' => $this->_params['note_uid']
+        ));
         return $html;
     }