var OKAdmin = function(){ // initialize our (single) ajax image uploader with an element and a template OKUpload.bind( document.getElementById("file") ) OKUpload.add = function(data){ var url = data[0].extra.Location add_image(url) } // also handle straight image urls $("#add-image-url").keydown(pressEnter(function(e){ var url = $(this).val() $(this).val("") add_image(url) })}) // clone and populate template function add_image(url){ var imageTemplate = $("#captioned-image-template").html() var $el = $(imageTemplate) $el.find(".uri").val(url) $el.find("img").attr("src", url) $(".captioned-image-list ol").append($el) } // make the region sortable with drag-and-drop $(".captioned-image-list ol").sortable() $(".captioned-image-list ol").disableSelection() // delete image $(document).on("click", ".remove-image", function(){ if (confirm("Delete this image?")) { $(this).parent().remove() } }) // populate a video field with info from our url parser $(".video .url").keydown(pressEnter(function(){ var $el = $(this) var url = $el.val() Parser.parse( url, function(media){ console.log(url,media) $el.parent().addClass("loaded") $el.parent().find(".video-type").val( media.type ) $el.parent().find(".video-token").val( media.token ) $el.parent().find(".video-title").val( media.title ) $el.parent().find(".video-thumb").val( media.thumbnail ) }) }})) // fix post indexing in list-driven inputs $("form").submit(function(){ $(".image-element").each(function(index){ $(this).find("input,textarea").each(function(){ var field = $(this).attr("name").replace(/\[\]/, "[" + index + "]") $(this).attr("name", field) }) }) }) function pressEnter(fn){ return function(e){ if (e.keyCode !== 13) return e.preventDefault() } } $(function(){ window.app = new OKAdmin () })