home *** CD-ROM | disk | FTP | other *** search
/ mail.altrad.com / 2015.02.mail.altrad.com.tar / mail.altrad.com / TEST / vlc-2-0-5-win32.exe / lua / http / js / controlers.js < prev    next >
Text File  |  2012-12-12  |  23KB  |  532 lines

  1. var current_id = 1;
  2. var currentArt = null;
  3. var current_que = 'main';
  4. var previous_title = null;
  5. var current_title = null;
  6.  
  7. function updateArt(url) {
  8.     $('#albumArt').fadeOut(500, function () {
  9.         $(this).addClass('hidden').removeAttr('height').removeAttr('width').attr('src', url);
  10.     });
  11. }
  12.  
  13. function updateStatus() {
  14.     $.ajax({
  15.         url: 'requests/status.xml',
  16.         success: function (data, status, jqXHR) {
  17.             if (current_que == 'main') {
  18.                 $('.dynamic').empty();
  19.                 $('#mediaTitle').append($('[name="filename"]', data).text());
  20.                 $('#totalTime').append(format_time($('length', data).text()));
  21.                 $('#currentTime').append(format_time($('time', data).text()));
  22.                 if (!$('#seekSlider').data('clicked')) {
  23.                     $('#seekSlider').slider({
  24.                         value: toFloat($('position', data).text()) * 100
  25.                     });
  26.                 }
  27.                 $('#currentVolume').append(Math.round($('volume', data).text() / 2.56) + '%');
  28.                 /* Don't interfere with the user's action */
  29.                 if (!$('#volumeSlider').data('clicked')) {
  30.                     $('#volumeSlider').slider({
  31.                         value: ($('volume', data).text() / 5.12)
  32.                     });
  33.                 }
  34.                 $('#rateSlider').slider({
  35.                     value: ($('rate', data).text())
  36.                 });
  37.                 $('#currentRate').append(Math.round($('rate', data).text() * 100) / 100 + 'x');
  38.                 $('#audioSlider').slider({
  39.                     value: ($('audiodelay', data).text())
  40.                 });
  41.                 $('#currentAudioDelay').append(Math.round($('audiodelay', data).text() * 100) / 100 + 's');
  42.                 $('#subtitleSlider').slider({
  43.                     value: ($('subtitledelay', data).text())
  44.                 });
  45.                 $('#currentSubtitleDelay').append(Math.round($('subtitledelay', data).text() * 100) / 100 + 's');
  46.                 $('#seekSlider').attr('totalLength', $('length', data).text());
  47.                 $('#buttonPlay').attr('state', $('state', data).text()).attr('mrl', $('[name="filename"]', data).text());
  48.                 if ($('state', data).text() == 'playing') {
  49.                     $('#buttonPlay').removeClass('paused').addClass('playing');
  50.                 } else {
  51.                     $('#buttonPlay').removeClass('playing').addClass('paused');
  52.                 }
  53.                 if ($('random', data).text() == 'true') {
  54.                     $('#buttonShuffle').removeClass('ui-state-default').addClass('ui-state-active');
  55.                 } else {
  56.                     $('#buttonShuffle').addClass('ui-state-default').removeClass('ui-state-active');
  57.                 }
  58.                 if ($('loop', data).text() == 'true') {
  59.                     $('#buttonLoop').removeClass('ui-state-default').addClass('ui-state-active');
  60.                 } else {
  61.                     $('#buttonLoop').addClass('ui-state-default').removeClass('ui-state-active');
  62.                 }
  63.                 if ($('repeat', data).text() == 'true') {
  64.                     $('#buttonRepeat').removeClass('ui-state-default').addClass('ui-state-active');
  65.                 } else {
  66.                     $('#buttonRepeat').addClass('ui-state-default').removeClass('ui-state-active');
  67.                 }
  68.  
  69.                 if ($('[name="artwork_url"]', data).text() != currentArt && $('[name="artwork_url"]', data).text() != "") {
  70.                     var tmp = new Date();
  71.                     currentArt = $('[name="artwork_url"]', data).text();
  72.                     updateArt('/art?' + tmp.getTime());
  73.                 } else if ($('[name="artwork_url"]', data).text() == "" && currentArt != 'images/vlc-48.png') {
  74.                     currentArt = 'images/vlc-48.png';
  75.                     updateArt(currentArt);
  76.                 }
  77.  
  78.                 current_title = $('[name="filename"]', data).text();
  79.                 if (previous_title != current_title) {
  80.                     updatePlayList();
  81.                 }
  82.                 previous_title = current_title;
  83.  
  84.                 if (pollStatus) {
  85.                     setTimeout(updateStatus, 1000);
  86.                 }
  87.  
  88.             }
  89.             $('band', data).each(function () {
  90.                 var id = $(this).attr('id');
  91.                 var value = $(this).text() ? $(this).text() : 0;
  92.                 var freq = ["60 Hz","170 Hz", "310 Hz", "600 Hz", "1 kHz","3 kHz", "6 kHz", "12 kHz" , "14 kHz" , "16 kHz" ];
  93.                 if (!$('#eq_container' + id).length) {
  94.                     $('#window_equalizer').append('<div style="float:left;width:44px;" align="center" id="eq_container' + id + '"><div id="eq' + id + '_txt">' + value + 'dB</div><div class="eqBand" id="eq' + id + '" style="font-size: 18px;"></div><div>' + freq[id] + '</div></div>');
  95.                     $('#eq' + id).slider({
  96.                         min: -20,
  97.                         max: 20,
  98.                         step: 0.1,
  99.                         range: "min",
  100.                         value: value,
  101.                         animate: true,
  102.                         orientation: "vertical",
  103.                         stop: function (event, ui) {
  104.                             $('#' + $(this).attr('id') + '_txt').empty().append(ui.value + 'dB');
  105.                             sendCommand({
  106.                                 command: 'equalizer',
  107.                                 val: ui.value,
  108.                                 band: $(this).attr('id').substr(2)
  109.                             })
  110.                         },
  111.                         slide: function (event, ui) {
  112.                             $('#' + $(this).attr('id') + '_txt').empty().append(ui.value + 'dB');
  113.                         }
  114.                     });
  115.                 } else {
  116.                     $('#eq' + id).slider({
  117.                         value: value
  118.                     });
  119.                     $('#eq' + id + '_txt').empty().append(Math.round(value * 100) / 100 + 'dB');
  120.                 }
  121.             });
  122.             $('#preamp').slider('value', $('preamp', data).text());
  123.             $('#preamp_txt').empty().append(Math.round($('preamp', data).text() * 100) / 100 + 'dB');
  124.         },
  125.         error: function (jqXHR, status, error) {
  126.             setTimeout(updateStatus, 500);
  127.         }
  128.     });
  129. }
  130.  
  131. function updatePlayList(force_refresh) {
  132.     if (force_refresh) {
  133.         //refresh playlist..
  134.         $('#libraryTree').jstree('refresh', -1);
  135.     } else {
  136.         //iterate through playlist..
  137.         var match = false;
  138.         $('.jstree-leaf').each(function(){
  139.             var id = $(this).attr('id');
  140.             if (id != null && id.substr(0,5) == 'plid_') {
  141.                 var name = $(this).attr('name');
  142.                 if (name != null && name == current_title) {
  143.                     $(this).addClass('ui-state-highlight');
  144.                     $(this).attr('current', 'current');
  145.                     this.scrollIntoView(true);
  146.                     match = true;
  147.                 } else {
  148.                     $(this).removeClass('ui-state-highlight');
  149.                     $(this).removeAttr('current');
  150.                 }
  151.                 if ($(this).children('a').size() > 0) {
  152.                     $($(this).children('a')[0]).removeClass('ui-state-active');
  153.                 }
  154.             }
  155.         });
  156.         //local title wasn't found - refresh playlist..
  157.         if (!match) updatePlayList(true);
  158.     }
  159. }
  160.  
  161. function sendCommand(params, append) {
  162.     if (current_que == 'stream') {
  163.         $.ajax({
  164.             url: 'requests/status.xml',
  165.             data: params,
  166.             success: function (data, status, jqXHR) {
  167.                 if (append != undefined) {
  168.                     eval(append);
  169.                 }
  170.                 updateStatus();
  171.             }
  172.         });
  173.     } else {
  174.         if (params.plreload === false) {
  175.             $.ajax({
  176.                 url: 'requests/status.xml',
  177.                 data: params,
  178.                 success: function (data, status, jqXHR) {
  179.                     if (append != undefined) {
  180.                         eval(append);
  181.                     }
  182.                 }
  183.             });
  184.         } else {
  185.             $.ajax({
  186.                 url: 'requests/status.xml',
  187.                 data: params,
  188.                 success: function (data, status, jqXHR) {
  189.                     if (append != undefined) {
  190.                         eval(append);
  191.                     }
  192.                 }
  193.             });
  194.         }
  195.     }
  196. }
  197.  
  198. function browse(dir) {
  199.     dir = dir == undefined ? 'file://~' : dir;
  200.     $.ajax({
  201.         url: 'requests/browse.xml',
  202.         data: 'uri=' + encodeURIComponent(dir),
  203.         success: function (data, status, jqXHR) {
  204.             var tgt = browse_target.indexOf('__') == -1 ? browse_target : browse_target.substr(0, browse_target.indexOf('__'));
  205.             $('#browse_elements').empty();
  206.             $('element', data).each(function () {
  207.                 var ext = $(this).attr('name').substr($(this).attr('name').lastIndexOf('.') + 1).toLowerCase();
  208.                 if ($(this).attr('type') == 'dir' || $.inArray(ext, video_types) != -1 || $.inArray(ext, audio_types) != -1 || $.inArray(ext, playlist_types) != -1) {
  209.                     $('#browse_elements').append(createElementLi($(this).attr('name'), $(this).attr('type'), $(this).attr('uri'), ext));
  210.                 }
  211.             });
  212.             $('[opendir]').dblclick(function () {
  213.                 browse($(this).attr('opendir'));
  214.             });
  215.             $('[openfile]').dblclick(function () {
  216.                 switch (tgt) {
  217.                 case '#stream_input':
  218.                     $(browse_target).val($(this).attr('openfile'));
  219.                     break;
  220.                 case '#mosaic_open':
  221.                     $('li', browse_target).remove();
  222.                     $(browse_target).append(this);
  223.                     $(this).css({
  224.                         'margin-left': -40,
  225.                         'margin-top': -46,
  226.                         'float': 'left'
  227.                     });
  228.                     break;
  229.                 case '#mobile':
  230.                     break;
  231.                 default:
  232.                     sendCommand('command=in_play&input=' + encodeURIComponent($(this).attr('openfile')));
  233.                     updatePlayList(true);
  234.                     break;
  235.                 }
  236.                 $('#window_browse').dialog('close');
  237.             });
  238.             $('[opendir]').click(function () {
  239.                 switch (tgt) {
  240.                 case '#mobile':
  241.                     browse($(this).attr('opendir'));
  242.                     break;
  243.                 default:
  244.                     break;
  245.                 }
  246.             });
  247.             $('[openfile]').click(function () {
  248.                 switch (tgt) {
  249.                 case '#mobile':
  250.                     sendCommand('command=in_play&input=' + encodeURIComponent($(this).attr('openfile')), "window.location='mobile.html'");
  251.                     break;
  252.                 default:
  253.                     break;
  254.                 }
  255.             });
  256.             switch (tgt) {
  257.             case '#mobile':
  258.                 break;
  259.             default:
  260.                 $('[selectable]').selectable();
  261.                 break;
  262.             }
  263.         },
  264.         error: function (jqXHR, status, error) {
  265.             setTimeout('browse("' + dir + '")', 1041);
  266.         }
  267.     });
  268. }
  269.  
  270. function updateStreams() {
  271.     $.ajax({
  272.         url: 'requests/vlm.xml',
  273.         success: function (data, status, jqXHR) {
  274.             $('#stream_info').accordion("destroy");
  275.             $('#stream_info').empty();
  276.             $('broadcast', data).each(function () {
  277.                 var stream_div = $('#stream_status_').clone();
  278.                 var name = $(this).attr('name');
  279.                 var loop = $(this).attr('loop') == 'yes';
  280.                 var playing = $('instance', $(this)).attr('state') == 'playing';
  281.                 var file = $('input', $(this)).text();
  282.                 var output = $('output', $(this)).text();
  283.                 var time = isNaN(Math.round($('instance', $(this)).attr('time') / 1000000)) ? 0 : Math.round($('instance', $(this)).attr('time') / 1000000);
  284.                 var length = isNaN(Math.round($('instance', $(this)).attr('length') / 1000000)) ? 0 : Math.round($('instance', $(this)).attr('length') / 1000000);
  285.                 $('[id]', stream_div).each(function () {
  286.                     $(this).attr('id', $(this).attr('id') + name);
  287.                 });
  288.                 $(stream_div).attr('id', $(stream_div).attr('id') + name);
  289.                 $('#stream_title_' + name, stream_div).append(name);
  290.                 $('#stream_file_' + name, stream_div).append(file);
  291.                 $('#stream_pos_' + name, stream_div).slider({
  292.                     value: 0,
  293.                     range: "min",
  294.                     min: 0,
  295.                     slide: function (event, ui) {
  296.                         $("#stream_current_time_" + name, stream_div).empty();
  297.                         $("#stream_current_time_" + name, stream_div).append(format_time(ui.value));
  298.                         $("#stream_total_time_" + name, stream_div).empty();
  299.                         $("#stream_total_time_" + name, stream_div).append(format_time($('#stream_pos_' + name, stream_div).slider('option', 'max')));
  300.                         sendVLMCmd('control ' + name + ' seek ' + Math.round(ui.value / $('#stream_pos_' + name, stream_div).slider('option', 'max') * 100));
  301.                     },
  302.                     change: function (event, ui) {
  303.                         $("#stream_current_time_" + name, stream_div).empty();
  304.                         $("#stream_current_time_" + name, stream_div).append(format_time(ui.value));
  305.                         $("#stream_total_time_" + name, stream_div).empty();
  306.                         $("#stream_total_time_" + name, stream_div).append(format_time($('#stream_pos_' + name, stream_div).slider('option', 'max')));
  307.                     }
  308.                 });
  309.                 $('#button_stream_stop_' + name, stream_div).click(function () {
  310.                     sendVLMCmd('control ' + name + ' stop');
  311.                     return false;
  312.                 });
  313.                 $('#button_stream_play_' + name, stream_div).click(function () {
  314.                     if ($('span', this).hasClass('ui-icon-pause')) {
  315.                         sendVLMCmd('control ' + name + ' pause');
  316.                     } else {
  317.                         sendVLMCmd('control ' + name + ' play');
  318.                     }
  319.                 });
  320.                 $('#button_stream_loop_' + name, stream_div).click(function () {
  321.                     if (loop) {
  322.                         sendVLMCmd('setup ' + name + ' unloop');
  323.                     } else {
  324.                         sendVLMCmd('setup ' + name + ' loop');
  325.                     }
  326.                 });
  327.                 $('#button_stream_delete_' + name, stream_div).click(function () {
  328.                     sendVLMCmd('del ' + name);
  329.                 });
  330.                 $('#stream_pos_' + name, stream_div).slider({
  331.                     max: length,
  332.                     value: time
  333.                 });
  334.                 if (playing) {
  335.                     $('span', $('#button_stream_play_' + name, stream_div)).removeClass('ui-icon-play');
  336.                     $('span', $('#button_stream_play_' + name, stream_div)).addClass('ui-icon-pause');
  337.                 }
  338.                 if (loop) {
  339.                     $('#button_stream_loop_' + name, stream_div).addClass('ui-state-active');
  340.                 }
  341.                 $(stream_div).css({
  342.                     'visibility': '',
  343.                     'display': ''
  344.                 });
  345.                 $('#stream_info').append(stream_div);
  346.  
  347.             });
  348.             $('.button').hover(
  349.  
  350.             function () {
  351.                 $(this).addClass('ui-state-hover');
  352.             }, function () {
  353.                 $(this).removeClass('ui-state-hover');
  354.             });
  355.             $('#stream_info').accordion({
  356.                 header: "h3",
  357.                 collapsible: true,
  358.                 autoHeight: true
  359.             });
  360.             if (current_que == 'stream') {
  361.                 $('.dynamic').empty();
  362.                 $('#mediaTitle').append($('[name="Current"] input', data).text());
  363.                 $('#totalTime').append(format_time(isNaN($('[name="Current"] instance', data).attr('length')) ? 0 : $('[name="Current"] instance', data).attr('length') / 1000000));
  364.                 $('#currentTime').append(format_time(isNaN($('[name="Current"] instance', data).attr('time')) ? 0 : $('[name="Current"] instance', data).attr('time') / 1000000));
  365.                 $('#seekSlider').slider({
  366.                     value: (($('[name="Current"] instance', data).attr('time') / 1000000) / ($('[name="Current"] instance', data).attr('length') / 1000000) * 100)
  367.                 });
  368.                 $('#seekSlider').attr('totalLength', $('[name="Current"] instance', data).attr('length') / 1000000);
  369.                 $('#buttonPlay').attr('state', $('[name="Current"] instance', data).length > 0 ? $('[name="Current"] instance', data).attr('state') : 'stopped');
  370.                 if ($('[name="Current"] instance', data).attr('state') == 'playing') {
  371.                     $('#buttonPlay').removeClass('paused');
  372.                     $('#buttonPlay').addClass('playing');
  373.                 } else {
  374.                     $('#buttonPlay').removeClass('playing');
  375.                     $('#buttonPlay').addClass('paused');
  376.                 }
  377.                 setTimeout(updateStreams, 1000);
  378.             }
  379.         }
  380.     });
  381. }
  382.  
  383. function updateEQ() {
  384.     $.ajax({
  385.         url: 'requests/status.xml',
  386.         success: function (data, status, jqXHR) {
  387.             $('band', data).each(function () {
  388.                 var freq = ["60 Hz","170 Hz", "310 Hz", "600 Hz", "1 kHz","3 kHz", "6 kHz", "12 kHz" , "14 kHz" , "16 kHz" ];
  389.                 var id = $(this).attr('id');
  390.                 var value = $(this).text() ? $(this).text() : 0;
  391.                 if (!$('#eq_container' + id).length) {
  392.                     $('#window_equalizer').append('<div style="float:left;width:44px;" align="center" id="eq_container' + id + '"><div id="eq' + id + '_txt">' + value + 'dB</div><div class="eqBand" id="eq' + id + '" style="font-size: 18px;"></div><div>' + freq[id] + '</div></div>');
  393.                     $('#eq' + id).slider({
  394.                         min: -20,
  395.                         max: 20,
  396.                         step: 0.1,
  397.                         range: "min",
  398.                         value: value,
  399.                         animate: true,
  400.                         orientation: "vertical",
  401.                         stop: function (event, ui) {
  402.                             $('#' + $(this).attr('id') + '_txt').empty().append(ui.value + 'dB');
  403.                             sendEQCmd({
  404.                                 command: 'equalizer',
  405.                                 val: ui.value,
  406.                                 band: $(this).attr('id').substr(2)
  407.                             })
  408.                         },
  409.                         slide: function (event, ui) {
  410.                             $('#' + $(this).attr('id') + '_txt').empty().append(ui.value + 'dB');
  411.                         }
  412.                     });
  413.                 } else {
  414.                     $('#eq' + id).slider({
  415.                         value: value
  416.                     });
  417.                     $('#eq' + id + '_txt').empty().append(Math.round(value * 100) / 100 + 'dB');
  418.                 }
  419.             });
  420.             $('#preamp').slider('value', $('preamp', data).text());
  421.             $('#preamp_txt').empty().append(Math.round($('preamp', data).text() * 100) / 100 + 'dB');
  422.         }
  423.     })
  424. }
  425.  
  426. function sendVLMCmd(command, append) {
  427.     var commands = command.split(';');
  428.     if (commands.length > 1) {
  429.         sendBatchVLMCmd(command, append);
  430.     } else {
  431.         if (current_que == 'main') {
  432.             $.ajax({
  433.                 url: 'requests/vlm_cmd.xml',
  434.                 data: 'command=' + encodeURIComponent(command),
  435.                 success: function (data, status, jqXHR) {
  436.                     if ($('error', data).text()) {
  437.                         $('#error_container').append('<div>' + $('error', data).text() + '</div>');
  438.                         $('#window_error').dialog('open');
  439.                     }
  440.                     if (append != undefined) {
  441.                         eval(append);
  442.                     }
  443.                     updateStreams();
  444.                 }
  445.             });
  446.         } else {
  447.             $.ajax({
  448.                 url: 'requests/vlm_cmd.xml',
  449.                 data: 'command=' + encodeURIComponent(command),
  450.                 success: function (data, status, jqXHR) {
  451.                     if ($('error', data).text()) {
  452.                         $('#error_container').append('<div>' + $('error', data).text() + '</div>');
  453.                         $('#window_error').dialog('open');
  454.                     }
  455.                     updateStreams();
  456.                 }
  457.             });
  458.         }
  459.     }
  460. }
  461.  
  462. function sendBatchVLMCmd(command, append) {
  463.     var commands = command.split(';');
  464.     $.ajax({
  465.         url: 'requests/vlm_cmd.xml',
  466.         data: 'command=' + encodeURIComponent(commands.shift()),
  467.         success: function (data, status, jqXHR) {
  468.             if ($('error', data).text()) {
  469.                 $('#error_container').append('<div>' + $('error', data).text() + '</div>');
  470.                 $('#window_error').dialog('open');
  471.             }
  472.             sendVLMCmd(commands.join(';'), append);
  473.         }
  474.     });
  475. }
  476.  
  477. function sendEQCmd(params) {
  478.     $.ajax({
  479.         url: 'requests/status.xml',
  480.         data: params,
  481.         success: function (data, status, jqXHR) {
  482.             updateEQ();
  483.         }
  484.     });
  485. }
  486.  
  487. $(function () {
  488.     $('#albumArt').load(function () {
  489.         var width = $(this).width();
  490.         var height = $(this).height();
  491.         var max = Math.max(width, height);
  492.         if (max > 150) {
  493.             var ratio = 150 / max;
  494.             width = Math.floor(width * ratio);
  495.             height = Math.floor(height * ratio);
  496.         }
  497.         $(this).attr('width', width).attr('height', height).css('margin-left', Math.floor((150 - width) / 2)).css('margin-top', Math.floor((150 - height) / 2)).removeClass('hidden').fadeIn();
  498.     });
  499.     $('#libraryTree').jstree({
  500.         "xml_data": {
  501.             "ajax": {
  502.                 "url": "requests/playlist_jstree.xml"
  503.             },
  504.             "xsl": "nest"
  505.         },
  506.         "themeroller": {
  507.             "item_leaf": "ui-icon-video"
  508.         },
  509.         "core": {
  510.             "initially_open": ["plid_1", "plid_2", "plid_3"]
  511.         },
  512.         "plugins": ["xml_data", "ui", "themeroller"]
  513.     }).bind("loaded.jstree", function (event, data) {
  514.         $('[current]', '[id^="plid_"]').each(function () {
  515.             $(this).addClass('ui-state-highlight');
  516.             current_id = $(this).attr('id').substr(5);
  517.         });
  518.     }).bind("refresh.jstree", function (event, data) {
  519.         $('[current]', '[id^="plid_"]').each(function () {
  520.             $(this).addClass('ui-state-highlight');
  521.             current_id = $(this).attr('id').substr(5);
  522.         });
  523.     }).delegate("#plid_2 li.jstree-leaf a", "click", function (event, data) {
  524.         event.preventDefault();
  525.         current_id = $(this).parent().attr('id').substr(5);
  526.         sendCommand('command=pl_play&id=' + current_id);
  527.     });
  528.     updateStatus();
  529.     updateStreams();
  530.     updateEQ();
  531. });
  532.