                           (remove-self-from-documents self-slug
                                                       documents))})))
 
-(defn add-menu-item-callback [feeds documents evt]
-  (ui/display-dialog "Add Menu Item"
-                     (ui/render-template-as-string
-                      tpl/add-menu-item-dialog
-                      {:feeds feeds
-                       :documents documents})
-                     (partial menu-item-dialog-callback
-                              feeds
-                              documents
-                              (dom/getElement "menu-container")))
+(defn add-menu-item-callback
+  ([feeds documents evt]
+     (add-menu-item-callback feeds
+                             documents
+                             (dom/getElement "menu-container")
+                             evt))
+  ([feeds documents el evt]
+     (. evt (preventDefault))
+     
+     (ui/display-dialog "Add Menu Item"
+                        (ui/render-template-as-string
+                         tpl/add-menu-item-dialog
+                         {:feeds feeds
+                          :documents documents})
+                        (partial menu-item-dialog-callback
+                                 feeds
+                                 documents
+                                 el))
       
-  (let [feed-el (dom/getElement "internal-link-feed")]
-    (events/listen feed-el
-                   "change"
-                   (fn []
-                     (let [feed-pair (util/pair-from-string
-                                      (.-value feed-el))]
-                       (document/get-documents-for-feed
-                        (first feed-pair)
-                        (last feed-pair)
-                        menu-dialog-upd-link-mode-xhr-callback)))))
+     (let [feed-el (dom/getElement "internal-link-feed")]
+       (events/listen feed-el
+                      "change"
+                      (fn []
+                        (let [feed-pair (util/pair-from-string
+                                         (.-value feed-el))]
+                          (document/get-documents-for-feed
+                           (first feed-pair)
+                           (last feed-pair)
+                           menu-dialog-upd-link-mode-xhr-callback)))))
       
-  (events/listen (dom/getElement "link-type-external")
-                 "change"
-                 menu-link-type-change-callback)
+     (events/listen (dom/getElement "link-type-external")
+                    "change"
+                    menu-link-type-change-callback)
       
-  (events/listen (dom/getElement "link-type-internal")
-                 "change"
-                 menu-link-type-change-callback))
+     (events/listen (dom/getElement "link-type-internal")
+                    "change"
+                    menu-link-type-change-callback)))
 
 (defn add-related-page-link-callback [self-slug feeds documents evt]
   (ui/display-dialog "Add Related Page"

  (ui/trigger-on-class
   "add-sub-item"
   "click"
   (fn [e]
     (. e (preventDefault))
                     
     (let [new-ul (dom/createElement "ul")]
       (classes/add new-ul "new-node")
       (set! (.-draggable new-ul) false)
       (dom/appendChild (util/get-parent (util/get-parent (.-target e)))
                        new-ul)
       (ui/display-dialog
        "Add Menu Item"
        (ui/render-template-as-string
         tpl/add-menu-item-dialog
         {:feeds feeds
          :documents documents})
        (partial menu-item-dialog-callback
                 feeds
                 documents
                 new-ul)))))

  (ui/trigger-on-class
   "add-item-to-nested-menu"
   "click"
   (fn [e]
     (. e (preventDefault))
             
     (ui/display-dialog
      "Add Menu Item"
      (ui/render-template-as-string
       tpl/add-menu-item-dialog
       {:feeds feeds
        :documents documents})
      (partial menu-item-dialog-callback
               feeds
               documents
               (util/get-parent (util/get-parent (.-target e))))))))

(defn menu-container-el? [el]
  (= (.-id el) "menu-container"))

                         self-slug)))))))

(defn get-related-pages [related-page-el]
  (when related-page-el
    (map (fn [li-el]
           (let [slug-el (first (util/get-children-by-tag li-el "input"))
                 title-el (first (util/get-children-by-tag li-el "span"))]
             {:slug (.-value slug-el)
              :title (.-textContent title-el)}))
         (util/get-children related-page-el))))

(defn add-related-page-dialog-callback [feeds documents related-pages-el e]
  (when (= "ok" (.-key e))
