                           (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"

                           (get-links-from-ul menu-container-el)
                           feeds
                           documents))))
  
(defn menu-item-dialog-callback [feeds documents parent-el e]
  (if (= "ok" (.-key e))
    (let [nested (classes/has parent-el "new-node")


(defn get-link-data-from-li [el]
  (when-not (classes/has el "add-item-node") ; ignore "Add Item" in nested ul
    ;; remove meta tag (sometimes added due to a bug in Chrome)
    (let [children (remove #(= (.-tagName %) "meta") (util/get-children el))
          item-detail-elements (vec (util/get-children (first children)))
          label-el (nth item-detail-elements 0)
          uri-el (nth item-detail-elements 1)]
      {:label (. label-el -textContent)
       :uri (. uri-el -textContent)
       :children (when (= (.-tagName (last children)) "UL")
                   (get-links-from-ul (last children)))})))

