Commits

Takafumi Arakaki committed 1e5f271

Changed neorg.js to disable/enable Save/Preview

Comments (0)

Files changed (2)

neorg/static/neorg.coffee

     success: (data) ->
       $("#edit-form-wrapper").html data
       $("#edit-form-textarea").focus()
+      # Call `neorgTextAreaInit` to disable buttons until the text is
+      # changed.  Here, `"save"` is in the button list because
+      # dynamically loaded text is not needed to be saved unless it is
+      # changed.
+      neorgTextAreaInit ["save", "preview"]
       return
 
 
+#### Initialize #edit-form-textarea
+#
+# When the textarea is not changed, buttons specified in `buttons`
+# will be disabled.
+#
+# Argument:
+#
+# * `buttons` (`[string]`, _optional_) :
+#   "`#edit-form-STR`" (where `STR` is an element in the list)
+#   must be an ID of the input element in the edit form.
+neorgTextAreaInit = (buttons = ["preview"]) ->
+  btnList = ($("#edit-form-#{n}") for n in buttons)
+  original = $("#edit-form-textarea").val()
+  watchTextarea = ->
+    if original == $("#edit-form-textarea").val()
+      $(b).attr("disabled", true) for b in btnList
+    else
+      $(b).attr("disabled", false) for b in btnList
+  $("#edit-form-textarea").keyup watchTextarea
+  watchTextarea()
+
+
 #### Initialize everything for a neorg page
 #
 neorgInit = ->
   neorgDictDiffInit()
 
   $("a.page-action-edit").click neorgEdit
+  neorgTextAreaInit() if $("#edit-form-textarea")
   $("#edit-form-textarea").focus()
 
 

neorg/templates/edit_form.html

   <textarea name="page_text" rows="20" cols="80" id="edit-form-textarea">
     {{- page_text -}}
   </textarea>
-  <input type="submit" name="save" value="Save" />
-  <input type="submit" name="preview" value="Preview" />
-  <input type="submit" name="cancel" value="Cancel" />
-  <input type="submit" name="delete" value="Delete" />
+  <input type="submit" name="save" value="Save" id="edit-form-save" />
+  <input type="submit" name="preview" value="Preview" id="edit-form-preview" />
+  <input type="submit" name="cancel" value="Cancel" id="edit-form-cancel" />
+  <input type="submit" name="delete" value="Delete" id="edit-form-delete" />
 </form>