neorg / neorg / static / neorg.coffee

#### Open neighboring image
#
# Argument:
#
# + **`arrow`** (`string`) : one of `arrowKeyArray`
neorgCBOpen = (arrow) ->
  ctd = $.colorbox.element().parent()
  if arrow == "left"
    otd = ctd.prev()
  else if arrow == "right"
    otd = ctd.next()
  else
    ctr = ctd.parent()
    idx = ctr.children().index(ctd[0])

    return if idx == undefined
    if arrow == "up"
      otr = ctr.prev()
    else if arrow == "down"
        otr = ctr.next()
    otd = otr.children().eq(idx)

  oa = otd.children("a.neorg-gene-image-link").first()
  oa.click() if oa.length


#### Bind arrow keys using [jquery.hotkeys]
#
# [jquery.hotkeys]: https://github.com/tzuryby/jquery.hotkeys/
bindKeydownToNeorgCBOpen = (key) ->
  $(document).bind "keydown.neorg." + key, key, (e) ->
      e.preventDefault()
      neorgCBOpen key
      return


#### Unbind arrow keys; called via colorbox's `onClosed`
unBindKeydown = (key) ->
  $(document).unbind "keydown.neorg." + key

arrowKeyArray = [ "left", "right", "up", "down" ]


#### Setting for [colorbox] plugin
#
# [colorbox]: http://jacklmoore.com/colorbox/
neorgCBSetting =
  title: ->
    $(this).attr "title", $(this).prev("p").text()
    return  # return nothing

  transition: "none"
  speed: 0
  maxWidth: "100%"
  maxHeight: "100%"
  fixed: true
  onOpen: ->
    for i of arrowKeyArray
      bindKeydownToNeorgCBOpen arrowKeyArray[i]

  onClosed: ->
    for i of arrowKeyArray
      unBindKeydown arrowKeyArray[i]


#### Colorize dictdiff table using [jquery.heatcolor] plugin
#
# [jquery.heatcolor]: http://www.jnathanson.com/blog/client/jquery/heatcolor/
neorgDictDiffInit = ->
  $("table.neorg-dictdiff").each ->
    ncolsList = ($(e).find("td").length for e in $(this).find("tr"))
    ncols = Math.max ncolsList...

    for column in [1..ncols]
      $(this).find("tr > td:nth-child(" + column + ")")[1..]
        .heatcolor -> $(this).text()
    return


#### Dynamically load text area to edit current page
#
# This function will be invoked by clicking `a.page-action-edit`
neorgEdit = (e) ->
  e.preventDefault()
  $("#edit-form-wrapper").html "<p>Loading...</p>"
  $.ajax
    url: "_edit_form"
    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"], true
      return


#### Initialize #edit-form-textarea
#
# This function activates some functionality for the text area:
#
# + When the textarea is not changed, buttons specified in `buttons`
#   will be disabled.
# + Ask user if he/she wants to leave the page if the text is
#   changed.
#
# 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.
# + **`saved`** (`bool`, _optional_) :
#   pass `false` to it if the text is not saved in the DB
neorgTextAreaInit = (buttons = ["preview"], saved = false) ->
  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()

  editFormSubmitted = false
  $("#edit-form").bind "submit", (e) ->
    editFormSubmitted = true

  shouldNotUnload = ->
    not editFormSubmitted \
      and (not saved or original != $("#edit-form-textarea").val())

  $(window).bind "beforeunload", ->
    if shouldNotUnload()
      return "You will loose unsaved changes."
  return


#### Initialize everything for a neorg page
#
neorgInit = ->
  $(".neorg-gene-image-link").colorbox neorgCBSetting

  neorgDictDiffInit()

  $("a.page-action-edit").click neorgEdit
  neorgTextAreaInit() if $("#edit-form-textarea").length > 0
  $("#edit-form-textarea").focus()


#### Export `neorgInit` as a global function
# see: <http://stackoverflow.com/questions/4214731/>
root = exports ? this
root.neorgInit = neorgInit
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.