1- import CodeMirror from "codemirror/lib/codemirror.js" ;
2- import "codemirror/lib/codemirror.css" ;
3- import "codemirror/theme/mdn-like.css" ;
4-
5- import Clipboard from "clipboard" ;
1+ import * as CodeMirror from 'codemirror' ;
2+ import 'codemirror/lib/codemirror.css' ;
3+ import 'codemirror/theme/mdn-like.css' ;
4+ import 'codemirror/mode/javascript/javascript.js' ;
5+ import 'codemirror/mode/xml/xml.js' ;
6+ import 'codemirror/mode/htmlmixed/htmlmixed.js' ;
7+
8+ import * as ClipboardJS from 'clipboard' ;
9+
10+ class knockoutType {
11+ constructor ( baseType : string ) {
12+ this . baseType = baseType ;
13+
14+ this . observable = `${ baseType } observable` ;
15+ this . observableArray = `${ baseType } observableArray` ;
16+ this . computed = `${ baseType } computed` ;
17+ }
618
7- import js from 'codemirror/mode/javascript/javascript.js' ;
8- import xml from 'codemirror/mode/xml/xml.js' ;
9- import htmlmixed from 'codemirror/mode/htmlmixed/htmlmixed.js' ;
19+ baseType : string ;
20+ observable : string ;
21+ observableArray : string ;
22+ computed : string ;
23+ }
1024
11- function knockoutType ( baseType ) {
12- this . baseType = baseType ;
25+ class Generator {
26+ rand = ( ) => {
27+ return Math . floor ( Math . random ( ) * 26 ) + Date . now ( ) ;
28+ }
1329
14- // knockout types
15- this . observable = ` ${ baseType } observable` ;
16- this . observableArray = ` ${ baseType } observableArray` ;
17- this . computed = ` ${ baseType } computed` ;
30+ getId = ( ) => {
31+ return 'uniqueID_' + ( this . rand ( ) + 1 ) ;
32+ }
33+ }
1834
19- return this ;
35+ declare global {
36+ interface Window {
37+ idGen : Generator ;
38+ paramAsText : ( property : any ) => string ;
39+ codeEditorFunction : ( element : any , valueAccessor : any , allBindings : any , viewModel : any , bindingContext : any ) => void ;
40+ }
2041}
2142
22- window . paramAsText = function ( property ) {
43+ window . idGen = new Generator ( ) ;
44+
45+ window . paramAsText = function ( property : any ) {
2346 if ( property === undefined ) {
2447 return "undefined" ;
2548 }
@@ -31,49 +54,45 @@ window.paramAsText = function(property) {
3154 return JSON . stringify ( property ) ;
3255}
3356
34-
35- function Generator ( ) { } ;
36- Generator . prototype . rand = Math . floor ( Math . random ( ) * 26 ) + Date . now ( ) ;
37- Generator . prototype . getId = function ( ) { return 'uniqueID_' + this . rand ++ ; } ;
38- window . idGen = new Generator ( ) ;
39-
40-
41-
42-
4357// Create uniqueID and run function
4458// pass a function that accepts the params (element, allBindings, viewModel, bindingContext)
4559ko . bindingHandlers . uniqueIdFunction = {
46- init : function ( element , valueAccessor , allBindings , viewModel , bindingContext ) {
60+ init : ( element , valueAccessor , allBindings , viewModel , bindingContext ) => {
4761 // bind a unique ID
48- let uniqueID = idGen . getId ( ) ;
62+ let uniqueID = window . idGen . getId ( ) ;
4963 element . setAttribute ( "id" , uniqueID ) ;
5064
5165 ko . unwrap ( valueAccessor ) ( ) . fn ( element , valueAccessor , allBindings , viewModel , bindingContext ) ;
5266 }
5367} ;
5468
5569ko . bindingHandlers . addUniqueID = {
56- init : function ( element , valueAccessor , allBindingsAccessor , viewModel , bindingContext ) {
57- let uniqueID = idGen . getId ( ) ;
70+ init : ( element , valueAccessor , allBindingsAccessor , viewModel , bindingContext ) => {
71+ let uniqueID = window . idGen . getId ( ) ;
5872 element . setAttribute ( "id" , uniqueID ) ;
5973 valueAccessor ( ) ( uniqueID ) ;
6074 }
6175} ;
6276
6377ko . bindingHandlers . clipboard = {
64- init : function ( el , valueAccessor , allBindings , data , context ) {
65- new Clipboard ( el , {
66- text : function ( trigger ) {
67- return ko . unwrap ( valueAccessor ( ) ) ;
68- }
69- } ) . on ( 'success' , function ( e ) {
70- $ ( e . trigger ) . addClass ( "btn-success" ) . find ( "span" )
71- . removeClass ( "glyphicon-copy" )
72- . addClass ( "glyphicon-ok" ) ;
73- setTimeout ( function ( ) {
74- $ ( e . trigger ) . removeClass ( "btn-success" ) . find ( "span" )
75- . addClass ( "glyphicon-copy" )
76- . removeClass ( "glyphicon-ok" ) ;
78+ init : ( el , valueAccessor , allBindings , data , context ) => {
79+ new ClipboardJS ( el , {
80+ text : ( trigger ) => ko . unwrap ( valueAccessor ( ) )
81+ } ) . on ( 'success' , ( e ) => {
82+ const trigger = e . trigger ;
83+ trigger . classList . add ( "btn-success" ) ;
84+
85+ // show copied icon
86+ const copyIcon = trigger . querySelector ( "span" ) ;
87+ copyIcon . classList . remove ( "glyphicon-copy" ) ;
88+ copyIcon . classList . add ( "glyphicon-ok" ) ;
89+
90+ // revert copy button
91+ setTimeout ( ( ) => {
92+ trigger . classList . remove ( "btn-success" ) ;
93+
94+ copyIcon . classList . add ( "glyphicon-copy" ) ;
95+ copyIcon . classList . remove ( "glyphicon-ok" ) ;
7796 } , 750 ) ;
7897 } ) ;
7998 }
@@ -99,7 +118,7 @@ ko.bindingHandlers.innerHtml = {
99118 // re-add
100119 element . innerHTML = `<div data-bind='component: { name: componentName, params: componentParamObject }'>${ valueUnwrapped . value ( ) } </div>` ;
101120 // apply the binding again
102- setTimeout ( function ( ) {
121+ setTimeout ( ( ) => {
103122 try {
104123 ko . applyBindings ( viewModel , element . firstChild ) ;
105124 }
@@ -132,7 +151,6 @@ window.codeEditorFunction = function(element, valueAccessor, allBindings, viewMo
132151 readOnly : bindingParams . readOnly ,
133152 lineWrapping : true ,
134153 indentWithTabs : true ,
135- matchBrackets : true ,
136154 theme : "mdn-like"
137155 } ) ;
138156
@@ -145,7 +163,7 @@ window.codeEditorFunction = function(element, valueAccessor, allBindings, viewMo
145163} ;
146164
147165
148- import "./knockout-documentation-search.js " ;
149- import "./knockout-component-documentor.js " ;
150- import "./random-sample-component.js " ;
151- import "./jsdoc-sample-component.js " ;
166+ import "./knockout-documentation-search.ts " ;
167+ import "./knockout-component-documentor.ts " ;
168+ import "./random-sample-component.ts " ;
169+ import "./jsdoc-sample-component.ts " ;
0 commit comments