11/* Controllers */
2+ var angular = window . angular
23
3- function SassCtrl ( $scope , $http , apSass , $timeout ) {
4+ function SassCtrl ( $scope , $http , apSass , $timeout , $sce , $q ) {
45 var scopeVar = {
56 variables : { } ,
67 fonts : { } ,
7- autoapplysass : false ,
8+ templates : undefined ,
9+ autoapplysass : true ,
810 isViewLoading : false ,
9- minified : false ,
11+ show : 'variables' ,
1012 editorOptions : {
11- lineWrapping : true ,
13+ lineWrapping : false ,
1214 lineNumbers : true ,
15+ firstLineNumber : 1 ,
1316 mode : 'text/html' ,
17+ tabSize : 2 ,
1418 onKeyEvent : function ( e , s ) {
1519 if ( s . type === 'keyup' ) {
1620 window . CodeMirror . showHint ( e )
1721 }
1822 } ,
1923 extraKeys : { 'Ctrl-Space' : 'autocomplete' }
2024 } ,
21- myHTML :
22- 'I am an <code>HTML</code>string with ' + '<a href="#">links!</a> and other <em>stuff</em>' ,
23- customHtml : `<div class="bd-example" data-example-id="">
24- <div class="p-4 mb-2 bg-primary text-white">.bg-primary</div>
25- <div class="p-4 mb-2 bg-secondary text-white">.bg-secondary</div>
26- <div class="p-4 mb-2 bg-success text-white">.bg-success</div>
27- <div class="p-4 mb-2 bg-danger text-white">.bg-danger</div>
28- <div class="p-4 mb-2 bg-warning text-white">.bg-warning</div>
29- <div class="p-4 mb-2 bg-info text-white">.bg-info</div>
30- <div class="p-4 mb-2 bg-light text-gray-dark">.bg-light</div>
31- <div class="p-4 mb-2 bg-dark text-white">.bg-dark</div>
32- <div class="p-4 mb-2 bg-white text-gray-dark">.bg-white</div>
33- <div class="p-4 mb-2 swatch-100">.swatch-100</div>
34- <div class="p-4 mb-2 swatch-200">.swatch-200</div>
35- <div class="p-4 mb-2 swatch-300">.swatch-300</div>
36- <div class="p-4 mb-2 swatch-400">.swatch-400</div>
37- <div class="p-4 mb-2 swatch-500">.swatch-500</div>
38- <div class="p-4 mb-2 swatch-600">.swatch-600</div>
39- <div class="p-4 mb-2 swatch-700">.swatch-700</div>
40- <div class="p-4 mb-2 swatch-800">.swatch-800</div>
41- <div class="p-4 mb-2 swatch-900">.swatch-900</div>
42- </div>
43- `
25+ template : {
26+ blobUrl : undefined ,
27+ html : undefined ,
28+ css : undefined
29+ }
4430 }
4531
4632 var scopeFunction = {
@@ -54,10 +40,14 @@ function SassCtrl($scope, $http, apSass, $timeout) {
5440 saveCSS : saveCSS ,
5541 resetSassVariables : resetSassVariables ,
5642 saveSassVariables : saveSassVariables ,
43+ initTemplatesVariables : initTemplatesVariables ,
5744 resetBaseVariable : resetBaseVariable ,
5845 codemirrorLoaded : codemirrorLoaded ,
5946 format : format ,
60- toggle : toggle
47+ toggle : toggle ,
48+ goTo : goTo ,
49+ chooseTemplate : chooseTemplate ,
50+ generatePreviewHtml : generatePreviewHtml
6151 }
6252
6353 $scope . $on ( '$routeChangeStart' , function ( ) {
@@ -76,48 +66,88 @@ function SassCtrl($scope, $http, apSass, $timeout) {
7666 window . CodeMirror . showHint ( cm , window . CodeMirror . hint . html )
7767 }
7868
69+ function goTo ( route ) {
70+ middleware ( route )
71+ }
72+
73+ function middleware ( route ) {
74+ if ( route === 'show' ) {
75+ generatePreviewHtml ( )
76+ }
77+ $scope . show = route || 'show'
78+ }
79+
7980 function toggle ( group ) {
8081 group . hidden = ! group . hidden
8182 }
8283
83- function codemirrorLoaded ( editor ) {
84+ function codemirrorLoaded ( selector ) {
85+ var el = document . querySelector ( '.code-mirror[data-template="' + selector + '"]' )
86+ var editor = window . CodeMirror ( el , $scope . editorOptions )
8487 $scope . editor = editor
85- // var modification = 0
8688 var doc = editor . getDoc ( )
8789 editor . focus ( )
88- editor . setValue ( $scope . x )
89- editor . setOption ( $scope . editorOptions )
90+ editor . setValue ( $scope . template [ selector ] )
9091 doc . markClean ( )
9192
93+ window . doc = doc
94+ window . editor = editor
95+
9296 editor . on ( 'change' , function ( ) {
9397 var html = editor . getValue ( )
94- var code = document . querySelector ( 'div[data-example-id="editor"]' )
95- code . innerHTML = html
98+ $scope . customHtml = html
99+ } )
100+ }
101+
102+ function chooseTemplate ( template ) {
103+ $http ( {
104+ method : 'GET' ,
105+ url : 'preview/' + template . slug + '.html'
106+ } ) . then ( function ( templateHtml ) {
107+ $scope . template . html = templateHtml
108+ $http ( {
109+ method : 'GET' ,
110+ url : 'preview/' + template . slug + '.css'
111+ } ) . then ( function ( templateCss ) {
112+ $scope . template . css = templateCss . data
113+ $scope . show = 'variables'
114+ applySass ( )
115+ } )
96116 } )
97117 }
98118
99119 function format ( ) {
100120 $scope . editor . setValue ( window . html_beautify ( $scope . editor . getValue ( ) ) )
101121 }
102122
123+ function initTemplatesVariables ( ) {
124+ $http ( {
125+ method : 'GET' ,
126+ url : 'data/template-html.json'
127+ } ) . then ( function ( response ) {
128+ $scope . templates = response . data
129+ } )
130+ }
131+
103132 function initSassVariables ( ) {
104133 $http ( {
105134 method : 'GET' ,
106- url : 'scss/ variables.json'
135+ url : 'data/scss- variables.json'
107136 } ) . then ( function ( response ) {
108137 $scope . variables = response . data . map ( function ( d ) {
109138 d . hidden = true
110139 return d
111140 } )
141+
112142 $scope . applySass ( false )
113143
114- var variables_keys = apSass . getKeys ( $scope )
115- var icons_keys = apSass . getUrls ( )
116- var fonts_keys = apSass . getFonts ( )
144+ var variablesKeys = apSass . getKeys ( $scope )
145+ var iconsKeys = apSass . getUrls ( )
146+ var fontsKeys = apSass . getFonts ( )
117147
118148 $timeout ( function ( ) {
119149 var $colorpicker = $ ( '.colorpicker' )
120- $colorpicker . colorpicker ( ) . on ( 'changeColor ' , function ( ev ) {
150+ $colorpicker . colorpicker ( ) . on ( 'cha ngeColor ' , function ( ev ) {
121151 var scope = angular . element ( this ) . scope ( )
122152 scope . variable . value = ev . color . toHex ( )
123153
@@ -132,21 +162,23 @@ function SassCtrl($scope, $http, apSass, $timeout) {
132162 if ( this . dataset . color !== color . value ) {
133163 }
134164 } )
165+
135166 $ ( '.sassVariable' ) . each ( function ( index ) {
167+ var src
136168 var scope = angular . element ( this ) . scope ( )
137169 switch ( scope . variable . type ) {
138170 case 'icons' :
139- var src = icons_keys
171+ src = iconsKeys
140172 break
141173 case 'font' :
142- var src = fonts_keys
174+ src = fontsKeys
143175 break
144176 case 'boolean' :
145- var src = [ 'true' , 'false' ]
177+ src = [ 'true' , 'false' ]
146178 break
147179 case 'color' :
148180 default :
149- var src = variables_keys
181+ src = variablesKeys
150182 }
151183 $ ( this ) . typeahead ( {
152184 source : src ,
@@ -162,12 +194,38 @@ function SassCtrl($scope, $http, apSass, $timeout) {
162194
163195 function autoApplySass ( event ) {
164196 if ( $scope . autoapplysass ) {
165- apSass . applySass ( $scope )
197+ applySass ( event )
166198 }
167199 }
168200
169- function applySass ( applyAll ) {
170- apSass . applySass ( $scope )
201+ function generatePreviewHtml ( css , html ) {
202+ var previewCss = css || $scope . customCss
203+ var previewHtml = html || $scope . customHtml
204+ var generateHtml =
205+ '<!doctype html><html lang="en"><head>' +
206+ previewCss +
207+ '</head><body><div class="preview">' +
208+ previewHtml +
209+ '</div></body></html>'
210+ var blob = new Blob ( [ generateHtml ] , {
211+ type : 'text/html'
212+ } )
213+
214+ $timeout ( function ( ) {
215+ $scope . blobUrl = $sce . trustAsResourceUrl ( URL . createObjectURL ( blob ) )
216+ } , 0 )
217+ }
218+
219+ function applySass ( ) {
220+ apSass
221+ . applySass ( $scope )
222+ . then ( function ( result ) {
223+ $scope . customCss = '<style>' + result + '</style>'
224+ generatePreviewHtml ( )
225+ } )
226+ . catch ( function ( error ) {
227+ console . error ( error )
228+ } )
171229 }
172230
173231 function resetBaseVariable ( variable ) {
@@ -218,8 +276,7 @@ function SassCtrl($scope, $http, apSass, $timeout) {
218276 }
219277
220278 initSassVariables ( )
279+ initTemplatesVariables ( )
221280}
222- SassCtrl . $inject = [ '$scope' , '$http' , 'apSass' , '$timeout' ]
223281
224- function PageCtrl ( $scope , $http , apSass ) { }
225- PageCtrl . $inject = [ '$scope' , '$http' , 'apSass' ]
282+ SassCtrl . $inject = [ '$scope' , '$http' , 'apSass' , '$timeout' , '$sce' , '$q' ]
0 commit comments