1+ /*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
2+ /*global define, document, console, brackets, $, Mustache */
3+
4+ define ( function ( require , exports , module ) {
5+ "use strict" ;
6+
7+ var widgetContext = null ;
8+
9+ var PreferencesManager = brackets . getModule ( 'preferences/PreferencesManager' ) ,
10+ AppInit = brackets . getModule ( "utils/AppInit" ) ,
11+ FileSystem = brackets . getModule ( "filesystem/FileSystem" ) ,
12+ FileUtils = brackets . getModule ( "file/FileUtils" ) ;
13+
14+ // Extension config
15+ var _ExtensionID = "swmitra.html-designer" ;
16+ var _settingsPath = null ;
17+
18+ var _preferenceCache = { } ;
19+
20+ var _prefs = PreferencesManager . getExtensionPrefs ( _ExtensionID ) ;
21+ _prefs . definePreference ( 'settings-file-path' , 'string' , "" ) ;
22+
23+ var currentApplication = null ;
24+ var _entryTemplate = '<li class="list-group-item" data-path="{{path}}" title="{{path}}"><a href="#">{{value}}</a><div>Default</div></li>' ;
25+
26+ $ ( document ) . on ( "application.context" , "#html-design-editor" , function ( event , applicationKey ) {
27+ currentApplication = applicationKey ;
28+ $ ( "#html-design-editor" ) . trigger ( "default-stylesheet-path" , [ _preferenceCache [ currentApplication ] ] ) ;
29+ } ) ;
30+
31+ function _createStylesheetOptions ( styleSheets ) {
32+ $ ( "#default-stylesheet-select-list" ) . html ( "" ) ;
33+ var prefferedStylePath = _preferenceCache [ currentApplication ] ;
34+ var newOption ;
35+
36+ var sheetCount , styleSheet ;
37+ for ( sheetCount = 0 ; sheetCount < styleSheets . length ; sheetCount ++ ) {
38+ styleSheet = styleSheets [ sheetCount ] ;
39+ newOption = _entryTemplate . split ( "{{path}}" ) . join ( styleSheet . href ) ;
40+ newOption = newOption . split ( "{{value}}" ) . join ( FileUtils . getBaseName ( styleSheet . href ) ) ;
41+ newOption = $ ( newOption ) . appendTo ( "#default-stylesheet-select-list" ) ;
42+ if ( styleSheet . href === prefferedStylePath ) {
43+ newOption . addClass ( "active" ) ;
44+ }
45+ }
46+
47+ if ( $ ( "#default-stylesheet-select-list" ) . children ( ) . length === 0 ) {
48+ $ ( "#default-stylesheet-select-list" ) . html ( "No Stylesheets Loaded!" ) ;
49+ }
50+ }
51+
52+ $ ( document ) . on ( "click" , "#reset-default-stylesheet" , function ( event ) {
53+ $ ( "#default-stylesheet-select-list>li" ) . removeClass ( "active" ) ;
54+ delete _preferenceCache [ currentApplication ] ;
55+ $ ( "#html-design-editor" ) . trigger ( "default-stylesheet-path" , [ _preferenceCache [ currentApplication ] ] ) ;
56+ FileSystem . getFileForPath ( _settingsPath + "/swmitra.html-designer-settings/DefaultStyleSheetPreferences.json" ) . write ( JSON . stringify ( _preferenceCache ) , { blind : true } ) ;
57+ } ) ;
58+
59+ $ ( document ) . on ( "click" , "#default-stylesheet-select-list>li" , function ( event ) {
60+ $ ( "#default-stylesheet-select-list>li" ) . removeClass ( "active" ) ;
61+ $ ( this ) . addClass ( "active" ) ;
62+ _preferenceCache [ currentApplication ] = $ ( this ) . data ( 'path' ) ;
63+ $ ( "#html-design-editor" ) . trigger ( "default-stylesheet-path" , [ _preferenceCache [ currentApplication ] ] ) ;
64+ FileSystem . getFileForPath ( _settingsPath + "/swmitra.html-designer-settings/DefaultStyleSheetPreferences.json" ) . write ( JSON . stringify ( _preferenceCache ) , { blind : true } ) ;
65+ } ) ;
66+
67+ $ ( document ) . on ( "stylesheets-in-dom" , "#html-design-editor" , function ( event , styleSheets ) {
68+ _createStylesheetOptions ( styleSheets ) ;
69+ } ) ;
70+
71+ $ ( document ) . on ( "click" , "#design-settings-anchor" , function ( event ) {
72+ if ( _settingsPath ) {
73+ $ ( "#design-modal-backdrop" ) . show ( ) ;
74+ $ ( "#designer-settings-container" ) . show ( ) ;
75+ }
76+ } ) ;
77+
78+ $ ( document ) . on ( "click" , "#designer-settings-close" , function ( event ) {
79+ $ ( "#design-modal-backdrop" ) . hide ( ) ;
80+ $ ( "#designer-settings-container" ) . hide ( ) ;
81+ } ) ;
82+
83+ AppInit . appReady ( function ( ) {
84+ _settingsPath = _prefs . get ( 'settings-file-path' ) ;
85+ if ( _settingsPath ) {
86+ var dir = FileSystem . getDirectoryForPath ( _settingsPath + "/swmitra.html-designer-settings" ) ;
87+ dir . exists ( function ( err , flag ) {
88+ if ( flag ) {
89+ var file = FileSystem . getFileForPath ( _settingsPath + "/swmitra.html-designer-settings/DefaultStyleSheetPreferences.json" ) ;
90+ file . exists ( function ( err , flag ) {
91+ if ( flag ) {
92+ file . read ( function ( err , data , stats ) {
93+ if ( data ) {
94+ _preferenceCache = JSON . parse ( data ) ;
95+ }
96+ } ) ;
97+ } else {
98+ file . write ( "{}" , { blind : true } ) ;
99+ }
100+ } ) ;
101+ } else {
102+ dir . create ( function ( ) {
103+ FileSystem . getFileForPath ( _settingsPath + "/swmitra.html-designer-settings/DefaultStyleSheetPreferences.json" ) . write ( "{}" , { blind : true } ) ;
104+ } ) ;
105+ }
106+ } ) ;
107+ }
108+ } ) ;
109+
110+ } ) ;
0 commit comments