@@ -69,21 +69,27 @@ CMS.AWSUtils.create_url_builder = function(url_root) {
6969 * content.
7070 */
7171CMS . AWSUtils . prototype . display_subpage = function ( elements ) {
72+ var content = $ ( "#subpage_content" ) ;
73+ content . empty ( ) ;
7274 // TODO: update jQuery to allow appending of arrays of elements.
7375 for ( var i = 0 ; i < elements . length ; ++ i ) {
74- elements [ i ] . appendTo ( $ ( "#subpage_content" ) ) ;
76+ elements [ i ] . appendTo ( content ) ;
7577 }
76- $ ( "#subpage" ) . show ( ) ;
78+ document . getElementById ( 'subpage_popup' ) . show ( ) ;
7779} ;
7880
81+ // set up some event listeners for the subpage
82+ window . addEventListener ( 'DOMContentLoaded' , ( ) => {
83+ $ ( '#subpage_close' ) . on ( 'click' , ( ) => {
84+ document . getElementById ( 'subpage_popup' ) . close ( ) ;
85+ } ) ;
86+ } ) ;
7987
80- /**
81- * Hides a subpage previously displayed.
82- */
83- CMS . AWSUtils . prototype . hide_subpage = function ( ) {
84- $ ( "#subpage" ) . hide ( ) ;
85- $ ( "#subpage_content" ) . empty ( ) ;
86- } ;
88+ document . addEventListener ( 'keydown' , function ( event ) {
89+ if ( event . key === "Escape" ) {
90+ document . getElementById ( 'subpage_popup' ) . close ( ) ;
91+ }
92+ } ) ;
8793
8894
8995/**
@@ -108,20 +114,42 @@ CMS.AWSUtils.prototype.file_received = function(response, error) {
108114 this . display_subpage ( elements ) ;
109115 return ;
110116 }
111- var pre_class = "" ;
112- // TODO: add more languages.
113- if ( file_name . match ( / .c ( | p p ) $ / i) ) {
114- pre_class = "brush: cpp" ;
115- } else if ( file_name . match ( / .p a s $ / i) ) {
116- pre_class = "brush: delphi" ;
117+ // TODO: update if adding a new language to cms
118+ // (need to also update the prism bundle then)
119+ var extension_to_lang = {
120+ 'cs' : 'csharp' ,
121+ 'cpp' : 'cpp' ,
122+ 'c' : 'c' ,
123+ 'h' : 'c' ,
124+ 'go' : 'go' ,
125+ 'hs' : 'haskell' ,
126+ 'java' : 'java' ,
127+ 'js' : 'javascript' ,
128+ 'php' : 'php' ,
129+ 'py' : 'python' ,
130+ 'rs' : 'rust' ,
117131 }
132+ var file_ext = file_name . split ( '.' ) . pop ( ) ;
133+ var lang_name = extension_to_lang [ file_ext ] || file_ext ;
134+
118135 elements . push ( $ ( '<h1>' ) . text ( file_name ) ) ;
119136 elements . push ( $ ( '<a>' ) . text ( "Download" ) . prop ( "href" , url ) ) ;
120- elements . push ( $ ( '<pre>' ) . text ( response ) . prop ( "id" , "source_container" )
121- . prop ( "class" , pre_class ) ) ;
122-
137+ elements . push ( $ ( '<span>' ) . text ( " - " ) )
138+ elements . push ( $ ( '<a>' ) . text ( "copy" ) . prop ( 'href' , '#' ) . on ( 'click' , ( event ) => {
139+ var range = document . createRange ( ) ;
140+ var code_area = $ ( '#subpage_content code' ) [ 0 ] ;
141+ range . setStartBefore ( code_area ) ;
142+ range . setEndAfter ( code_area ) ;
143+ window . getSelection ( ) . removeAllRanges ( ) ;
144+ window . getSelection ( ) . addRange ( range ) ;
145+ // execCommand is deprecated, but this seems much less annoying than the new clipboard api
146+ document . execCommand ( 'copy' ) ;
147+ event . preventDefault ( ) ; // prevent the <a> from navigating
148+ } ) ) ;
149+ var codearea = $ ( '<code>' ) . text ( response ) . addClass ( 'line-numbers' ) . addClass ( 'language-' + lang_name ) ;
150+ elements . push ( $ ( '<pre>' ) . append ( codearea ) ) ;
123151 this . display_subpage ( elements ) ;
124- SyntaxHighlighter . highlight ( ) ;
152+ Prism . highlightAllUnder ( document . getElementById ( 'subpage_content' ) ) ;
125153 }
126154} ;
127155
0 commit comments