2014-06-23 20:55:38 -07:00
( function ( Site ) { var Snipt = snipt . module ( 'snipt' ) ; Backbone . oldSync = Backbone . sync ; Backbone . Model . prototype . idAttribute = 'resource_uri' ; var addSlash = function ( str ) { return str + ( ( str . length > 0 && str . charAt ( str . length - 1 ) === '/' ) ? '' : '/' ) ; } ; Backbone . sync = function ( method , model , options ) { options . headers = _ . extend ( { 'Authorization' : 'ApiKey ' + window . user + ':' + window . api _key } , options . headers ) ; return Backbone . oldSync ( method , model , options ) ; } ; Backbone . Model . prototype . url = function ( ) { var url = this . id ; if ( ! url ) { url = this . urlRoot ; url = url || this . collection && ( _ . isFunction ( this . collection . url ) ? this . collection . url ( ) : this . collection . url ) ; if ( url && this . has ( 'id' ) ) { url = addSlash ( url ) + this . get ( 'id' ) ; } }
url = url && addSlash ( url ) ; if ( typeof url === 'undefined' ) { url = '/api/private/snipt/' ; this . unset ( 'id' , { 'silent' : true } ) ; this . unset ( 'user' , { 'silent' : true } ) ; }
return url || null ; } ; Site . SiteView = Backbone . View . extend ( { el : 'body' , initialize : function ( opts ) { this . $body = $ ( this . el ) ; this . $html = $ ( 'html' ) ; this . $html _body = this . $body . add ( this . $html ) ; this . $aside _main = $ ( 'aside.main' , this . $body ) ; this . $aside _nav = $ ( 'aside.nav' , this . $body ) ; this . $aside _nav _ul = $ ( 'ul' , this . $aside _nav ) ; this . $search _form = $ ( 'form.search' , this . $body ) ; this . $search _query = $ ( 'input#search-query' , this . $body ) ; this . $search _page _query = $ ( 'input.search-query' , this . $body ) ; this . $search _queries = this . $search _query . add ( this . $search _page _query ) ; this . $snipts = $ ( 'section#snipts article.snipt' , this . $body ) ; this . $modals = $ ( 'div.modal' , this . $snipts ) ; this . $main _edit = $ ( 'section#main-edit' ) ; this . $main = $ ( 'section#main' ) ; this . $keyboard _shortcuts = $ ( '#keyboard-shortcuts' , this . $body ) ; this . keyboardShortcuts ( ) ; this . inFieldLabels ( ) ; var SniptListView = Snipt . SniptListView ; this . snipt _list = new SniptListView ( { 'snipts' : this . $snipts } ) ; var that = this ; this . $body . click ( function ( ) { if ( ! window . ui _halted && ! window . from _modal && window . $selected ) { window . $selected . trigger ( 'deselect' ) ; }
if ( window . from _modal ) { window . from _modal = false ; }
that . $aside _nav . removeClass ( 'open' ) ; } ) ; this . $aside _nav _ul . click ( function ( e ) { e . stopPropagation ( ) ; } ) ; $search _queries = this . $search _queries ; $search _queries . focus ( function ( ) { if ( window . $selected ) { $selected . trigger ( 'deselect' ) ; } } ) ; this . $body . on ( 'click' , 'a.close' , function ( ) { $ ( this ) . parent ( ) . parent ( ) . modal ( 'hide' ) ; window . ui _halted = false ; return false ; } ) ; this . $keyboard _shortcuts . on ( 'hidden' , function ( ) { window . ui _halted = false ; } ) ; if ( this . $body . hasClass ( 'pro-signup' ) ) { var $form = $ ( 'form#pro-signup' ) ; var $submit = $ ( 'button[type="submit"]' , $form ) ; var $name = $ ( 'input#name' ) ; var $cardNumber = $ ( 'input#number' ) ; var $expMonth = $ ( 'select#exp-month' ) ; var $expYear = $ ( 'select#exp-year' ) ; var $cvc = $ ( 'input#cvc' ) ; $form . submit ( function ( ) { $submit . attr ( 'disabled' , 'disabled' ) ; var errors = false ; if ( ! Stripe . validateCardNumber ( $cardNumber . val ( ) ) ) { $cardNumber . parents ( 'div.control-group' ) . addClass ( 'error' ) ; errors = true ; } else { $cardNumber . parents ( 'div.control-group' ) . removeClass ( 'error' ) ; }
if ( ! Stripe . validateExpiry ( $expMonth . val ( ) , $expYear . val ( ) ) ) { $expMonth . parents ( 'div.control-group' ) . addClass ( 'error' ) ; errors = true ; } else { $expMonth . parents ( 'div.control-group' ) . removeClass ( 'error' ) ; }
if ( ! Stripe . validateCVC ( $cvc . val ( ) ) ) { $cvc . parents ( 'div.control-group' ) . addClass ( 'error' ) ; errors = true ; } else { $cvc . parents ( 'div.control-group' ) . removeClass ( 'error' ) ; }
if ( ! errors ) { $ ( '.payment-errors' ) . hide ( ) ; $ ( '.payment-loading' ) . show ( ) ; Stripe . createToken ( { name : $name . val ( ) , number : $cardNumber . val ( ) , cvc : $cvc . val ( ) , exp _month : $expMonth . val ( ) , exp _year : $expYear . val ( ) } , that . stripeResponseHandler ) ; } else { $submit . removeAttr ( 'disabled' ) ; }
return false ; } ) ; }
if ( this . $body . hasClass ( 'login' ) ) { $ ( 'input#id_username' ) . focus ( ) ; }
if ( window . gittip _username ) { this . $aside _main . html ( this . $aside _main . html ( ) . replace ( /\[\[.*gittip.*\]\]/ , '<iframe style="border: 0; margin: 0; padding: 0;" src="https://www.gittip.com/' + window . gittip _username + '/widget.html" width="48pt" height="22pt"></iframe>' ) ) ; $ ( 'iframe' , this . $aside _main ) . parent ( 'p' ) . prev ( 'p' ) . css ( 'margin-bottom' , '10px' ) ; }
$ ( 'div.markdown pre code' ) . each ( function ( i , e ) { hljs . highlightBlock ( e ) ; } ) ; var $embeddedTweets = $ ( 'div.embedded-tweet' ) ; if ( $embeddedTweets . length ) { $ . each ( $embeddedTweets , function ( ) { var $tweetPlaceholder = $ ( this ) ; var tweetID = $tweetPlaceholder . attr ( 'data-tweet-id' ) ; $ . ajax ( { url : 'https://api.twitter.com/1/statuses/oembed.json?id=' + tweetID + '&align=center' , dataType : 'jsonp' , type : 'get' , success : function ( resp ) { $tweetPlaceholder . replaceWith ( $ ( resp . html ) ) ; } } ) ; } ) ; }
window . ui _halted = false ; } , events : { 'showKeyboardShortcuts' : 'showKeyboardShortcuts' , 'click a.mini-profile' : 'toggleMiniProfile' } , keyboardShortcuts : function ( ) { var $body = this . $body ; var that = this ; $search _queries = this . $search _queries ; $search _page _query = this . $search _page _query ; $search _query = this . $search _query ; $document = $ ( document ) ; $document . bind ( 'keydown' , '/' , function ( e ) { if ( ! window . ui _halted ) { e . preventDefault ( ) ; if ( $body . hasClass ( 'search' ) ) { $search _page _query . focus ( ) ; } else { $search _query . focus ( ) ; } } } ) ; $document . bind ( 'keydown' , 'Ctrl+h' , function ( e ) { if ( ! window . ui _halted ) { window . ui _halted = true ; $body . trigger ( 'showKeyboardShortcuts' ) ; } else { if ( that . $keyboard _shortcuts . is ( ':visible' ) ) { that . $keyboard _shortcuts . modal ( 'hide' ) ; } } } ) ; this . $search _queries . bind ( 'keydown' , 'esc' , function ( e ) { if ( ! window . ui _halted ) { e . preventDefault ( ) ; this . blur ( ) ; } } ) ; } , showKeyboardShortcuts : function ( ) { this . $keyboard _shortcuts . modal ( 'toggle' ) ; } , toggleMiniProfile : function ( e ) { this . $aside _nav . toggleClass ( 'open' ) ; return false ; } , inFieldLabels : function ( ) { $ ( 'div.infield label' , this . $body ) . inFieldLabels ( { fadeDuration : 200 } ) ; } , stripeResponseHandler : function ( status , response ) { var $form = $ ( 'form#pro-signup' ) ; if ( response . error ) { $ ( 'button[type="submit"]' , $form ) . removeAttr ( 'disabled' ) ; $ ( '.payment-loading' ) . hide ( ) ; $ ( '.payment-errors' ) . text ( response . error . message ) . show ( ) ; } else { var token = response . id ; $ ( 'input#name' ) . val ( '' ) ; $ ( 'input#number' ) . val ( '' ) ; $ ( 'select#exp-month' ) . val ( '' ) ; $ ( 'select#exp-year' ) . val ( '' ) ; $ ( 'input#cvc' ) . val ( '' ) ; $form . append ( "<input type='hidden' name='token' value='" + token + "'/>" ) ; $form . get ( 0 ) . submit ( ) ; } } } ) ; } ) ( snipt . module ( 'site' ) ) ;