 # You can use CoffeeScript in this file: http://coffeescript.org/
 
 artifact_search_fields = ['apps', 'tags', 'license', 'hash', 'formats']
+
+# Make tag text editable after it's deleted
+# Based on https://github.com/select2/select2/issues/116#issuecomment-56856875
+# But I totally butchered her code to make it fit my use case here
+#   id: Takes the select2 element
+#   token_function: A function to extract the text from the removed tag
+_setup_select2_delete_callback = (id, token_function) ->
+  s2c = $(id).select2("container")
+  inputField = s2c.find(".select2-input")
+
+  _checkEdit = (e) ->
+
+    if e.keyCode == 8 && !$(e.target).val().length
+
+      lastTag = $(e.target).parent().prev().last()
+      lastText = token_function(lastTag)
+
+      currentTags = s2c.select2("data")
+
+      findTags = (item) ->
+        item unless item.text == lastText
+
+      remainingTags = currentTags.filter(findTags)
+
+      lastTag.remove()
+
+      s2c.select2("data", remainingTags)
+      s2c.find("li.select2-search-field input").val(lastText)
+
+  inputField.on("keydown", _checkEdit)
 
 initialize_select = (field_id, url) ->
   return if not $(field_id)[0]

      results: (data, page) ->
        results: data.map (term) -> {id: term, text: term}

  $(field_id).ready ->
    values = []


    window.onresize = ->
      editor.reflow()

initialize_wayback_links = ->
  wayback_url = 'https://web.archive.org/web/*/'
  $('#show-wayback-links').on 'click', (e) ->
