9 changed files with 177 additions and 58 deletions
@ -1,43 +1,62 @@ |
|||||||
(ns NoteHub.main |
(ns NoteHub.main |
||||||
(:use [jayq.core :only [$ xhr css inner val anim show]]) |
(:use [jayq.core :only [$ xhr css inner val anim show]]) |
||||||
(:require [goog.dom :as gdom] |
(:require [goog.dom :as gdom] |
||||||
|
[goog.crypt.Md5 :as md5] |
||||||
|
[goog.crypt :as crypt] |
||||||
[NoteHub.crossover.lib :as lib] |
[NoteHub.crossover.lib :as lib] |
||||||
[clojure.browser.dom :as dom])) |
[clojure.browser.dom :as dom])) |
||||||
|
|
||||||
; frequently used selectors |
; frequently used selectors |
||||||
(def $draft ($ :#draft)) |
(def $draft ($ :#draft)) |
||||||
|
(def $action ($ :#action)) |
||||||
(def $preview ($ :#preview)) |
(def $preview ($ :#preview)) |
||||||
|
(def $password ($ :#password)) |
||||||
|
(def $plain-password ($ :#plain-password)) |
||||||
(def $input-elems ($ :#input-elems)) |
(def $input-elems ($ :#input-elems)) |
||||||
(def $preview-start-line ($ :#preview-start-line)) |
(def $dashed-line ($ :#dashed-line)) |
||||||
|
|
||||||
; Markdown Converter & Sanitizer instantiation |
; Markdown Converter & Sanitizer instantiation |
||||||
|
|
||||||
(def md-converter (Markdown/getSanitizingConverter)) |
(def md-converter (Markdown/getSanitizingConverter)) |
||||||
|
|
||||||
|
; instantiate & reset a MD5 hash digester |
||||||
|
(def md5 (goog.crypt.Md5.)) |
||||||
|
(.reset md5) |
||||||
|
|
||||||
; try to detect iOS |
; try to detect iOS |
||||||
(def ios-detected (.match (.-userAgent js/navigator) "(iPad|iPod|iPhone)")) |
(def ios-detected (.match (.-userAgent js/navigator) "(iPad|iPod|iPhone)")) |
||||||
|
|
||||||
|
(defn update-preview |
||||||
|
[] |
||||||
|
"Updates the preview" |
||||||
|
(do |
||||||
|
(show $dashed-line) |
||||||
|
(show $input-elems) |
||||||
|
(inner $preview |
||||||
|
(.makeHtml md-converter (val $draft))))) |
||||||
|
|
||||||
; set focus to the draft textarea (if there is one) |
; set focus to the draft textarea (if there is one) |
||||||
(when $draft |
(when $action |
||||||
(do |
(do |
||||||
(val $draft "") |
(if (= "update" (val $action)) |
||||||
|
(update-preview) |
||||||
|
(val $draft "")) |
||||||
; foces setting is impossible in iOS, so we border the field instead |
; foces setting is impossible in iOS, so we border the field instead |
||||||
(if ios-detected |
(if ios-detected |
||||||
(.addClass $draft "ui-border") |
(.addClass $draft "ui-border") |
||||||
(.focus $draft)))) |
(.focus $draft)))) |
||||||
|
|
||||||
; show the preview & publish buttons as soon as the user starts typing. |
; show the preview & publish buttons as soon as the user starts typing. |
||||||
(.keyup $draft |
(.keyup $draft update-preview) |
||||||
(fn [e] |
|
||||||
(do |
|
||||||
(show $preview-start-line) |
|
||||||
(show $input-elems) |
|
||||||
(inner $preview |
|
||||||
(.makeHtml md-converter (val $draft)))))) |
|
||||||
|
|
||||||
; when the publish button is clicked, compute the hash of the entered text and |
; when the publish button is clicked, compute the hash of the entered text and |
||||||
; provided session key and assign to the field session-value |
; provided session key and assign to the field session-value; |
||||||
|
; moreover, compute the password hash as md5 before transmission |
||||||
(.click ($ :#publish-button) |
(.click ($ :#publish-button) |
||||||
(fn [e] |
(fn [e] |
||||||
|
(do |
||||||
|
(.update md5 (val $plain-password)) |
||||||
|
(val $plain-password nil) |
||||||
|
(when (val $plain-password) |
||||||
|
(val $password (crypt/byteArrayToHex (.digest md5)))) |
||||||
(val ($ :#session-value) |
(val ($ :#session-value) |
||||||
(lib/hash #(.charCodeAt % 0) (str (val $draft) (val ($ :#session-key))))))) |
(lib/hash #(.charCodeAt % 0) (str (val $draft) (val ($ :#session-key)))))))) |
||||||
|
|||||||
Loading…
Reference in new issue