-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathugui.js
2965 lines (2297 loc) · 103 KB
/
ugui.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//UGUI is a library and framework used to bootstrap NW.js
//projects however it specializes in allowing the quick and
//easy conversion of CLI to GUI.
//## TABLE OF CONTENTS
//
//**A00. [Intro](#a00-intro)**
//**A01**. [UGUI Start](#a01-ugui-start)
//**A02**. [UGUI variables](#a02-ugui-variables)
//
//**B00. [Simplified Commands](#b00-simplified-commands)**
//**B01**. [Run CMD](#b01-run-cmd)
//**B02**. [Run CMD (Advanced)](#b02-run-cmd-advanced-)
//**B03**. [Read a file](#b03-read-a-file)
//**B04**. [Read contents of a folder](#b04-read-contents-of-a-folder)
//**B05**. [Write to file](#b05-write-to-file)
//**B06**. [Create a folder](#b06-create-a-folder)
//**B07**. [Delete a file](#b07-delete-a-file)
//**B08**. [Delete a folder](#b08-delete-a-folder)
//
//**C00. [CLI Command Processing](#c00-cli-command-processing)**
//**C01**. [Clicking Submit](#c01-clicking-submit)
//**C02**. [Building the command array](#c02-building-the-command-array)
//**C03**. [Build UGUI Args object](#c03-build-ugui-arg-object)
//**C04**. [Find key value](#c04-find-key-value)
//**C05**. [Parse argument](#c05-parse-argument)
//**C06**. [Process all <cmd> definitions](#c06-process-all-cmd-definitions)
//**C07**. [Convert command array to string](#c07-convert-command-array-to-string)
//**C08**. [Set input file path, file name, and extension](#c08-set-input-file-path-file-name-and-extension)
//**C09**. [Set input folder path and folder name](#c09-set-input-folder-path-and-folder-name)
//**c10**. [Prevent user from entering quotes in forms](#c10-prevent-user-from-entering-quotes-in-forms)
//**C11**. [Color processor](#c11-color-processor)
//
//**D00. [UI Elements](#d00-ui-elements)**
//**D01**. [Submit is locked until required is fulfilled](#d01-submit-locked-until-required-fulfilled)
//**D02**. [Replace HTML text with text from package.json](#d02-replace-html-text-with-text-from-package-json)
//**D03**. [Update about modal](#d03-update-about-modal)
//**D04**. [Navigation bar functionality](#d04-navigation-bar-functionality)
//**D05**. [Launch links in default browser](#d05-launch-links-in-default-browser)
//
//**E00. [Warnings](#e00-warnings)**
//**E01**. [Warn if identical data-argNames](#e01-warn-if-identical-data-argnames)
//
//**F00. [UGUI Developer Toolbar](#f00-ugui-developer-toolbar)**
//**F01**. [Detect if in developer environment](#f01-detect-if-in-developer-environment)
//**F02**. [Put all executables in dropdowns](#f02-put-all-executables-in-dropdowns)
//**F03**. [Real-time updating of command output in UGUI Dev Tools](#f03-real-time-updating-dev-tool-command-output)
//**F04**. [Put CLI help info in UGUI dev tools](#f04-put-cli-help-info-in-ugui-dev-tools)
//**F05**. [Swap Bootswatches](#f05-swap-bootswatches)
//**F06**. [Save chosen Bootswatch](#f06-save-chosen-bootswatch)
//**F07**. [Custom keyboard shortcuts](#f07-custom-keyboard-shortcuts)
//
//**G00. [Plugins](#g00-plugins)**
//**G01**. [EZDZ: Drag and drop file browse box](#g01-ezdz-drag-and-drop)
//**G02**. [Range slider](#g02-range-slider)
//**G03**. [Cut/copy/paste context menu](#g03-cut-copy-paste-context-menu)
//
//**H00. [Settings](#h00-settings)**
//**H01**. [Save Settings](#h01-save-settings)
//**H02**. [Load Settings](#h02-load-settings)
//**H03**. [The UGUI Object](#h03-the-ugui-object)
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//
//## A00. Intro
//
//This is the start of the file. We have UGUI wait until the
//document is ready before performing any actions. We've also
//added in a way to delay the running of UGUI until Webkit
//Developer Tools can launch. This will allow you to hit a
//debugger in time, if contributing to UGUI.
//
//
//
//
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### A01. UGUI Start
//
//Wait for the document to load before running ugui.js. Use either runUGUI or waitUGUI for immediate or delayed launch.
$(document).ready( runUGUI );
//This lets you open NW.js, then immediately launch the Webkit Developer Tools, then a few seconds later run UGUI.
//Good for hitting a debugger in time, as often the JS runs before the Webkit Developer Tools can open.
function waitUGUI() {
require("nw.gui").Window.get().showDevTools();
setTimeout(runUGUI, 6000);
}
//Container for all UGUI components
function runUGUI() {
//This is the one place where the UGUI version is declared
var uguiVersion = "1.1.2";
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### A02. UGUI Variables
//
//>Listing of Variables used throughout this library.
//All arguments sent in the command
var allArgElements = $("cmd arg");
var index = 0;
//All executables
var executable = [];
for (index = 0; index < $("cmd").length; index++) {
var currentCommandBlock = $("cmd")[index];
executable.push($(currentCommandBlock).attr("executable"));
}
//Create the argsForm array containing all elements with a `data-argName` for each executable form.
var argsForm = [];
for (index = 0; index < executable.length; index++) {
argsForm.push( $("#" + executable[index] + " *[data-argName]" ) );
}
//Get all text fields where a quote could be entered
var textFields = $( "textarea[data-argName], input[data-argName][type=text]" ).toArray();
//Allow access to the filesystem
var fs = require("fs");
//Access the contents of the package.json file
var packageJSON = require("nw.gui").App.manifest;
//Name of the developer's application as a URL/File path safe name, set in package.json
var appName = packageJSON.name;
//The app title that is used dynamically throughout the UI and title bar, set in package.json
var appTitle = packageJSON.window.title;
//Version of the developer's application, set in package.json
var appVersion = packageJSON.version;
//Description or tagline for application, set in package.json
var appDescription = packageJSON.description;
//Name of the app developer or development team, set in package.json
var authorName = packageJSON.author;
//Name of the starting page for the app, set in package.json
var indexFile = packageJSON.main;
//Full path to the app project folder
var pathToProject = window.location.pathname.split(indexFile)[0];
//Detect if Bootstrap is loaded
var bootstrap3_enabled = (typeof $().emulateTransitionEnd == 'function');
//Detect if Bootstrap Slider is loaded
var slider_enabled = (typeof $().slider == 'function' );
//You can stylize console outputs in Webkit, these are essentially CSS classes
var consoleNormal = "font-family: sans-serif";
var consoleBold = "font-family: sans-serif;" +
"font-weight: bold";
var consoleCode = "background: #EEEEF6;" +
"border: 1px solid #B2B0C1;" +
"border-radius: 7px;" +
"padding: 2px 8px 3px;" +
"color: #5F5F5F;" +
"line-height: 22px;" +
"box-shadow: 0px 0px 1px 1px rgba(178,176,193,0.3)";
var consoleError = "background: #F6EEEE;" +
"border: 1px solid #C1B0B2;" +
"border-radius: 7px;" +
"padding: 2px 8px 3px;" +
"color: #5F5F5F;" +
"line-height: 22px;" +
"box-shadow: 0px 0px 1px 1px rgba(193,176,178,0.3)";
//Placing this at the start of a console output will let you style it.
//**Example**: `console.info(º+"Some bold text.", consoleBold);`
var º = "%c";
//Make sure the `ugui` and `ugui.args` objects exist, if not create them
if (!window.ugui) {
window.ugui = {};
window.ugui.args = {};
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//
//## B00. Simplified Commands
//
//These are easy to run commands for common tasks a desktop
//application would perform. Such as reading, writing, and
//deleting files and folders or running native executables.
//
//
//
//
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### B01. Run CMD
//
//>This is what makes running your CLI program and arguments
// easier. Cow & Taco examples below to make life simpler.
//>
//
//> $("#taco").click( function() {
// runcmd('pngquant --force "file.png"');
// });
//
//> runcmd("node --version", function(data) {
// $("#cow").html("<pre>Node Version: " + data + "</pre>");
// });
//
function runcmd(executableAndArgs, callback) {
//Validate that the required argument is passed and is a string
if (!executableAndArgs || typeof(executableAndArgs) !== "string") {
console.info(º+"You must pass in a string containing the exectuable " +
"and arguments to be sent to the command line.", consoleNormal);
console.info(º+"Example:", consoleBold);
console.info(º+'ugui.helpers.runcmd("pngquant.exe --speed 11mph --force file.png");', consoleCode);
return;
}
var exec = require("child_process").exec;
var child = exec( executableAndArgs,
//Throw errors and information into console
function(error, stdout, stderr) {
console.log(executableAndArgs);
console.log("stdout: " + stdout);
console.log("stderr: " + stderr);
if (error !== null) {
console.log("Executable Error: " + error);
}
console.log("---------------------");
}
);
//Return data from command line
child.stdout.on("data", function(chunk) {
if (typeof callback === "function") {
callback(chunk);
}
});
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### B02. Run CMD (Advanced)
//
//>This is a more advanced option for running executables. You
// can pass in a parameters object to get additional
// functionality such as running a function when an executable
// closes, finishes, errors, or returns data.
//
//> ugui.helpers.runcmdAdvanced(parameters);
//
//>Below is an example parameters object.
//
//> var parameters = {
// "executableAndArgs": "node --version",
// "returnedData": function(data) {
// console.log("The text from the executable: " + data);
// },
// "onExit": function(code) {
// console.log("Executable finished with the exit code: " + code);
// },
// "onError": function(err) {
// console.log("Executable finished with the error: " + err);
// },
// "onClose": function(code) {
// console.log("Executable has closed with the exit code: " + code);
// }
// };
//
function runcmdAdvanced(parameters) {
//Validate that required argument is passed
if (!parameters) {
console.info(º+"You must pass in an object with your options.", consoleNormal);
console.info(º+"Example:", consoleBold);
console.info(º+"var parameters = { 'executableAndArgs': 'node --version' };", consoleCode);
console.info(º+"ugui.helpers.runcmdAdv(parameters);", consoleCode);
return;
}
//Validate types
if (Object.prototype.toString.call(parameters) !== "[object Object]") {
console.info(º+"Your parameters must be passed as an object.", consoleNormal);
return;
} else if (typeof(parameters.executableAndArgs) !== "string") {
console.info(º+"Executable and arguments must be passed as a string. Example:", consoleNormal);
console.info(º+'"node --version"', consoleCode);
return;
} else if (parameters.returnedData && typeof(parameters.returnedData) !== "function") {
console.info(º+"returnedData must be a function.", consoleNormal);
return;
} else if (parameters.onExit && typeof(parameters.onExit) !== "function") {
console.info(º+"onExit must be a function.", consoleNormal);
return;
} else if (parameters.onError && typeof(parameters.onError) !== "function") {
console.info(º+"onError must be a function.", consoleNormal);
return;
} else if (parameters.onClose && typeof(parameters.onClose) !== "function") {
console.info(º+"onClose must be a function.", consoleNormal);
return;
}
var exec = require("child_process").exec;
var child = exec( parameters.executableAndArgs,
//Throw errors and information into console
function(error, stdout, stderr) {
console.log(parameters.executableAndArgs);
console.log("stdout: " + stdout);
console.log("stderr: " + stderr);
if (error !== null) {
console.log("Executable Error: " + error);
} else {
return child;
}
console.log("---------------------");
}
);
//Detect when executable finishes
child.on("exit", function(code) {
if (typeof parameters.onExit === "function") {
parameters.onExit(code);
}
});
//Detect when executable errors
child.on("error", function(code) {
if (typeof parameters.onError === "function") {
parameters.onError(code);
}
});
//Detect when the executable is closed
child.on("close", function(code) {
if (typeof parameters.onClose === "function") {
parameters.onClose(code);
}
});
//Return data from command line
child.stdout.on("data", function(chunk) {
if (typeof parameters.returnedData === "function") {
parameters.returnedData(chunk);
}
});
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### B03. Read a file
//
//>A function that allows you to set the contents of a file to
// a variable. Like so:
//
//> var devToolsHTML = ugui.helpers.readAFile("_markup/ugui-devtools.htm");
//
function readAFile(filePathAndName) {
//Validate that required argument is passed
if (!filePathAndName) {
console.info(º+"Supply a path to the file you want to read as " +
"an argument to this function.", consoleNormal);
return;
}
//Validate types
if (typeof(filePathAndName) !== "string") {
console.info(º+"File path must be passed as a string.", consoleNormal);
return;
}
var fileData = fs.readFileSync(filePathAndName, {encoding: "UTF-8"});
return fileData;
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### B04. Read contents of a folder
//
//>Supply a path to a folder as a string and UGUI will return an
// object with each file and folder as a sub-object. Each item
// returned will have an "isFolder" property to tell if it's a
// file or a folder, and with the exception of some system
// files, they should all have a file size as well.
//
//>To make things easier, we also return an array that lists all
// items returned.
//
//> var mediaContents = ugui.helpers.readAFolder("_media");
//
//>Since you can't use spaces or dots in dot notation, folders
// and files like "Hello World" and "file.txt" can't be
// accessed by doing `mediaContents.Hello World.size` or
// `mediaContents.file.txt.isFolder`. So to access those use:
//
//> mediaContents["Hello World"].size
// mediaContents["file.txt"].isFolder
//
function readAFolder(filePath, callback) {
//Validate that required argument is passed
if (!filePath) {
console.info(º+"Supply a path to the file you want to read as " +
"an argument to this function.", consoleNormal);
return;
}
//Validate types
if (typeof(filePath) !== "string") {
console.info(º+"File path must be passed as a string.", consoleNormal);
return;
} else if (callback && typeof(callback) !== "function") {
console.info(º+"Callback must be passed as a function.", consoleNormal);
return;
}
//Create an object with an array in it
var contents = { "_folder_contents_array": [] };
//Read the directory passed in
fs.readdir(filePath, function (err, files) {
//If there were problems reading the contents of a folder, stop and report them
if (err) {
console.info(º+"Unable to read contents of the folder:", consoleNormal);
console.warn(º+err.message, consoleError);
return;
}
files.forEach( function (file) {
fs.lstat(filePath + file, function(err, stats) {
//Retain an array of all files and folders
contents._folder_contents_array.push(file);
//Check if it's a folder
if (!err && stats.isDirectory()) {
contents[file] = {
"isFolder": true,
"size": 0
}
//Check if it has a file size
} else if (!err && file !== "undefined") {
contents[file] = {
"isFolder": false,
"size": stats.size
}
//Catch-all
} else {
contents[file] = {
"isFolder": false
}
}
});
});
});
//If a callback was passed in, run it
if (callback) {
callback(contents);
//Otherwise just return the contents of the folder
} else {
return contents;
}
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### B05. Write to file
//
//>This will override the contents of a file you pass in with
// the data you supply. If the file you point to doesn't exist,
// it will be created with your supplied data.
//
//> ugui.helpers.writeToFile("C:/folder/new_file.htm", "Text.");
//
function writeToFile(filePathAndName, data, callback) {
//Validate that required arguments are passed and are the correct types
if (!filePathAndName || typeof(filePathAndName) !== "string") {
console.info(º+"Supply a path to the file you want to create or replace the " +
"contents of as the first argument to this function.", consoleNormal);
console.info(º+"File path and name must be passed as a string.", consoleNormal);
console.info(º+"Example:", consoleBold);
console.info(º+'ugui.helpers.writeToFile("C:/folder/file.htm", "Your data.");', consoleCode);
return;
} else if (!data) {
console.info(º+"You must pass in the data to be stored as the second argument " +
"to this function.", consoleNormal);
return;
} else if (typeof(data) !== "string") {
console.info(º+"The data to be stored must be passed as a string.", consoleNormal);
return;
} else if (callback && typeof(callback) !== "function") {
console.info(º+"Your callback must be passed as a function.", consoleNormal);
return;
}
//Write to the file the user passed in
fs.writeFile(filePathAndName, data, function(err) {
//If there was a problem writing to the file
if (err) {
console.info(º+"There was an error attempting to save your data.", consoleNormal);
console.warn(º+err.message, consoleError);
return;
//If the file was updated and the user passed in a callback function, run it
} else if (callback) {
callback();
}
});
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### B06. Create a folder
//
//>This will create a new folder in the location you pass in.
// You cannot create a new folder with a folder in it. So if
// `C:/Taco` doesn't exist then the following wouldn't work:
//
//> ugui.helpers.createAFolder("C:/Taco/Cheese");
//
//>You'd need to create the "Taco" folder first, then the
// "Cheese" folder, like so:
//
//> ugui.helpers.createAFolder("C:/Taco", ugui.helpers.createAFolder("C:/Taco/Cheese"));
//
//>In this example we are using a callback to create a
// subfolder. This ensures that the "Taco" folder will exist
// before we attempt to create the "Cheese" folder.
//
function createAFolder(filePath, callback) {
//Validate that required argument is passed and is the correct types
if (!filePath || typeof(filePath) !== "string") {
console.info(º+"Supply a path to where you want your folder as the first " +
"argument to this function.", consoleNormal);
console.info(º+"Folder path must be passed as a string.", consoleNormal);
console.info(º+"Example:", consoleBold);
console.info(º+'ugui.helpers.createAFolder("C:/folder/new_folder");', consoleCode);
return;
//If a callback was passed in and it isn't a function
} else if (callback && typeof(callback) !== "function") {
console.info(º+"Your callback must be passed as a function.", consoleNormal);
return;
}
//Create the folder in the supplied location
fs.mkdir(filePath, function(err) {
//If there was a problem creating the folder
if (err) {
console.info(º+"There was an error attempting to create the folder.", consoleNormal);
console.warn(º+err.message, consoleError);
return;
//If the folder was created and the user passed in a callback function, run it now
} else if (callback) {
callback();
}
});
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### B07. Delete a file
//
//>Though UGUI doesn't currently use this functionality anywhere
// within itself, we thought it would be nice to offer a quick
// and easy way of deleting files.
//
//> ugui.helpers.deleteAFile("C:/folder/delete_me.htm");
//
function deleteAFile(filePathAndName, callback) {
//Validate that required argument is passed and is the correct types
if (!filePathAndName || typeof(filePathAndName) !== "string") {
console.info(º+"Supply a path to the file you want to delete as " +
"the first argument to this function.", consoleNormal);
console.info(º+"File path must be passed as a string.", consoleNormal);
console.info(º+"Example:", consoleBold);
console.info(º+'ugui.helpers.deleteAFile("C:/folder/delete_me.txt");', consoleCode);
return;
//If a callback was passed in and it isn't a function
} else if (callback && typeof(callback) !== "function") {
console.info(º+"Your callback must be passed as a function.", consoleNormal);
return;
}
//Delete the selected file
fs.unlink(filePathAndName, function(err) {
//If there was a problem deleting the file
if (err) {
console.info(º+"There was an error attempting to delete the file.", consoleNormal);
console.warn(º+err.message, consoleError);
return;
//If the file deleted and the user passed in a callback function, run it now
} else if (callback) {
callback();
}
});
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### B08. Delete a folder
//
//>Though UGUI doesn't currently use this functionality anywhere
// within itself, we thought it would be nice to offer a quick
// and easy way to delete folders.
//
//> ugui.helpers.deleteAFolder("C:/path/to/folder");
//
//>**NOTE:** This will not delete a folder unless it is empty.
//
function deleteAFolder(filePath, callback) {
//Validate that required argument is passed and is the correct types
if (!filePath || typeof(filePath) !== "string") {
console.info(º+"Supply a path to the folder you want to delete as " +
"the first argument to this function.", consoleNormal);
console.info(º+"Folder path must be passed as a string.", consoleNormal);
console.info(º+"Example:", consoleBold);
console.info(º+'ugui.helpers.deleteAFolder("C:/folder/delete_me");', consoleCode);
return;
//If a callback was passed in and it isn't a function
} else if (callback && typeof(callback) !== "function") {
console.info(º+"Your callback must be passed as a function.", consoleNormal);
return;
}
//Delete the selected folder
fs.rmdir(filePath, function(err) {
//If there was a problem deleting the folder
if (err) {
console.info(º+"There was an error attempting to delete the folder.", consoleNormal);
console.warn(º+err.message, consoleError);
return;
//If the folder deleted and the user passed in a callback function, run it now
} else if (callback) {
callback();
}
});
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//
//## C00. CLI Command Processing
//
//This section is the primary purpose, and the heart of UGUI.
//It's what takes the `<cmd>`, `<arg>`, and `<def>` stuff,
//matches it with the `data-argName` stuff, and ultimately
//outputs it to the command-line.
//
//It's also responsible for building the UGUI Arg object,
//which is used for other things, like saving settings.
//
//
//
//
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### C01. Clicking Submit
//
//>What happens when you click the submit button.
//
//>When the button is pressed, prevent it from submitting the
// form like it normally would in a browser. Detect which form
// the submit button is in and what executable it corresponds
// to. Remove quotes from all fields. Build the command line
// array based on UGUI Args Object and turn it into a string.
//
//>Detect if text from the command line is meant to be printed
// on the page. Then run the command.
//When you click the submit button.
$(".sendCmdArgs").click( function(event) {
//Prevent the form from sending like a normal website.
event.preventDefault();
//Get the correct executable to use based on the form you clicked on
var thisExecutable = $(this).closest("form").attr("id");
//Remove all single/double quotes from any text fields
removeTypedQuotes();
//Build the command line array with the executable and all commands
var builtCommandArray = buildCommandArray(thisExecutable);
//Convert the array to a string that can be ran in a command line using the runcmd function
var builtCommandString = convertCommandArraytoString(builtCommandArray);
//Check if the form has an element with a class of `returnedCmdText`
if ( $("#" + thisExecutable + " .returnedCmdText").length > 0 ) {
//If so, run a command and put its returned text on the page
runcmd( builtCommandString, function(data) {
$("#" + thisExecutable + " .returnedCmdText").html(data);
});
} else {
//Run the command!
runcmd(builtCommandString);
}
});
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### C02. Building the Command Array
//
//>What happens when you click the submit button or when the
// UGUI Developer Toolbar's "CMD Output" section is updated to
// preview the outputted command that would be sent to the
// command line/terminal.
//
function buildCommandArray(thisExecutable) {
//Validate types
if (thisExecutable !== undefined && typeof(thisExecutable) !== "string") {
console.info(º+"Executable must be passed as a string.", consoleNormal);
return;
}
//If no executable was passed in, just use the first one in `<cmd>`'s
thisExecutable = thisExecutable || executable[0];
//Set up commands to be sent to command line
var cmds = [ thisExecutable ];
//Fill out `window.ugui.args` object
buildUGUIArgObject();
//Process all definitions and place them in `window.ugui.args`
patternMatchingDefinitionEngine();
//Setting up arrays
var cmdArgsText = [];
//Loop through all the `<args>` in the `<cmd>` with the selected executable
for (index = 0; index < $("cmd[executable=" + thisExecutable + "] arg").length; index++) {
//Set the current `<arg>`
var currentArg = $("cmd[executable=" + thisExecutable + "] arg")[index];
//Put the `<arg>` text into an array
cmdArgsText.push( $(currentArg).text() );
}
//Loop through all phrases and add processed versions to output array
for (index = 0; index < cmdArgsText.length; index++) {
//`cmdArgsText[index]` is `--quality ((meow)) to ((oink.min))`
cmds.push( parseArgument(cmdArgsText[index]) );
}
//After all the processing is done and the array is built, return it
return cmds;
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### C03. Build UGUI Arg Object
//
//>This grabs all the data about the elements on the page that
// have a `data-argName` and puts that information on the window
// object, located here: `window.ugui.args`
//
function buildUGUIArgObject() {
//Reset the UGUI Args Object to remove any stragglers
window.ugui.args = {};
//Make an array containing every element on the page with an attribute of `data-argName`
var cmdArgs = $("*[data-argName]");
//Cycle through all elements with a `data-argName` in `<form id="currentexecutable">`
for (index = 0; index < cmdArgs.length; index++) {
//Get `bob` from `<input data-argName="bob" value="--kitten" />`
var argName = $(cmdArgs[index]).attr("data-argName");
//Get `--carrot` from `<input type="radio" data-argName="vegCarrot" value="--carrot" />`
var argValue = $(cmdArgs[index]).val();
//Declare variable now to be defined below
var argType = "";
//Get `input` from `<input data-argName="bob" type="checkbox" />`
var argTag = $(cmdArgs[index]).prop("tagName").toLowerCase();
//See if the current item is a range slider
if ( $(cmdArgs[index]).hasClass("slider") ) {
//Manually set the type to `range` for range slider elements
argType = "range";
//See if the element is an item in one of Bootstrap's fake dropdowns
} else if ( $(cmdArgs[index]).parent().parent().hasClass("dropdown-menu") ) {
//Manually set the type to `range` for range slider elements
argType = "dropdown";
} else if ( $(cmdArgs[index]).attr("nwdirectory") ) {
//Manually set the type if it's a directory browser
argType = "folder";
} else if (argTag == "select") {
//Manually set the type if it's a traditional dropdown
argType = "select";
} else {
//Get `checkbox` from `<input data-argName="bob" type="checkbox" />`
argType = $(cmdArgs[index]).attr("type");
}
//Basic info put on every object
window.ugui.args[argName] = {
"value": argValue,
"htmltag": argTag,
"htmltype": argType
};
//Special info just for `<input type="file" nwdirectory="nwdirectory">`
if (argType === "folder") {
setInputFolderPathName(cmdArgs[index], argName);
window.ugui.args[argName].htmltag = argTag;
window.ugui.args[argName].htmltype = argType;
}
//Special info just for `<input type="file">`
if (argType === "file") {
setInputFilePathNameExt(cmdArgs[index], argName);
window.ugui.args[argName].htmltag = argTag;
window.ugui.args[argName].htmltype = argType;
}
//Special info just for `<input type="color">`
if (argType === "color") {
colorProcessor(argValue, argName);
window.ugui.args[argName].htmltag = argTag;
window.ugui.args[argName].htmltype = argType;
}
//For checkboxes and radio dials, add special info
if (argType === "checkbox" || argType === "radio" || argType === "dropdown") {
if ( $(cmdArgs[index]).prop("checked") ) {
window.ugui.args[argName].htmlticked = true;
} else {
window.ugui.args[argName].htmlticked = false;
}
}
if (argTag === "textarea") {
window.ugui.args[argName] = {
"value": argValue,
"htmltag": argTag,
"htmltype": "textarea"
};
}
}
}
//Run once on page load
buildUGUIArgObject();
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//### C04. Find Key Value
//
//>This is a general purpose function that allows retrieving
// information from an object. Here is an example object and
// how `findKeyValue()` works to return data from it:
//
//> var a = {
// "b": "dog",
// "c": {
// "d": "cat",
// "e": "bat"
// }
// };
// var ab = ["b"];
// var acd = ["c","d"];
//
//> console.log( findKeyValue(a,ab) ); //dog
// console.log( findKeyValue(a,acd) ); //cat
//
function findKeyValue(obj, arr) {
//Validate that both required arguments are passed
if(!obj || !arr) {
console.info(º+"You need to supply an object and an array of " +
"strings to drill down within the object.", consoleNormal);
return;
}
//Validate types
if (Object.prototype.toString.call(obj) !== "[object Object]") {