-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectworkspace.js
391 lines (332 loc) · 10.9 KB
/
projectworkspace.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* Project Workspace
*
* Extends Projects handling with auto save and restore of documents for each project,
* when changing between projects.
*
* @category WeBuilder Plugin
* @package Project Workspace
* @author Peter Klein <[email protected]>
* @copyright 2018
* @license http://www.freebsd.org/copyright/license.html BSD License
* @version 1.0
*/
/**
* [CLASS/FUNCTION INDEX of SCRIPT]
*
* 73 function ProjectWorkspace()
* 150 function CloseProjectFiles(exitmode)
* 199 function GetFileName(doc)
* 212 function OpenProjectFiles(Sender)
* 254 function Max(x,y)
* 268 function Min(x,y)
* 280 function Nullify(obj)
* 296 function OnCanResize(Sender, newWidth, newHeight, resize)
* 309 function OnBeforeSelectProject(Sender)
* 321 function OnAfterSelectProject(Sender)
* 330 function OnReady()
* 340 function OnExit()
* 353 function OnDisabled()
* 362 function OnEnabled()
* 371 function OnInstalled()
*
* TOTAL FUNCTIONS: 15
* (This index is automatically created/updated by the WeBuilder plugin "DocBlock Comments")
*
*/
/**
* Global vars
* Used for GUI restraints
*/
var minWidth, maxWidth;
/**
* Path to projects.ini file
* "Abusing" the editor's existing projects.ini file to store plugin data.
* That way user can rename projects without causing problems for the plugin.
*
* @var projectsIniFile
*/
var projectsIniFile = RegexReplace(Script.Path, "plugins(?!.*plugins).*", "settings\\projects.ini", false);
/**
* Name of previous project when project is switched
*
* @var ppName
*/
var ppName = "";
/**
* Flag for detecting if exit signal is caused by disabling plugin or exiting editor
*
* @var pluginAction
*/
var pluginAction = false;
/**
* Display GUI Modal Window
*
* @return void
*/
function ProjectWorkspace() {
var oMargin = Round(8 * Script.DpiScale); // Outer margin. for TPanels
var iMargin = Round(8 * Script.DpiScale); // Inner margin. for other VCL components
var bvPadding = Round(6 * Script.DpiScale); //Vertical button padding;
var bhPadding = Round(10 * Script.DpiScale); //Horizontal button padding;
var PSForm = new TForm(WeBuilder);
PSForm.Caption = "Project Workspace";
PSForm.ClientHeight = 300;
PSForm.ClientWidth = 300;
PSForm.Position = poScreenCenter;
PSForm.FormStyle = fsStayOnTop;
PSForm.BorderIcons = biSystemMenu;
PSForm.Color = clBtnFace;
PSForm.Font.Color = clWindowText;
PSForm.Font.Height = WeBuilder.Font.Height;
PSForm.Font.Name = WeBuilder.Font.Name;
var tHeight = PSForm.Canvas.TextHeight("Jj");
var GroupBox1 = new TGroupBox(PSForm);
GroupBox1.Parent = PSForm;
GroupBox1.Anchors = akLeft + akTop + akRight + akBottom;
GroupBox1.Caption = "Workspace enabled for Projects";
GroupBox1.Left = oMargin;
GroupBox1.Top = tHeight/2;
GroupBox1.Width = PSForm.ClientWidth - oMargin - oMargin;
GroupBox1.Height = PSForm.ClientHeight - GroupBox1.Top - oMargin;
var CheckListBox1 = new TCheckListBox(PSForm);
CheckListBox1.Parent = GroupBox1;
CheckListBox1.Anchors = akLeft + akTop + akRight + akBottom;
CheckListBox1.ParentFont = false;
CheckListBox1.Font.Name = WeBuilder.Font.Name;
CheckListBox1.Font.Height = WeBuilder.Font.Height-1; // Increase font height (uses negative value)
CheckListBox1.Left = iMargin;
CheckListBox1.Top = tHeight/2 + iMargin;
CheckListBox1.Width = GroupBox1.Width - iMargin - iMargin;
CheckListBox1.Height = GroupBox1.Height - CheckListBox1.Top - iMargin;
CheckListBox1.TabOrder = 0;
// Fill CheckListBox1 with Project names and set checkbox to projects WorkspaceMode setting
var projectsIni = new TIniFile(projectsIniFile);
projectsIni.ReadSections(CheckListBox1.Items);
for (var i=0;i<CheckListBox1.Items.Count;i++) {
CheckListBox1.Checked[i] = projectsIni.ReadBool(CheckListBox1.Items[i], "WorkspaceMode", StrToBool(Script.ReadSetting("WorkspaceMode", "0")));
}
delete projectsIni;
CheckListBox1.ItemIndex = CheckListBox1.Items.IndexOf(Script.ProjectSettings.SelectedProjectName);
minWidth = GroupBox1.Width + GroupBox1.Left + oMargin + oMargin + iMargin;
minHeight = GroupBox3.Top + ListBox2.Top + iMargin + tHeight*5;
PSForm.OnCanResize = "OnCanResize";
PSForm.ShowModal;
// Write mode settings back to each project
projectsIni = new TIniFile(projectsIniFile);
for (var j=0;j<CheckListBox1.Items.Count;j++) {
projectsIni.WriteBool(CheckListBox1.Items[j], "WorkspaceMode", CheckListBox1.Checked(j));
}
delete projectsIni;
// Cleanup
PSForm = Nullify(PSForm);
}
/**
* Close all open files from previous project
*
* @param boolean exitmode
*
* @return void
*/
function CloseProjectFiles(exitmode) {
if ((ppName == Script.ProjectSettings.SelectedProjectName) && (exitmode == false)) return; // Same project as before
if (ppName =="") return;
var projectsIni = new TIniFile(projectsIniFile);
if (projectsIni.ReadBool(ppName, "WorkspaceMode", StrToBool(Script.ReadSetting("WorkspaceMode", "0"))) == true) {
var OL = new TStringList; // Openfiles list in order
var selectedDocument = Document.FileName;
for (var i=Documents.Count-1; i>=0; i--) {
var doc = Documents.Tab[i];
var fname = GetFileName(doc);
if (doc.Editor.Modified == true) {
doc.Save(fname);
if (fname == "") fname = GetFileName(doc); // Re-get name if new unsaved document
}
if (fname != "") {
OL.Add(fname);
doc.Close(true);
}
}
projectsIni.WriteString(ppName, "WorkspaceOpenFiles", "{" + OL.CommaText + "}");
projectsIni.WriteString(ppName, "WorkspaceSelectedDocument", selectedDocument);
delete OL;
}
delete projectsIni;
// The opening of files can't be done directly, but has to be in a TimeOut function to work
if (!exitMode) Script.Timeout(10, "OpenProjectFiles");
}
/**
* Get document filename (local or FTP)
*
* @param object doc The Document object
*
* @return string
*/
function GetFileName(doc) {
var fname = doc.FileName;
if (fname == "") fname = doc.FtpFileName;
return fname;
}
/**
* Open all files for current project
*
* @param object Sender
*
* @return void
*/
function OpenProjectFiles(Sender) {
var spName = Script.ProjectSettings.SelectedProjectName;
var projectsIni = new TIniFile(projectsIniFile);
if (projectsIni.ReadBool(spName, "WorkspaceMode", StrToBool(Script.ReadSetting("WorkspaceMode", "0"))) == true) {
var curFiles = RegexReplace(projectsIni.ReadString(spName, "WorkspaceOpenFiles", ""), "^{(.*)}$", "$1", false);
if (curFiles != "") {
var OL = new TStringList; // Openfiles list in order
OL.CommaText = curFiles;
var selecteDocument = projectsIni.ReadString(spName, "WorkspaceSelectedDocument", "");
for (var i=OL.Count-1;i >= 0;i--) {
if ((FileExists(OL[i])) || (RegexMatch(OL[i], "^ftp::", true) != "")) {
Documents.OpenDocument(OL[i]); // Open document from list
}
}
if (selecteDocument != "") {
Documents.TabByFileName(selecteDocument).Activate;
}
delete OL;
}
}
delete projectsIni;
}
/**
*
* Returns the largest of two numbers.
*
* @param number x: The 1st value
* @param number y: The 2nd value
* @return number
*
*/
function Max(x,y) {
if (x > y) return x;
return y;
}
/**
*
* Returns the smallest two numbers.
*
* @param number x: The 1st value
* @param number y: The 2nd value
* @return number
*
*/
function Min(x,y) {
if (x < y) return x;
return y;
}
/**
* Remove object and resets it value to null
*
* @param object obj
*
* @return null
*/
function Nullify(obj) {
if (obj != null) delete obj;
return null;
}
/**
* OnCanResize callback function for TForm
* Connstrains the resizing of modal window
*
* @param object Sender TForm object
* @param int NewWidth
* @param int NewHeight
* @param boolean Resize set to false to prevent resizing
*
* @return void
*/
function OnCanResize(Sender, newWidth, newHeight, resize) {
newWidth = max(min(newWidth, 500), minWidth);
newHeight = max(min(newHeight, 500), minHeight);
}
/**
* project_before_select callback function.
* Fired after a new project have been selected, but before old project is unloaded.
*
* @param object Sender
*
* @return void
*/
function OnBeforeSelectProject(Sender) {
ppName = Script.ProjectSettings.SelectedProjectName; // Save name of previous project
}
/**
* project_after_select callback function.
* Fired after a new project have been selected, after old project have been unloaded.
*
* @param object Sender
*
* @return void
*/
function OnAfterSelectProject(Sender) {
CloseProjectFiles(false);
}
/**
* Signal triggered when plugin/editor is loaded
*
* @return void
*/
function OnReady() {
if (pluginAction) pluginAction = false;
else OpenProjectFiles(Document); // Load files for current project at editor start
}
/**
* Signal triggered when exiting plugin/editor
*
* @return void
*/
function OnExit() {
if (pluginAction) pluginAction = false;
else {
ppName = Script.ProjectSettings.SelectedProjectName;
CloseProjectFiles(true);
}
}
/**
* Signal triggered when plugin is disabled through Plugin Manager.
*
* @return void
*/
function OnDisabled() {
pluginAction = true;
}
/**
* Signal triggered when plugin is enabled through Plugin Manager.
*
* @return void
*/
function OnEnabled() {
pluginAction = true;
}
/**
* Signal triggered when plugin is installed through Plugin Manager.
*
* @return void
*/
function OnInstalled() {
Alert("Project Workspace 1.0 by Peter Klein installed sucessfully!");
}
// Signals for plugin setup
Script.ConnectSignal("installed", "OnInstalled");
// Signals to detect change of Project etc.
Script.ConnectSignal("project_after_select", "OnAfterSelectProject");
Script.ConnectSignal("project_before_select", "OnBeforeSelectProject");
Script.ConnectSignal("ready", "OnReady");
Script.ConnectSignal("exit", "OnExit");
Script.ConnectSignal("disabled", "OnDisabled");
Script.ConnectSignal("enabled", "OnEnabled");
// Action for displaying GUI
var bmp = new TBitmap, act;
LoadFileToBitmap(Script.Path + "project-workspace-icon.png", bmp);
act = Script.RegisterAction("", "Project Workspace", "", "ProjectWorkspace");
Actions.SetIcon(act, bmp);
delete bmp;