@@ -61,6 +61,11 @@ var haste = function(appName, options) {
61
61
this . $code = $ ( '#box code' ) ;
62
62
this . options = options ;
63
63
this . configureShortcuts ( ) ;
64
+ this . configureButtons ( ) ;
65
+ // If twitter is disabled, hide the button
66
+ if ( ! options . twitter ) {
67
+ $ ( '#key .box2 .twitter' ) . hide ( ) ;
68
+ }
64
69
} ;
65
70
66
71
// Set the page title - include the appName
@@ -71,23 +76,27 @@ haste.prototype.setTitle = function(ext) {
71
76
72
77
// Show the light key
73
78
haste . prototype . lightKey = function ( ) {
74
- var text = '' ;
75
- text += '<em>' + this . appName + '</em>' ;
76
- text += '^s - save<br>' ;
77
- text += '^n - new' ;
78
- $ ( '#key' ) . html ( text ) ;
79
+ this . configureKey ( [ 'new' , 'save' ] ) ;
79
80
} ;
80
81
81
82
// Show the full key
82
83
haste . prototype . fullKey = function ( ) {
83
- var text = '' ;
84
- text += '<em>' + this . appName + '</em>' ;
85
- text += '^n - new<br>' ;
86
- text += '^d - duplicate<br>' ;
87
- if ( this . options . twitter ) {
88
- text += '^t - twitter' ;
89
- }
90
- $ ( '#key' ) . html ( text ) ;
84
+ this . configureKey ( [ 'new' , 'duplicate' , 'twitter' , 'link' ] ) ;
85
+ } ;
86
+
87
+ // Set the key up for certain things to be enabled
88
+ haste . prototype . configureKey = function ( enable ) {
89
+ var $this , i = 0 ;
90
+ $ ( '#key .box2 .function' ) . each ( function ( ) {
91
+ $this = $ ( this ) ;
92
+ for ( i = 0 ; i < enable . length ; i ++ ) {
93
+ if ( $this . hasClass ( enable [ i ] ) ) {
94
+ $this . addClass ( 'enabled' ) ;
95
+ return true ;
96
+ }
97
+ }
98
+ $this . removeClass ( 'enabled' ) ;
99
+ } ) ;
91
100
} ;
92
101
93
102
// Remove the current document (if there is one)
@@ -170,32 +179,101 @@ haste.prototype.lockDocument = function() {
170
179
} ) ;
171
180
} ;
172
181
182
+ haste . prototype . configureButtons = function ( ) {
183
+ var _this = this ;
184
+ this . buttons = [
185
+ {
186
+ $where : $ ( '#key .box2 .save' ) ,
187
+ label : 'Save' ,
188
+ shortcutDescription : 'control + s' ,
189
+ shortcut : function ( evt ) {
190
+ return evt . ctrlKey && ( evt . keyCode === 76 || evt . keyCode === 83 ) ;
191
+ } ,
192
+ action : function ( ) {
193
+ if ( _this . $textarea . val ( ) . replace ( / ^ \s + | \s + $ / g, '' ) !== '' ) {
194
+ _this . lockDocument ( ) ;
195
+ }
196
+ }
197
+ } ,
198
+ {
199
+ $where : $ ( '#key .box2 .new' ) ,
200
+ label : 'New' ,
201
+ shortcut : function ( evt ) {
202
+ return evt . ctrlKey && evt . keyCode === 78
203
+ } ,
204
+ shortcutDescription : 'control + n' ,
205
+ action : function ( ) {
206
+ _this . newDocument ( ! _this . doc . key ) ;
207
+ }
208
+ } ,
209
+ {
210
+ $where : $ ( '#key .box2 .duplicate' ) ,
211
+ label : 'Duplicate & Edit' ,
212
+ shortcut : function ( evt ) {
213
+ return _this . doc . locked && evt . ctrlKey && evt . keyCode === 68 ;
214
+ } ,
215
+ shortcutDescription : 'control + d' ,
216
+ action : function ( ) {
217
+ _this . duplicateDocument ( ) ;
218
+ }
219
+ } ,
220
+ {
221
+ $where : $ ( '#key .box2 .twitter' ) ,
222
+ label : 'Twitter' ,
223
+ shortcut : function ( evt ) {
224
+ return _this . options . twitter && _this . doc . locked && evt . ctrlKey && evt . keyCode == 84 ;
225
+ } ,
226
+ shortcutDescription : 'control + t' ,
227
+ action : function ( ) {
228
+ window . open ( 'https://twitter.com/share?url=' + encodeURI ( _this . baseUrl + _this . doc . key ) ) ;
229
+ }
230
+ } ,
231
+ {
232
+ $where : $ ( '#key .box2 .link' ) ,
233
+ label : 'Copy URL' ,
234
+ action : function ( ) {
235
+ alert ( 'not yet implemented' ) ;
236
+ }
237
+ }
238
+ ] ;
239
+ for ( var i = 0 ; i < this . buttons . length ; i ++ ) {
240
+ this . configureButton ( this . buttons [ i ] ) ;
241
+ }
242
+ } ;
243
+
244
+ haste . prototype . configureButton = function ( options ) {
245
+ // Handle the click action
246
+ options . $where . click ( function ( evt ) {
247
+ evt . preventDefault ( ) ;
248
+ if ( $ ( this ) . hasClass ( 'enabled' ) ) {
249
+ options . action ( ) ;
250
+ }
251
+ } ) ;
252
+ // Show the label
253
+ options . $where . mouseenter ( function ( evt ) {
254
+ $ ( '#key .box3 .label' ) . text ( options . label ) ;
255
+ $ ( '#key .box3 .shortcut' ) . text ( options . shortcutDescription || '' ) ;
256
+ $ ( '#key .box3' ) . show ( ) ;
257
+ } ) ;
258
+ // Hide the label
259
+ options . $where . mouseleave ( function ( evt ) {
260
+ $ ( '#key .box3' ) . hide ( ) ;
261
+ } ) ;
262
+ } ;
263
+
173
264
// Configure keyboard shortcuts for the textarea
174
265
haste . prototype . configureShortcuts = function ( ) {
175
266
var _this = this ;
176
267
$ ( document . body ) . keydown ( function ( evt ) {
177
- // ^L or ^S for lock
178
- if ( evt . ctrlKey && ( evt . keyCode === 76 || evt . keyCode === 83 ) ) {
179
- if ( _this . $textarea . val ( ) . replace ( / ^ \s + | \s + $ / g, '' ) !== '' ) {
268
+ var button ;
269
+ for ( var i = 0 ; i < _this . buttons . length ; i ++ ) {
270
+ button = _this . buttons [ i ] ;
271
+ if ( button . shortcut && button . shortcut ( evt ) ) {
180
272
evt . preventDefault ( ) ;
181
- _this . lockDocument ( ) ;
273
+ button . action ( ) ;
274
+ return ;
182
275
}
183
276
}
184
- // ^N for new document
185
- else if ( evt . ctrlKey && evt . keyCode === 78 ) {
186
- evt . preventDefault ( ) ;
187
- _this . newDocument ( ! _this . doc . key ) ;
188
- }
189
- // ^D for duplicate - only when locked
190
- else if ( _this . doc . locked && evt . ctrlKey && evt . keyCode === 68 ) {
191
- evt . preventDefault ( ) ;
192
- _this . duplicateDocument ( ) ;
193
- }
194
- // ^T for redirecting to twitter
195
- else if ( _this . options . twitter && _this . doc . locked && evt . ctrlKey && evt . keyCode == 84 ) {
196
- evt . preventDefault ( ) ;
197
- window . open ( 'https://twitter.com/share?url=' + encodeURI ( _this . baseUrl + _this . doc . key ) ) ;
198
- }
199
277
} ) ;
200
278
} ;
201
279
0 commit comments