CKEDITOR.disableAutoInline = true; unsavedChanges = false window.onbeforeunload = -> return "Are you sure you want to leave this page? Your unsaved changes will not be stored!" if unsavedChanges $ -> $(document).ready -> # Find blocks blocks = $(".polyblock[contenteditable='true']") return unless blocks.length # Find switches pb_switch = $('.polyblock-switch') has_switch = pb_switch.is('*') switch_list = [] # Clone each block and instanciate CKeditor on the original blockclones = {} instanciateCKEditor = (id)-> CKEDITOR.inline id, on: focus:(e)-> editorModeOn() startListening(id) blur:(e)-> stopListening() blocks.each -> id = $(@).attr('id') blockclones[id] = $(@).clone() unless _.contains(_.keys(CKEDITOR.instances),id) if has_switch then switch_list.push(id) else instanciateCKEditor(id) if has_switch pb_switch.click (e)-> e.preventDefault() if $(@).hasClass("active") then window.location.reload() else instanciateCKEditor(id) for id in switch_list $(@).addClass("active") # Wait for changes verbose = false changeChecker = null indicator = $('.pb-change-indicator') changeText = $('.pb-change-text') startListening = (which)-> stopListening() console.log("I'm listening...") if verbose buffer = CKEDITOR.instances[which].getData() changeChecker = setInterval -> console.log("Checking for changes...") if verbose reading = CKEDITOR.instances[which].getData() if reading!=buffer and !unsavedChanges then showUnsavedChanges() else if reading==buffer and unsavedChanges then showSavedChanges() #buffer = reading , 1000 stopListening = -> return unless changeChecker? console.log("I'm no longer listening.") if verbose clearInterval(changeChecker) changeChecker = null showUnsavedChanges = -> indicator.transition({rotate:"-30deg",color:"red"}) changeText.html("You have unsaved changes.").transition({color:"red"}) unsavedChanges = true showSavedChanges = -> indicator.transition({rotate:"0deg",color:"green"}) changeText.html("All changes saved.").transition({color:"green"}) unsavedChanges = false # Editor functionality toggles initialBodyPadding = $('body').css("padding-bottom") editorModeOn = (pb)-> $('#pb_bar').show "slide", {direction:"down"}, 250, -> $('body').css("padding-bottom",$("#pb_bar").height() + 20) pb?() editorModeOff = (pb)-> $('#pb_bar').hide "slide", {direction:"down"}, 250, -> $('body').css("padding-bottom",initialBodyPadding) pb?() # Change storage saveChanges = -> showSavedChanges() pbs = _.object(_.map(blocks,(b)-> $(b).data("pbid")), _.map(blocks,(b)-> $(b).html())) $.post "/polyblock/update", {pbs:pbs}, (data)-> editorModeOff -> alertify.success("Your changes have been saved.") revertChanges = (pb)-> return unless confirm("Are you sure you want to discard your changes?") editorModeOff -> blocks.each -> $(@).replaceWith(blockclones[$(@).attr("id")]) alertify.success("Your changes have been discarded.") # Modes currentMode = previousMode = "WYSIWYG" convertToTextarea = (block,content=null,tab="\t")-> tabchars = tab.replace(/\\/,"").length previous_textarea = $("textarea[data-pbid=#{block.attr("data-pbid")}]") content ?= if previous_textarea.is("*") then previous_textarea.val() else block.html() textarea = $('