Mercurial > zeropaste
annotate app/assets/javascripts/init.tabs.js @ 208:953d284ec195
Revamp the routing
author | Edho Arief <edho@myconan.net> |
---|---|
date | Sat, 11 May 2013 22:39:06 +0900 |
parents | c054f01477c4 |
children |
rev | line source |
---|---|
59
1833de28583a
Turns out the init file is required.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
1 $(document).ready(function() { |
129
0785824a20b9
Return immediately if not in pastes#show.
Edho Arief <edho@myconan.net>
parents:
128
diff
changeset
|
2 if ($('#paste-show').length !== 1) { |
0785824a20b9
Return immediately if not in pastes#show.
Edho Arief <edho@myconan.net>
parents:
128
diff
changeset
|
3 return; |
0785824a20b9
Return immediately if not in pastes#show.
Edho Arief <edho@myconan.net>
parents:
128
diff
changeset
|
4 } |
136
b327d80c5ff9
Fixed indentation and spaces.
Edho Arief <edho@myconan.net>
parents:
135
diff
changeset
|
5 var update_state = function() { |
b327d80c5ff9
Fixed indentation and spaces.
Edho Arief <edho@myconan.net>
parents:
135
diff
changeset
|
6 var paste = $('#paste'), |
b327d80c5ff9
Fixed indentation and spaces.
Edho Arief <edho@myconan.net>
parents:
135
diff
changeset
|
7 mode = paste.data('mode'), |
b327d80c5ff9
Fixed indentation and spaces.
Edho Arief <edho@myconan.net>
parents:
135
diff
changeset
|
8 width = paste.data('width'), |
204
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
9 short_mode = function(mode) { |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
10 if (mode == 'highlight') { |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
11 return 'hl' |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
12 } else if (mode =='markdown') { |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
13 return 'md' |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
14 } else { |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
15 return 'plain' |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
16 } |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
17 }, |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
18 qstring = '#!' + short_mode(mode) + '.' + width; |
137 | 19 if (width == 'auto') { |
144
b56ad3caed6d
Remove hash on `#!plain.auto`.
Edho Arief <edho@myconan.net>
parents:
141
diff
changeset
|
20 if (mode == 'plain') { |
b56ad3caed6d
Remove hash on `#!plain.auto`.
Edho Arief <edho@myconan.net>
parents:
141
diff
changeset
|
21 qstring = ' '; |
b56ad3caed6d
Remove hash on `#!plain.auto`.
Edho Arief <edho@myconan.net>
parents:
141
diff
changeset
|
22 } else { |
204
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
23 qstring = '#!' + short_mode(mode); |
134
3c777dbd6b11
Moved function declarations to beginning.
Edho Arief <edho@myconan.net>
parents:
133
diff
changeset
|
24 } |
137 | 25 } |
141
2104f0c477c0
Use replaceState afterall for browsers supporting it.
Edho Arief <edho@myconan.net>
parents:
140
diff
changeset
|
26 if (history.replaceState) { |
2104f0c477c0
Use replaceState afterall for browsers supporting it.
Edho Arief <edho@myconan.net>
parents:
140
diff
changeset
|
27 history.replaceState(null, '', qstring); |
2104f0c477c0
Use replaceState afterall for browsers supporting it.
Edho Arief <edho@myconan.net>
parents:
140
diff
changeset
|
28 } else { |
2104f0c477c0
Use replaceState afterall for browsers supporting it.
Edho Arief <edho@myconan.net>
parents:
140
diff
changeset
|
29 window.location = qstring; |
2104f0c477c0
Use replaceState afterall for browsers supporting it.
Edho Arief <edho@myconan.net>
parents:
140
diff
changeset
|
30 } |
140
13ada93ad6a2
Return false on update state.
Edho Arief <edho@myconan.net>
parents:
137
diff
changeset
|
31 return false; |
137 | 32 }, |
33 paste_resize = function(size) { | |
34 var paste = $('#paste'), | |
35 title = $('#paste-resize a.btn'); | |
36 title.html(title.html().replace(/(auto|\d+px)/, size)); | |
37 if (size == 'auto') { | |
38 paste.css('width', ''); | |
39 paste.data('width', size); | |
40 } else { | |
41 paste.css('width', size); | |
42 paste.data('width', size); | |
43 } | |
44 update_state(); | |
45 }, | |
46 paste_resize_menu_handler = function() { | |
47 paste_resize($(this).data('size')); | |
48 $('.open').removeClass('open'); | |
49 return false; | |
50 }, | |
51 paste_mode_state_handler = function() { | |
52 $('#paste').data('mode', $(this).prop('href').split('#')[1]); | |
53 update_state(); | |
54 }, | |
55 init_state = function() { | |
56 mode = window.location.hash.substr(2).split('.'); | |
204
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
57 if (mode[0] == 'hl' || mode[0] == 'highlight') { $('#paste-mode a:eq(1)').click(); } |
c054f01477c4
Use shorter name for the hash magic thing
Edho Arief <edho@myconan.net>
parents:
146
diff
changeset
|
58 else if (mode[0] == 'md' || mode[0] == 'markdown') { $('#paste-mode a:eq(2)').click(); }; |
137 | 59 if (mode[1] && mode[1].match(/^\d+px$/)) { paste_resize(mode[1]); } |
60 }, | |
61 init_tabs_base = function() { | |
62 var raw = $('#plain pre').text(), | |
63 markdown = $('#markdown div'); | |
64 markdown.html(marked(raw)); | |
65 $('#highlight pre').text(raw); | |
135 | 66 |
137 | 67 markdown.find('pre').addClass('prettyprint'); |
68 markdown.find('a').prop('rel', 'nofollow'); | |
69 prettyPrint(); | |
70 }, | |
71 init_tabs_menu = function() { | |
72 $('#paste-mode a').click(paste_mode_state_handler); | |
73 $('#paste-resize ul a').click(paste_resize_menu_handler); | |
74 }; | |
104 | 75 |
135 | 76 init_tabs_base(); |
77 init_tabs_menu(); | |
78 init_state(); | |
79 update_state(); | |
59
1833de28583a
Turns out the init file is required.
Edho Arief <edho@myconan.net>
parents:
diff
changeset
|
80 }); |