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