                          self-slug)))))))
 
 (defn get-related-pages [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)))
+  (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))


(defn get-link-data-from-li [el]
  (when-not (classes/has el "add-item-node") ; ignore "Add Item" in nested ul
    (let [children (util/get-children el)
          item-detail-elements (util/get-children (first children))]
      {:label (.-textContent (nth item-detail-elements 0))
       :uri (.-textContent (nth item-detail-elements 1))
       :children (when (= (.-tagName (last children)) "UL")
                   (get-links-from-ul (last children)))})))


                          (remove-self-from-documents self-slug
                                                      documents))})))

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

(defn add-related-page-link-callback [self-slug feeds documents evt]
  (ui/display-dialog "Add Related Page"
