2011-05-29 02:46:38 -04:00
( function ( ) {
2012-07-17 11:03:20 -04:00
var MailCatcher ,
_ _bind = function ( fn , me ) { return function ( ) { return fn . apply ( me , arguments ) ; } ; } ;
2012-03-21 23:40:22 -04:00
jQuery . expr [ ':' ] . icontains = function ( a , i , m ) {
2013-01-28 21:21:32 -05:00
var _ref , _ref1 ;
return ( ( _ref = ( _ref1 = a . textContent ) != null ? _ref1 : a . innerText ) != null ? _ref : "" ) . toUpperCase ( ) . indexOf ( m [ 3 ] . toUpperCase ( ) ) >= 0 ;
2012-03-21 23:40:22 -04:00
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
MailCatcher = ( function ( ) {
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
function MailCatcher ( ) {
2012-07-17 11:03:20 -04:00
this . nextTab = _ _bind ( this . nextTab , this ) ;
2013-01-28 21:21:32 -05:00
2012-07-17 11:03:20 -04:00
this . previousTab = _ _bind ( this . previousTab , this ) ;
2013-01-28 21:21:32 -05:00
2012-07-17 11:03:20 -04:00
this . openTab = _ _bind ( this . openTab , this ) ;
2013-01-28 21:21:32 -05:00
2012-07-17 11:03:20 -04:00
this . selectedTab = _ _bind ( this . selectedTab , this ) ;
2013-01-28 21:21:32 -05:00
2012-07-17 11:03:20 -04:00
this . getTab = _ _bind ( this . getTab , this ) ;
2013-01-28 21:21:32 -05:00
2012-07-17 11:03:20 -04:00
var _this = this ;
$ ( '#messages tr' ) . live ( 'click' , function ( e ) {
2011-07-07 03:57:28 -04:00
e . preventDefault ( ) ;
2012-07-17 11:03:20 -04:00
return _this . loadMessage ( $ ( e . currentTarget ) . attr ( 'data-message-id' ) ) ;
} ) ;
$ ( 'input[name=search]' ) . keyup ( function ( e ) {
2012-03-21 23:40:22 -04:00
var query ;
query = $ . trim ( $ ( e . currentTarget ) . val ( ) ) ;
if ( query ) {
2012-07-17 11:03:20 -04:00
return _this . searchMessages ( query ) ;
2012-03-12 06:29:42 -04:00
} else {
2012-07-17 11:03:20 -04:00
return _this . clearSearch ( ) ;
2012-03-12 06:29:42 -04:00
}
2012-07-17 11:03:20 -04:00
} ) ;
$ ( '#message .views .format.tab a' ) . live ( 'click' , function ( e ) {
2011-07-07 03:57:28 -04:00
e . preventDefault ( ) ;
2012-07-17 11:03:20 -04:00
return _this . loadMessageBody ( _this . selectedMessage ( ) , $ ( $ ( e . currentTarget ) . parent ( 'li' ) ) . data ( 'message-format' ) ) ;
} ) ;
$ ( '#message .views .analysis.tab a' ) . live ( 'click' , function ( e ) {
2011-07-07 04:08:32 -04:00
e . preventDefault ( ) ;
2012-07-17 11:03:20 -04:00
return _this . loadMessageAnalysis ( _this . selectedMessage ( ) ) ;
} ) ;
2011-07-06 23:39:05 -04:00
$ ( '#resizer' ) . live ( {
mousedown : function ( e ) {
var events ;
e . preventDefault ( ) ;
return $ ( window ) . bind ( events = {
mouseup : function ( e ) {
e . preventDefault ( ) ;
return $ ( window ) . unbind ( events ) ;
} ,
mousemove : function ( e ) {
e . preventDefault ( ) ;
return $ ( '#messages' ) . css ( {
height : e . clientY - $ ( '#messages' ) . offset ( ) . top
} ) ;
}
} ) ;
}
} ) ;
2012-07-17 11:03:20 -04:00
$ ( 'nav.app .clear a' ) . live ( 'click' , function ( e ) {
2011-10-07 11:31:02 -04:00
e . preventDefault ( ) ;
2011-05-31 13:15:05 -04:00
if ( confirm ( "You will lose all your received messages.\n\nAre you sure you want to clear all messages?" ) ) {
return $ . ajax ( {
url : '/messages' ,
type : 'DELETE' ,
success : function ( ) {
2012-10-29 19:00:50 -04:00
return this . unselectMessage ( ) ;
2011-05-31 13:15:05 -04:00
} ,
error : function ( ) {
return alert ( 'Error while quitting.' ) ;
}
} ) ;
}
2012-07-17 11:03:20 -04:00
} ) ;
$ ( 'nav.app .quit a' ) . live ( 'click' , function ( e ) {
2011-10-07 11:31:02 -04:00
e . preventDefault ( ) ;
2011-05-31 13:14:56 -04:00
if ( confirm ( "You will lose all your received messages.\n\nAre you sure you want to quit?" ) ) {
return $ . ajax ( {
type : 'DELETE' ,
success : function ( ) {
return location . replace ( $ ( 'body > header h1 a' ) . attr ( 'href' ) ) ;
} ,
error : function ( ) {
return alert ( 'Error while quitting.' ) ;
}
} ) ;
2011-05-31 12:49:36 -04:00
}
2012-07-17 11:03:20 -04:00
} ) ;
key ( 'up' , function ( ) {
2012-10-24 17:54:07 -04:00
if ( _this . selectedMessage ( ) ) {
2012-10-29 18:40:34 -04:00
_this . loadMessage ( $ ( '#messages tr.selected' ) . prev ( ) . data ( 'message-id' ) ) ;
2012-10-24 17:54:07 -04:00
} else {
2012-10-29 18:40:34 -04:00
_this . loadMessage ( $ ( '#messages tbody tr[data-message-id]:first' ) . data ( 'message-id' ) ) ;
2012-10-24 17:54:07 -04:00
}
2012-10-24 17:21:08 -04:00
return false ;
2012-07-17 11:03:20 -04:00
} ) ;
key ( 'down' , function ( ) {
2012-10-24 17:54:07 -04:00
if ( _this . selectedMessage ( ) ) {
2012-10-29 18:40:34 -04:00
_this . loadMessage ( $ ( '#messages tr.selected' ) . next ( ) . data ( 'message-id' ) ) ;
2012-10-24 17:54:07 -04:00
} else {
2012-10-29 18:40:34 -04:00
_this . loadMessage ( $ ( '#messages tbody tr[data-message-id]:first' ) . data ( 'message-id' ) ) ;
2012-10-24 17:54:07 -04:00
}
2012-10-24 17:21:08 -04:00
return false ;
2012-07-17 11:03:20 -04:00
} ) ;
key ( '⌘+up, ctrl+up' , function ( ) {
2012-10-29 18:40:34 -04:00
_this . loadMessage ( $ ( '#messages tbody tr[data-message-id]:first' ) . data ( 'message-id' ) ) ;
2012-10-24 17:21:08 -04:00
return false ;
2012-07-17 11:03:20 -04:00
} ) ;
key ( '⌘+down, ctrl+down' , function ( ) {
2012-10-29 18:40:34 -04:00
_this . loadMessage ( $ ( '#messages tbody tr[data-message-id]:last' ) . data ( 'message-id' ) ) ;
2012-10-24 17:21:08 -04:00
return false ;
2012-07-17 11:03:20 -04:00
} ) ;
key ( 'left' , function ( ) {
2012-10-24 17:21:08 -04:00
_this . openTab ( _this . previousTab ( ) ) ;
return false ;
2012-07-17 11:03:20 -04:00
} ) ;
key ( 'right' , function ( ) {
2012-10-24 17:21:08 -04:00
_this . openTab ( _this . nextTab ( ) ) ;
return false ;
2012-07-17 11:03:20 -04:00
} ) ;
2012-10-24 18:45:26 -04:00
key ( 'backspace, delete' , function ( ) {
var id ;
id = _this . selectedMessage ( ) ;
if ( id != null ) {
$ . ajax ( {
url : '/messages/' + id ,
type : 'DELETE' ,
success : function ( ) {
2012-10-29 18:40:34 -04:00
var messageRow , switchTo ;
2012-10-29 18:34:14 -04:00
messageRow = $ ( "#messages tbody tr[data-message-id='" + id + "']" ) ;
2012-10-29 18:40:34 -04:00
switchTo = messageRow . next ( ) . data ( 'message-id' ) || messageRow . prev ( ) . data ( 'message-id' ) ;
2012-10-24 19:20:33 -04:00
messageRow . remove ( ) ;
if ( switchTo ) {
return _this . loadMessage ( switchTo ) ;
} else {
2012-10-29 19:00:50 -04:00
return _this . unselectMessage ( ) ;
2012-10-24 19:20:33 -04:00
}
2012-10-24 18:45:26 -04:00
} ,
error : function ( ) {
return alert ( 'Error while removing message.' ) ;
}
} ) ;
}
return false ;
} ) ;
2011-05-29 02:46:38 -04:00
this . refresh ( ) ;
this . subscribe ( ) ;
2011-05-27 09:46:12 -04:00
}
2012-07-17 11:03:20 -04:00
2011-05-29 03:42:07 -04:00
MailCatcher . prototype . parseDateRegexp = /^(\d{4})[-\/\\](\d{2})[-\/\\](\d{2})(?:\s+|T)(\d{2})[:-](\d{2})[:-](\d{2})(?:([ +-]\d{2}:\d{2}|\s*\S+|Z?))?$/ ;
2012-07-17 11:03:20 -04:00
2011-05-29 03:42:07 -04:00
MailCatcher . prototype . parseDate = function ( date ) {
var match ;
if ( match = this . parseDateRegexp . exec ( date ) ) {
2011-07-07 01:40:22 -04:00
return new Date ( match [ 1 ] , match [ 2 ] - 1 , match [ 3 ] , match [ 4 ] , match [ 5 ] , match [ 6 ] , 0 ) ;
2011-05-29 03:42:07 -04:00
}
} ;
2012-07-17 11:03:20 -04:00
2011-12-30 03:29:52 -05:00
MailCatcher . prototype . offsetTimeZone = function ( date ) {
var offset ;
offset = Date . now ( ) . getTimezoneOffset ( ) * 60000 ;
date . setTime ( date . getTime ( ) - offset ) ;
return date ;
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 03:42:07 -04:00
MailCatcher . prototype . formatDate = function ( date ) {
2013-01-28 21:21:32 -05:00
if ( typeof date === "string" ) {
date && ( date = this . parseDate ( date ) ) ;
}
2012-01-01 22:01:26 -05:00
date && ( date = this . offsetTimeZone ( date ) ) ;
2011-05-29 03:42:07 -04:00
return date && ( date = date . toString ( "dddd, d MMM yyyy h:mm:ss tt" ) ) ;
} ;
2012-07-17 11:03:20 -04:00
2012-04-19 11:26:40 -04:00
MailCatcher . prototype . messagesCount = function ( ) {
return $ ( '#messages tr' ) . length - 1 ;
} ;
2012-07-17 11:03:20 -04:00
2012-04-19 11:26:40 -04:00
MailCatcher . prototype . tabs = function ( ) {
return $ ( '#message ul' ) . children ( '.tab' ) ;
} ;
2012-07-17 11:03:20 -04:00
2012-04-19 11:26:40 -04:00
MailCatcher . prototype . getTab = function ( i ) {
return $ ( this . tabs ( ) [ i ] ) ;
} ;
2012-07-17 11:03:20 -04:00
2012-04-19 11:26:40 -04:00
MailCatcher . prototype . selectedTab = function ( ) {
return this . tabs ( ) . index ( $ ( '#message li.tab.selected' ) ) ;
} ;
2012-07-17 11:03:20 -04:00
2012-04-19 11:26:40 -04:00
MailCatcher . prototype . openTab = function ( i ) {
return this . getTab ( i ) . children ( 'a' ) . click ( ) ;
} ;
2012-07-17 11:03:20 -04:00
2012-04-19 11:26:40 -04:00
MailCatcher . prototype . previousTab = function ( tab ) {
var i ;
i = tab || tab === 0 ? tab : this . selectedTab ( ) - 1 ;
2013-01-28 21:21:32 -05:00
if ( i < 0 ) {
i = this . tabs ( ) . length - 1 ;
}
2012-04-19 11:26:40 -04:00
if ( this . getTab ( i ) . is ( ":visible" ) ) {
return i ;
} else {
return this . previousTab ( i - 1 ) ;
}
} ;
2012-07-17 11:03:20 -04:00
2012-04-19 11:26:40 -04:00
MailCatcher . prototype . nextTab = function ( tab ) {
var i ;
i = tab ? tab : this . selectedTab ( ) + 1 ;
2013-01-28 21:21:32 -05:00
if ( i > this . tabs ( ) . length - 1 ) {
i = 0 ;
}
2012-04-19 11:26:40 -04:00
if ( this . getTab ( i ) . is ( ":visible" ) ) {
return i ;
} else {
return this . nextTab ( i + 1 ) ;
}
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
MailCatcher . prototype . haveMessage = function ( message ) {
2013-01-28 21:21:32 -05:00
if ( message . id != null ) {
message = message . id ;
}
2011-05-31 12:40:13 -04:00
return $ ( "#messages tbody tr[data-message-id=\"" + message + "\"]" ) . length > 0 ;
2011-05-29 02:46:38 -04:00
} ;
2012-07-17 11:03:20 -04:00
2011-07-07 03:57:28 -04:00
MailCatcher . prototype . selectedMessage = function ( ) {
return $ ( '#messages tr.selected' ) . data ( 'message-id' ) ;
} ;
2012-07-17 11:03:20 -04:00
2012-03-21 23:40:22 -04:00
MailCatcher . prototype . searchMessages = function ( query ) {
var $rows , selector , token ;
selector = ( ( function ( ) {
var _i , _len , _ref , _results ;
_ref = query . split ( /\s+/ ) ;
_results = [ ] ;
for ( _i = 0 , _len = _ref . length ; _i < _len ; _i ++ ) {
token = _ref [ _i ] ;
_results . push ( ":icontains('" + token + "')" ) ;
}
return _results ;
} ) ( ) ) . join ( "" ) ;
$rows = $ ( "#messages tbody tr" ) ;
$rows . not ( selector ) . hide ( ) ;
return $rows . filter ( selector ) . show ( ) ;
2012-03-12 06:29:42 -04:00
} ;
2012-07-17 11:03:20 -04:00
2012-03-12 06:29:42 -04:00
MailCatcher . prototype . clearSearch = function ( ) {
return $ ( '#messages tbody tr' ) . show ( ) ;
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
MailCatcher . prototype . addMessage = function ( message ) {
2013-01-28 21:21:32 -05:00
return $ ( '#messages tbody' ) . prepend ( $ ( '<tr />' ) . attr ( 'data-message-id' , message . id . toString ( ) ) . append ( $ ( '<td/>' ) . text ( message . sender || "No sender" ) . toggleClass ( "blank" , ! message . sender ) ) . append ( $ ( '<td/>' ) . text ( ( message . recipients || [ ] ) . join ( ', ' ) || "No receipients" ) . toggleClass ( "blank" , ! message . recipients . length ) ) . append ( $ ( '<td/>' ) . text ( message . subject || "No subject" ) . toggleClass ( "blank" , ! message . subject ) ) . append ( $ ( '<td/>' ) . text ( this . formatDate ( message . created _at ) ) ) ) ;
2011-05-29 02:46:38 -04:00
} ;
2012-07-17 11:03:20 -04:00
2012-10-24 17:25:41 -04:00
MailCatcher . prototype . scrollToRow = function ( row ) {
var overflow , relativePosition ;
relativePosition = row . offset ( ) . top - $ ( '#messages' ) . offset ( ) . top ;
if ( relativePosition < 0 ) {
return $ ( '#messages' ) . scrollTop ( $ ( '#messages' ) . scrollTop ( ) + relativePosition - 20 ) ;
} else {
overflow = relativePosition + row . height ( ) - $ ( '#messages' ) . height ( ) ;
if ( overflow > 0 ) {
return $ ( '#messages' ) . scrollTop ( $ ( '#messages' ) . scrollTop ( ) + overflow + 20 ) ;
}
}
} ;
2012-10-29 19:00:50 -04:00
MailCatcher . prototype . unselectMessage = function ( ) {
$ ( '#messages tbody, #message .metadata dd' ) . empty ( ) ;
$ ( '#message .metadata .attachments' ) . hide ( ) ;
$ ( '#message iframe' ) . attr ( 'src' , 'about:blank' ) ;
return null ;
} ;
2011-05-29 02:46:38 -04:00
MailCatcher . prototype . loadMessage = function ( id ) {
2012-10-24 17:25:41 -04:00
var messageRow ,
_this = this ;
2013-01-28 21:21:32 -05:00
if ( ( id != null ? id . id : void 0 ) != null ) {
id = id . id ;
}
2011-05-31 12:40:13 -04:00
id || ( id = $ ( '#messages tr.selected' ) . attr ( 'data-message-id' ) ) ;
2011-05-29 02:46:38 -04:00
if ( id != null ) {
2012-10-29 18:34:14 -04:00
$ ( "#messages tbody tr:not([data-message-id='" + id + "'])" ) . removeClass ( 'selected' ) ;
messageRow = $ ( "#messages tbody tr[data-message-id='" + id + "']" ) ;
2012-10-24 17:25:41 -04:00
messageRow . addClass ( 'selected' ) ;
this . scrollToRow ( messageRow ) ;
2012-10-29 18:34:14 -04:00
return $ . getJSON ( "/messages/" + id + ".json" , function ( message ) {
2011-07-08 02:47:41 -04:00
var $ul ;
2012-07-17 11:03:20 -04:00
$ ( '#message .metadata dd.created_at' ) . text ( _this . formatDate ( message . created _at ) ) ;
2011-05-31 12:40:13 -04:00
$ ( '#message .metadata dd.from' ) . text ( message . sender ) ;
$ ( '#message .metadata dd.to' ) . text ( ( message . recipients || [ ] ) . join ( ', ' ) ) ;
$ ( '#message .metadata dd.subject' ) . text ( message . subject ) ;
$ ( '#message .views .tab.format' ) . each ( function ( i , el ) {
2011-05-29 02:46:38 -04:00
var $el , format ;
$el = $ ( el ) ;
2010-10-27 15:12:26 -04:00
format = $el . attr ( 'data-message-format' ) ;
2011-05-29 02:46:38 -04:00
if ( $ . inArray ( format , message . formats ) >= 0 ) {
2012-10-29 18:34:14 -04:00
$el . find ( 'a' ) . attr ( 'href' , "/messages/" + id + "." + format ) ;
2011-05-29 02:46:38 -04:00
return $el . show ( ) ;
} else {
return $el . hide ( ) ;
}
} ) ;
2011-05-31 12:40:13 -04:00
if ( $ ( "#message .views .tab.selected:not(:visible)" ) . length ) {
$ ( "#message .views .tab.selected" ) . removeClass ( "selected" ) ;
$ ( "#message .views .tab:visible:first" ) . addClass ( "selected" ) ;
2011-05-29 02:46:38 -04:00
}
if ( message . attachments . length ) {
2011-07-08 02:47:41 -04:00
$ul = $ ( '<ul/>' ) . appendTo ( $ ( '#message .metadata dd.attachments' ) . empty ( ) ) ;
2011-05-29 02:46:38 -04:00
$ . each ( message . attachments , function ( i , attachment ) {
2011-07-08 02:47:41 -04:00
return $ul . append ( $ ( '<li>' ) . append ( $ ( '<a>' ) . attr ( 'href' , attachment [ 'href' ] ) . addClass ( attachment [ 'type' ] . split ( '/' , 1 ) [ 0 ] ) . addClass ( attachment [ 'type' ] . replace ( '/' , '-' ) ) . text ( attachment [ 'filename' ] ) ) ) ;
2011-05-29 02:46:38 -04:00
} ) ;
$ ( '#message .metadata .attachments' ) . show ( ) ;
2010-10-27 15:12:26 -04:00
} else {
2011-05-29 02:46:38 -04:00
$ ( '#message .metadata .attachments' ) . hide ( ) ;
2010-10-27 15:12:26 -04:00
}
2011-06-10 08:47:38 -04:00
$ ( '#message .views .download a' ) . attr ( 'href' , "/messages/" + id + ".eml" ) ;
2011-07-07 04:08:32 -04:00
if ( $ ( '#message .views .tab.analysis.selected' ) . length ) {
2012-07-17 11:03:20 -04:00
return _this . loadMessageAnalysis ( ) ;
2011-07-07 04:08:32 -04:00
} else {
2012-07-17 11:03:20 -04:00
return _this . loadMessageBody ( ) ;
2011-07-07 04:08:32 -04:00
}
2012-07-17 11:03:20 -04:00
} ) ;
2011-05-29 02:46:38 -04:00
}
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
MailCatcher . prototype . loadMessageBody = function ( id , format ) {
2011-07-07 03:57:28 -04:00
id || ( id = this . selectedMessage ( ) ) ;
2011-05-31 12:40:13 -04:00
format || ( format = $ ( '#message .views .tab.format.selected' ) . attr ( 'data-message-format' ) ) ;
2011-05-29 02:46:38 -04:00
format || ( format = 'html' ) ;
2011-05-31 12:40:13 -04:00
$ ( "#message .views .tab[data-message-format=\"" + format + "\"]:not(.selected)" ) . addClass ( 'selected' ) ;
$ ( "#message .views .tab:not([data-message-format=\"" + format + "\"]).selected" ) . removeClass ( 'selected' ) ;
2011-05-29 02:46:38 -04:00
if ( id != null ) {
return $ ( '#message iframe' ) . attr ( "src" , "/messages/" + id + "." + format ) ;
}
} ;
2012-07-17 11:03:20 -04:00
2011-07-07 04:08:32 -04:00
MailCatcher . prototype . loadMessageAnalysis = function ( id ) {
2011-10-07 12:25:04 -04:00
var $form , $iframe ;
2011-07-07 04:08:32 -04:00
id || ( id = this . selectedMessage ( ) ) ;
$ ( "#message .views .analysis.tab:not(.selected)" ) . addClass ( 'selected' ) ;
$ ( "#message .views :not(.analysis).tab.selected" ) . removeClass ( 'selected' ) ;
if ( id != null ) {
2012-11-15 13:05:00 -05:00
$iframe = $ ( '#message iframe' ) . contents ( ) . children ( ) . html ( "<html>\n<head>\n<title>Analysis</title>\n" + ( $ ( 'link[rel="stylesheet"]' ) [ 0 ] . outerHTML ) + "\n</head>\n<body class=\"iframe\">\n<h1>Analyse your email with Fractal</h1>\n<p><a href=\"http://getfractal.com/\" target=\"_blank\">Fractal</a> is a really neat service that applies common email design and development knowledge from <a href=\"http://www.email-standards.org/\" target=\"_blank\">Email Standards Project</a> to your HTML email and tells you what you've done wrong or what you should do instead.</p>\n<p>Please note that this <strong>sends your email to the Fractal service</strong> for analysis. Read their <a href=\"https://www.getfractal.com/page/terms\" target=\"_blank\">terms of service</a> if you're paranoid.</p>\n<form>\n<input type=\"submit\" value=\"Analyse\" /><span class=\"loading\" style=\"color: #999; display: none\">Analysing…</span>\n</form>\n</body>\n</html>" ) ;
2011-10-07 12:25:04 -04:00
return $form = $iframe . find ( 'form' ) . submit ( function ( e ) {
e . preventDefault ( ) ;
$ ( this ) . find ( 'input[type="submit"]' ) . attr ( 'disabled' , 'disabled' ) . end ( ) . find ( '.loading' ) . show ( ) ;
2011-12-22 14:34:40 -05:00
return $ ( '#message iframe' ) . contents ( ) . find ( 'body' ) . xslt ( "/messages/" + id + "/analysis.xml" , "/stylesheets/analysis.xsl" ) ;
2011-07-07 04:08:32 -04:00
} ) ;
}
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
MailCatcher . prototype . refresh = function ( ) {
2012-07-17 11:03:20 -04:00
var _this = this ;
return $ . getJSON ( '/messages' , function ( messages ) {
return $ . each ( messages , function ( i , message ) {
2013-01-28 21:21:32 -05:00
if ( ! _this . haveMessage ( message ) ) {
return _this . addMessage ( message ) ;
}
2012-07-17 11:03:20 -04:00
} ) ;
} ) ;
2011-05-29 02:46:38 -04:00
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
MailCatcher . prototype . subscribe = function ( ) {
2012-07-17 11:03:20 -04:00
if ( typeof WebSocket !== "undefined" && WebSocket !== null ) {
2011-05-29 02:46:38 -04:00
return this . subscribeWebSocket ( ) ;
} else {
return this . subscribePoll ( ) ;
}
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
MailCatcher . prototype . subscribeWebSocket = function ( ) {
2012-07-17 11:03:20 -04:00
var secure ,
_this = this ;
2011-05-29 02:46:38 -04:00
secure = window . location . scheme === 'https' ;
this . websocket = new WebSocket ( "" + ( secure ? 'wss' : 'ws' ) + "://" + window . location . host + "/messages" ) ;
2012-07-17 11:03:20 -04:00
return this . websocket . onmessage = function ( event ) {
return _this . addMessage ( $ . parseJSON ( event . data ) ) ;
} ;
2011-05-29 02:46:38 -04:00
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
MailCatcher . prototype . subscribePoll = function ( ) {
2012-07-17 11:03:20 -04:00
var _this = this ;
2011-05-29 02:46:38 -04:00
if ( this . refreshInterval == null ) {
2012-07-17 11:03:20 -04:00
return this . refreshInterval = setInterval ( ( function ( ) {
return _this . refresh ( ) ;
} ) , 1000 ) ;
2011-05-29 02:46:38 -04:00
}
} ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
return MailCatcher ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
} ) ( ) ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
$ ( function ( ) {
return window . MailCatcher = new MailCatcher ;
} ) ;
2012-07-17 11:03:20 -04:00
2011-05-29 02:46:38 -04:00
} ) . call ( this ) ;