-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
1442 lines (1172 loc) · 51.6 KB
/
main.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
// mpvDLNA 3.4.1
"use strict";
mp.module_paths.push(mp.get_script_directory() + "/modules.js");
var Options = require('Options');
var Ass = require('AssFormat');
var SelectionMenu = require('SelectionMenu');
mp.module_paths.pop();
var DLNA_Node = function(name, id) {
this.name = name;
this.id = id;
this.url = null;
this.children = null;
this.info = null; // format is {start: episode#, end: episode#, description: string}
this.isPlaying = false;
this.type = "node";
};
var DLNA_Server = function(name, url) {
this.name = name;
this.id = "0"
this.url = url;
this.children = null;
this.type = "server"
};
// Helper function to remove first element and trailing newlines
var removeNL = function(sp) {
for (var i = 0; i < sp.length; i++) {
var s = sp[i]
if (s[s.length - 1] == "\r") {
s = s.slice(0, -1);
}
if (s[s.length - 1] == "\n") {
s = s.slice(0, -1);
}
sp[i] = s;
}
if (!sp[0] || !sp[0].length) {
sp.shift();
}
return sp;
};
// Class to browse DLNA servers
var DLNA_Browser = function(options) {
options = options || {};
// --------------------------
this.showHelpHint = typeof options.showHelpHint === 'boolean' ?
options.showHelpHint : true;
this.descriptionSize = options.descriptionFontSize;
if (this.descriptionSize === null) {
this.descriptionSize = options.menuFontSize / 4.5;
}
this.menu = new SelectionMenu({ // Throws if bindings are illegal.
maxLines: options.maxLines,
menuFontSize: options.menuFontSize,
autoCloseDelay: options.autoCloseDelay,
keyRebindings: options.keyRebindings
});
this.menu.setMetadata({type:null});
this._registerCallbacks();
var self = this;
// Only use menu text colors while mpv is rendering in GUI mode (non-CLI).
this.menu.setUseTextColors(mp.get_property_bool('vo-configured'));
mp.observe_property('vo-configured', 'bool', function(name, value) {
self.menu.setUseTextColors(value);
});
// Determine how to call python
this.python = null;
var versions = ["python", "python3"];
var working_python = []
var upnp_errored = []
// If the .conf file specifies an option, test it first
if (options.python_version) {
versions.unshift(options.python_version);
}
// Test --version for each python call option
for (var i = 0; i < versions.length; i++) {
var result = mp.command_native({
name: "subprocess",
playback_only: false,
capture_stdout: true,
capture_stderr: true,
args : [versions[i], "--version"]
});
if (result.status != 0) {
mp.msg.debug("calling python --version as " + versions[i] + " errored with: " + result.stderr);
continue;
} else {
working_python.push(versions[i]);
}
}
// test mpvDLNA.py for each working python call
for (var i = 0; i < working_python.length; i++) {
var result = mp.command_native({
name: "subprocess",
playback_only: false,
capture_stdout: true,
capture_stderr: true,
args : [working_python[i], mp.get_script_directory()+"/mpvDLNA.py", "-v"]
});
if (result.status != 0) {
mp.msg.debug("calling mpvDLNA.py using " + working_python[i] + " errored with: " + result.stderr);
} else if (result.stdout.search("upnp import failed") != -1) {
upnp_errored.push(working_python[i]);
mp.msg.debug("python call: " + working_python[i] + " does not have upnp installed correctly ");
} else {
this.python = working_python[i];
mp.msg.debug("selecting '" + working_python[i] + "' for python call")
break;
}
}
// None of the options worked, throw an error
if (working_python.length == 0) {
throw new Error("Unable to find a correctly configured python call: \n \
in the following options: " + versions +
"\n Please add the name of your python install to the .conf file \n \
using the format: python_version=python \n \
or run mpv with the --msg-level=mpvDLNA=trace argument to see the errors");
// Some of the options worked but couldn't run mpvDLNA.py
} else if (this.python == null){
// upnpClient is not installed correctly
if (upnp_errored.length > 0) {
throw new Error("The following python calls exist but do not have upnpClient installed properly: " + versions);
// Some other error occured
} else {
throw new Error("The following python calls exist but failed to run mpvDLNA: " + versions +
"\n Please run mpv with the --msg-level=mpvDLNA=trace argument to see the errors");
}
}
// How long to spend searching for DLNA servers
this.timeout = options.timeout
// How many nodes to fetch from the DLNA server when making a request
this.count = options.count
// list of the parents of the current node.
// The first element represents the server we are currently browsing
// The last element represents the node we are currently on
this.parents = [];
// List of titles to combine to get the title of the menu
this.titles = [];
this.current_folder = [];
// determine if we need to scan for DLNA servers next time the browser opens
this.scan = true;
this.servers = [];
// handle servers listed in the config file
for (var i = 0; i < options.serverNames.length; i++) {
this.servers.push(new DLNA_Server(options.serverNames[i], options.serverAddrs[i]));
}
if (this.servers.length != 0) {
this.menu.title = "Servers";
this.current_folder = this.servers;
this.menu.setOptions(this.servers, 0);
this.scan = false;
}
// list of wake on lan mac addresses
this.mac_addresses = options.macAddresses;
// List of nodes added to playlist. Should mirror the MPV internal playlist.
// This is necessary because in certain edge cases if a user were to be playing
// an episode while browsing with the DLNA browser and left the folder that the
// current episodes were in then we wouldn't be able to figure out where the
// now playing indicator should be
this.playlist = [];
this.playingUrl = null;
// Typing functionality
this.typing_controls = {
"ESC" : function(self){ self.toggle_typing() },
"ENTER" : function(self){ self.typing_parse() },
"LEFT" : function(self){ self.typing_action("left") },
"RIGHT" : function(self){ self.typing_action("right") },
"DOWN" : function(self){ self.typing_action("down") },
"UP" : function(self){ self.typing_action("up") },
"BS" : function(self){ self.typing_action("backspace") },
"CTRL+BS" : function(self){ self.typing_action("ctrl+backspace") },
"DEL" : function(self){ self.typing_action("delete") },
"SPACE" : function(self){ self.typing_action(" ") },
"TAB" : function(self){ self.typing_action("tab") }
};
this.typing_keys = [];
for (var i = 33; i <= 126; i++) {
this.typing_keys.push(String.fromCharCode(i));
}
this.typing_mode = "text";
this.typing_active = false;
this.typing_position = 0;
this.typing_text = "";
this.typing_output = "";
this.autocomplete = [];
this.selected_auto = {id: 0, full: ""};
this.commands = {
"scan" : { func: function(self){ self.menu.showMessage("Scanning");
self.findDLNAServers();
self.menu.showMessage("Scan Complete"); },
args: [],
text: false,
output: false},
"cd" : { func: function(self, file) { self.command_cd(file); },
args: [],
text: true,
output: false},
"text" : { func: function(self, file){ self.typing_mode = "text";
self.typing_text = "";
self.typing_position = 0;
self.typing_active = true;
self.typing_action(""); },
args: [],
text: false,
output: false},
"info" : { func: function(self, file){ var info = self.command_info(file);
if (info === null) {this.typing_output = "No Information";}
else{
self.typing_output = "Episode Number: "+info.start;
if (info.start != info.end) {self.typing_output += "-"+info.end;}
self.typing_output += Ass.size(self.descriptionSize, true)+"\nDescription: "+info.description;
}},
args: [],
text: true,
output: true},
"ep" : { func: function(self, args, file){ self.command_ep(args, file); },
args: ["Episode"],
text: true,
output: true},
"pep" : { func: function(self, args, file){ var result = self.command_ep(args, file);
if (result) self.select(self.menu.getSelectedItem()) },
args: ["Episode"],
text: true,
output: true},
"wake" : { func: function(self, args){ self.command_wake(args); },
args: ["Mac Address"],
auto: function(self, index){ return self.mac_addresses; },
text: false,
output: true},
};
this.command_list = Object.keys(this.commands);
// String key of the current command
this.command = null;
// List of already typed command arguments
this.arguments = [];
// List of unfinished typed command argument
this.typing_argument= "";
this.result_displayed = true;
// Send startup MAC Address wake on lan packets
options.startupMacAddresses.forEach(function(addr) { self.command_wake([addr]) });
};
DLNA_Browser.prototype.findDLNAServers = function() {
this.scan = false;
this.menu.title = "Scanning for DLNA Servers";
this.menu.renderMenu("", 1);
mp.msg.info("scanning for dlna servers");
this.servers = [];
// Increase the timeout if you have trouble finding a DLNA server that you know is working
var result = mp.command_native({
name: "subprocess",
playback_only: false,
capture_stdout: true,
capture_stderr: true,
args : [this.python, mp.get_script_directory()+"/mpvDLNA.py", "-l", this.timeout]
});
mp.msg.debug("mpvDLNA.py -l: " + result.stderr);
// Get the output, delete the first element if empty, and remove trailing newlines
var sp = removeNL(result.stdout.split("\n"));
for (var i = 0; i < sp.length; i=i+3) {
var server = new DLNA_Server(sp[i], sp[i+1]);
this.servers.push(server);
}
this.menu.title = "Servers";
this.parents = [];
this.current_folder = this.servers;
this.menu.setOptions(this.servers, 0);
this.menu.renderMenu("", 1);
};
DLNA_Browser.prototype.toggle = function() {
// Toggle the menu display state.
if (this.menu.menuActive) {
this.menu.hideMenu();
} else {
this.menu.showMenu();
// Determine if we need to scan for DLNA servers
if (this.scan) {
this.menu.title = "Scanning for DLNA Servers";
this.findDLNAServers();
}
}
};
// starts typing and sets mode to either "text" or "command"
DLNA_Browser.prototype.toggle_typing = function(mode) {
if (!this.typing_active) {
// if mode command is invalid just leave it on what it was before
if (mode == "text" || mode == "command") {
this.typing_mode = mode;
}
var self = this;
Object.keys(this.typing_controls).forEach( function(key) {
mp.add_forced_key_binding(key, "typing_"+key, function(){self.typing_controls[key](self)}, {repeatable:true})
});
this.typing_keys.forEach( function(key) {
mp.add_forced_key_binding(key, "typing_"+key, function(){self.typing_action(key)}, {repeatable:true})
});
this.typing_text = "";
this.typing_position = 0;
this.typing_active = true;
this.menu.showTyping();
this.typing_action("");
} else {
this.typing_active=false;
Object.keys(this.typing_controls).forEach( function(key) {
mp.remove_key_binding("typing_"+key);
});
this.typing_keys.forEach( function(key) {
mp.remove_key_binding("typing_"+key);
});
this.menu.hideTyping();
}
};
DLNA_Browser.prototype.typing_action = function(key) {
var tabbing = false;
if (key.length == 1){
// "\" does not play nicely with the formatting characters in the osd message
if (key != "\\") {
this.typing_text = this.typing_text.slice(0, this.typing_position)
+ key + this.typing_text.slice(this.typing_position);
this.typing_position+=1;
}
} else if (key == "backspace") {
// can't backspace if at the start of the line
var removed = "";
if (this.typing_position) {
removed = this.typing_text.slice(this.typing_position-1, this.typing_position);
this.typing_text = this.typing_text.slice(0, this.typing_position-1)
+ this.typing_text.slice(this.typing_position);
this.typing_position-= 1;
}
if (this.typing_mode == "command" && this.command != null) {
// The backspace effected the command
if (this.typing_position <= this.command.length) {
this.command = null;
this.typing_output = "";
// The backspace effected an argument
} else if (removed == " "){
var arg_lengths = 0;
this.arguments.forEach(function(arg){arg_lengths+=arg.length+1});
if (this.typing_position <= this.command.length + arg_lengths + 1) {
this.arguments.pop();
}
}
}
} else if (key == "ctrl+backspace") {
// May change this to delete the relevant filename/argument/command later
// Right now it just clears all text
this.typing_position = 0;
this.typing_text = "";
if (this.typing_mode == "command") {
this.command = null;
this.arguments = [];
this.typing_argument = "";
}
} else if (key == "delete") {
this.typing_text = this.typing_text.slice(0, this.typing_position)
+ this.typing_text.slice(this.typing_position+1);
if (this.typing_mode == "command" && this.command != null) {
if (this.typing_position <= this.command.length) {
this.command = null;
// Because we autoadd a space when completing a command and because
// using delete means the cursor is not next to it, the space becomes
// almost impossible to find. I wrote this code and still thought it
// was a bug when the invisible space character supressed the autocorrect
// Much easier for users to just not have to deal with it
if (this.typing_text[this.typing_text.length-1] == " ") {
this.typing_text = this.typing_text.slice(0, -1);
}
}
}
} else if (key == "right") {
this.typing_position += 1;
if (this.typing_position > this.typing_text.length) {
this.typing_position = 0;
}
} else if (key == "left") {
this.typing_position -= 1;
if (this.typing_position < 0) {
this.typing_position = this.typing_text.length;
}
} else if (key == "tab" || key == "down") {
tabbing = true;
this.selected_auto.id++;
if (this.selected_auto.id >= this.autocomplete.length) {
this.selected_auto.id = 0;
}
if (this.autocomplete.length) {
this.selected_auto.full = this.autocomplete[this.selected_auto.id].full;
}
} else if (key == "up") {
tabbing = true;
this.selected_auto.id--;
if (this.selected_auto.id < 0) {
this.selected_auto.id = this.autocomplete.length - 1;
}
if (this.autocomplete.length) {
this.selected_auto.full = this.autocomplete[this.selected_auto.id].full;
}
} else if (key == "clear"){
this.typing_text = "";
this.typing_position = 0;
this.autocomplete = [];
this.selected_auto = {id: 0, full: ""};
if (this.result_displayed) {
this.typing_output = "";
} else {
this.result_displayed = true;
}
}
var message = "";
message += Ass.white(true) + this.typing_text.slice(0, this.typing_position);
message += Ass.yellow(true) + "|";
message += Ass.white(true) + this.typing_text.slice(this.typing_position);
// Use command mode autocorrect.
if (this.typing_mode == "command") {
// Look for a valid command
if (this.command == null) {
this.arguments = [];
// Check if the first piece of the input is a valid command
if (this.typing_text.split(" ").length > 1) {
var search = this.typing_text.split(" ")[0];
for (var i = 0; i < this.command_list.length; i++) {
if (this.command_list[i].toUpperCase() == search.toUpperCase()) {
this.command = this.command_list[i];
break;
}
}
// Otherwise try to autocomplete the command
} else {
message = this.autocomplete_command(this.typing_text, message, tabbing, this.command_list);
}
}
// Have a valid command, autocomplete the arguments
if (this.command){
// Let the user type arguments
if (this.arguments.length < this.commands[this.command].args.length) {
this.arguments = this.typing_text.split(" ").slice(1);
this.typing_argument = this.arguments.pop();
if (this.commands[this.command].auto != null) {
var arg_lengths = 0;
this.arguments.forEach(function(arg){arg_lengths+=arg.length+1});
var argument = this.typing_text.slice(this.command.length + arg_lengths + 1);
var index = message.split(" ").slice(0,-1).join(" ").length + 1
var msg = message.slice(index);
message = message.slice(0, index) + this.autocomplete_command(argument, msg, tabbing, this.commands[this.command].auto(this, this.arguments.length-1));
}
}
// Display a hint about what kind of argument to enter
if (this.arguments.length < this.commands[this.command].args.length) {
this.typing_output = "Argument: " + this.commands[this.command].args[this.arguments.length];
} else if (this.typing_output.split(":")[0] == "Argument"){
this.typing_output = "";
}
// Have all the arguments, autocomplete the file
if (this.arguments.length == this.commands[this.command].args.length &&
this.commands[this.command].text){
var arg_lengths = 0;
this.arguments.forEach(function(arg){arg_lengths+=arg.length+1});
var argument = this.typing_text.slice(this.command.length + arg_lengths + 1);
this.typing_argument = "";
var index = message.split(" ").slice(0,-1).join(" ").length + 1
var msg = message.slice(index);
message = message.slice(0, index) + this.autocomplete_text(argument, msg, tabbing);
}
}
message = "$ " + message;
// Use text mode autocorrect.
} else if (this.typing_mode == "text") {
message = this.autocomplete_text(this.typing_text, message, tabbing);
}
message = Ass.startSeq(true) + message;
message += "\n" + Ass.alpha("00") + this.typing_output;
message += Ass.stopSeq(true);
this.menu.typingText = message;
this.menu._renderActiveText();
};
// Try to find a valid command or folder
DLNA_Browser.prototype.typing_parse = function() {
var success = false;
// Planned Commands (more to come)
// search - Either query the DLNA server if thats possible or just manually search
// Y cd - exactly the same as what text mode does now
// N play - this has been replaced with just trying to cd into the media file
// Y ep - find episode by number (maybe have option for absolute episode number instead of just its place in a season)
// pep - ep but starts playback
// Y info - query DLNA server for metadata (For some reason my DLNA server only gives metadata for episodes, not seasons\shows)
// Y text - switch to text input mode
if (this.typing_mode == "command") {
// This flag is used to make sure we don't accidentally autocomplete the command
// and the arguments in a single enter keystroke
var text_input = false;
if (this.command == null) {
text_input = true;
if (this.autocomplete.length != 0) {
this.command = this.selected_auto.full
this.typing_text = this.selected_auto.full + " ";
this.typing_position = this.typing_text.length;
// This variable is true if the command requires more information than just its name
text_input = this.commands[this.command].text || this.commands[this.command].args.length > 0;
}
}
if (this.command != null && !text_input) {
var cmd = this.commands[this.command];
if (cmd.text) {
// We have all the arguments and file text needed for the command
if (cmd.args.length == this.arguments.length && this.autocomplete.length != 0) {
mp.msg.trace("Calling " + this.command + " with args: " + this.arguments);
if (this.arguments.length > 0) {
cmd.func(this, this.arguments, this.selected_auto.full);
} else {
cmd.func(this, this.selected_auto.full);
}
this.result_displayed = !cmd.output;
success = true;
}
} else {
// Command only needs its name
if (cmd.args.length == 0) {
mp.msg.trace("Calling " + this.command + " with no args");
cmd.func(this);
this.result_displayed = !cmd.output;
success = true;
// We already have all but the last argument
} else if (this.arguments.length == cmd.args.length - 1){
// Autocomplete the last argument
if (this.autocomplete.length != 0) {
this.arguments.push(this.selected_auto.full)
// Can't autocomplete the last argument, use what the user entered
} else {
this.arguments.push(this.typing_argument)
}
mp.msg.trace("Calling " + this.command + " with args: " + this.arguments);
cmd.func(this, this.arguments);
this.result_displayed = !cmd.output;
success = true;
// Not enough arguments, autocomplete the one we are on
} else {
if (this.autocomplete.length != 0) {
if (this.typing_argument.length != 0) {
this.typing_text = this.typing_text.slice(0, -this.typing_argument.length)
}
this.typing_text += this.selected_auto.full + " ";
this.typing_position = this.typing_text.length;
}
}
}
}
} else if (this.typing_mode == "text") {
success = this.command_cd(this.selected_auto.full);
}
if (success) {
this.command = null;
this.typing_action("clear");
} else {
// Rescan for autocomplete
this.typing_action("");
}
};
// Works for commands and arguments
DLNA_Browser.prototype.autocomplete_command = function(text, message, tabbing, options) {
// find new autocomplete options only if we are actually typing
if (!tabbing) {
this.autocomplete = [];
if (this.options === null || (text == "" && this.selected_auto.full=="")) {
this.selected_auto = {id: null, full: ""};
return message;
}
for (var i = 0; i < options.length; i++) {
var index = options[i].toUpperCase().indexOf(text.toUpperCase());
if (index != -1) {
this.autocomplete.push({
pre: options[i].slice(0, index),
post: options[i].slice(index + text.length),
full: options[i],
sindex: index,
findex: i
});
}
}
// Prefer the search term appearing as soon in the text as possible
this.autocomplete.sort(function(a, b) {
return a.sindex == b.sindex ? a.findex - b.findex : a.sindex-b.sindex;
});
}
if (this.autocomplete.length > 0) {
var search = this.selected_auto.full;
// Prefer the earliest option in the list
this.selected_auto = {
pre: this.autocomplete[0].pre,
post: this.autocomplete[0].post,
full: this.autocomplete[0].full,
sindex: this.autocomplete[0].sindex,
findex: this.autocomplete[0].findex,
id: 0
}; // have to break this out or you get crazy referencing issues
for (var i = 0; i < this.autocomplete.length; i++) {
if (search == this.autocomplete[i].full) {
this.selected_auto = {
pre: this.autocomplete[i].pre,
post: this.autocomplete[i].post,
full: this.autocomplete[i].full,
sindex: this.autocomplete[i].sindex,
findex: this.autocomplete[i].findex,
id: i
}; // have to break this out or you get crazy referencing issues
break;
}
}
// Move the actively selected option to the front of the list so entries are
// sorted by how close to the front of the string the search term is
if (!tabbing) {
this.autocomplete = [this.autocomplete[this.selected_auto.id]].concat(
this.autocomplete.slice(0,this.selected_auto.id),
this.autocomplete.slice(this.selected_auto.id+1));
this.selected_auto = {
pre: this.autocomplete[0].pre,
post: this.autocomplete[0].post,
full: this.autocomplete[0].full,
sindex: this.autocomplete[0].sindex,
findex: this.autocomplete[0].findex,
id: 0
}; // have to break this out or you get crazy referencing issues
}
message = Ass.alpha("DDDD6E") + this.selected_auto.pre
+ Ass.alpha("00") + message + Ass.alpha("DDDD6E") + this.selected_auto.post;
} else {
this.selected_auto = {id: 0, full: ""};
}
return message;
}
DLNA_Browser.prototype.autocomplete_text = function(text, message, tabbing) {
// find new autocomplete options only if we are actually typing
if (!tabbing) {
this.autocomplete = [];
// add ".." to the list of autocomplete options
var options = this.current_folder.concat({name: ".."});
for (var i = 0; i < options.length; i++) {
var item = options[i];
var index = item.name.toUpperCase().indexOf(text.toUpperCase());
if ((item.children == null || item.children.length != 0) && index != -1) {
this.autocomplete.push({
pre: item.name.slice(0, index),
post: item.name.slice(index + text.length),
full: item.name,
sindex: index,
findex: i
});
}
}
// Prefer the search term appearing as soon in the text as possible
this.autocomplete.sort(function(a, b) {
return a.sindex == b.sindex ? a.findex - b.findex : a.sindex-b.sindex;
});
}
if (this.autocomplete.length > 0) {
var search = this.selected_auto.full;
this.selected_auto = {
pre: this.autocomplete[0].pre,
post: this.autocomplete[0].post,
full: this.autocomplete[0].full,
sindex: this.autocomplete[0].sindex,
findex: this.autocomplete[0].findex,
id: 0
}; // have to break this out or you get crazy referencing issues
for (var i = 0; i < this.autocomplete.length; i++) {
if (search == this.autocomplete[i].full) {
this.selected_auto = {
pre: this.autocomplete[i].pre,
post: this.autocomplete[i].post,
full: this.autocomplete[i].full,
sindex: this.autocomplete[i].sindex,
findex: this.autocomplete[i].findex,
id: i
}; // have to break this out or you get crazy referencing issues
break;
}
}
// Move the actively selected option to the front of the list so entries are
// sorted by how close to the front of the string the search term is
if (!tabbing) {
this.autocomplete = [this.autocomplete[this.selected_auto.id]].concat(
this.autocomplete.slice(0,this.selected_auto.id),
this.autocomplete.slice(this.selected_auto.id+1));
this.selected_auto = {
pre: this.autocomplete[0].pre,
post: this.autocomplete[0].post,
full: this.autocomplete[0].full,
sindex: this.autocomplete[0].sindex,
findex: this.autocomplete[0].findex,
id: 0
}; // have to break this out or you get crazy referencing issues
}
// Update the menu selection to match
this.menu.selectionIdx = this.selected_auto.findex;
this.menu.renderMenu("", 1);
message = Ass.alpha("DDDD6E") + this.selected_auto.pre
+ Ass.alpha("00") + message + Ass.alpha("DDDD6E") + this.selected_auto.post;
} else {
this.selected_auto = {id: 0, full: ""};
}
return message;
}
DLNA_Browser.prototype.command_cd = function(text) {
var success = false;
if (text == "..") {
this.back();
success = true;
}else {
for (var i = 0; i < this.current_folder.length; i++) {
var item = this.current_folder[i];
if (text == item.name) {
success = this.select(item);
}
}
}
return success
}
DLNA_Browser.prototype.command_info = function(text) {
if (text == "..") {
return null;
}
var selection = null;
for (var i = 0; i < this.current_folder.length; i++) {
var item = this.current_folder[i];
if (text == item.name) {
selection = item;
}
}
return this.info(selection);
}
DLNA_Browser.prototype.command_ep = function(args, text) {
if (text == "..") {
return false;
}
var selection = null;
for (var i = 0; i < this.current_folder.length; i++) {
var item = this.current_folder[i];
if (text == item.name) {
selection = item;
}
}
if (!selection) {
return false;
}
var target = parseInt(args[0]);
var episode = 0;
selection.children = this.getChildren(selection);
for (var i = 0; i < selection.children.length; i++) {
this.typing_output = "Scanning: "+ selection.children[i].name + ", reached E" + episode;
this.result_displayed = false;
this.typing_action("");
var info = this.info(selection.children[i]);
if (info === null) {
return false; // can't get enough information to find the episode
} else if (isNaN(info.end)) {
continue; // Maybe need to find a better check for this
}
if (target <= episode + info.end) {
// Season based episode# target
var s_target = target - episode;
selection.children[i].children = this.getChildren(selection.children[i]);
for (var j = 0; j < selection.children[i].children.length; j++) {
this.typing_output = "Scanning: "+ selection.children[i].name + ", reached E" + episode;
this.result_displayed = false;
this.typing_action("");
episode++;
var episode_info = this.info(selection.children[i].children[j]);
if (episode_info === null) {
continue;
}
// episode contains target episode
if (episode_info.start <= s_target && s_target <= episode_info.end) {
this.select(selection.children[i]);
this.menu.selectionIdx = j;
this.menu.renderMenu("", 1);
// Make the output look nice
var E = episode_info.start;
if (E < 10) {
E = "0"+E;
}
if (episode_info.start != episode_info.end) {
if (episode_info.end < 10) {
E += "0";
}
E +="-E"+episode_info.end;
}
if (target < 10) {
target = "0"+target;
}
this.typing_output = selection.name+" E"+target+" = "+selection.children[i].name+" E"+E;
return true;
}
}
break;
}
episode += info.end;
}
if (target < 10) {
target = "0"+target;
}
this.typing_output = Ass.color("FF0000", true) + "Failed to find "+selection.name+" E"+target
return false;
}
DLNA_Browser.prototype.command_wake = function(args) {
if (args[0] === null) {
this.typing_output = Ass.color("FF0000", true) + "MAC Address cannot be null";
return;
}
var result = mp.command_native({
name: "subprocess",
playback_only: false,
capture_stdout: true,
capture_stderr: true,