PageRenderTime 23ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/ffmpeg_wrapper.js

https://gitlab.com/vozmob/ffmpeg_wrapper
JavaScript | 115 lines | 55 code | 17 blank | 43 comment | 17 complexity | b5d4a07f85368c2a4fa4bc63e8b9e743 MD5 | raw file
  1
  2/**
  3 * @param string $prefix is the prefix of the elements
  4 * @param string $source is the source for the output type
  5 */
  6function ffmpeg_wrapper_update_options(prefix, source) {
  7
  8  // get the path to the output settings set in ffmpeg_wrapper module
  9  var path = Drupal.settings.ffmpeg_wrapper.ffmpeg_wrapper_output_url;
 10  
 11  // get the output type
 12  var output = $('#'+prefix+source).val();
 13  
 14  // only look for the value if there is one selected
 15  if (output != 0) {
 16    // now get the values for this codec
 17    $.getJSON(path+output, function(json) {
 18    
 19      // now we need to itterate through each of the keys returned and 
 20      // limit the choices to the incoming values.
 21      var data = eval (json);
 22      
 23      // first break the configuration into the types of config data we have
 24      for (var type in data) {
 25        // catch the flag for default settings and don't do 
 26        // any form updating for this value
 27        if (data[type] != 'default') {
 28          // now get each of the items in the config
 29          // types here are 'audio' and 'video' 
 30          for (var key in data[type] ) {
 31            // build the element from the prefix value:  ffmpeg & the kind of item it is & the key 
 32            var element = '#'+prefix+'ffmpeg-'+type+'-'+key;
 33            // make sure element exists
 34            if ($(element)) {
 35              // remove existing html from this select box
 36              $(element).html('');
 37              var html = '';
 38             
 39              // break each of the options out for each select box
 40              for (var option in data[type][key]) {
 41              
 42                // create the description for this element 
 43                var description = data[type][key][option].valueOf();              
 44                                
 45                // now we check to see if the option coming in contains
 46                // the default value for this configuration so we know to use it as default
 47                // make a new variable to properly type cast the value
 48                var test = ''+data[type][key][option]+'';
 49           
 50                // search this string for the "default" text and save it for 
 51                // use after the select is rebuilt
 52                if (test.match(Drupal.settings.ffmpeg_wrapper.default_string) ) { 
 53                  var selected = option;                 
 54                }
 55                
 56                // now build the html for the select options
 57                html += ffmpeg_wrapper_select_builder(option, description);
 58              }
 59               
 60              // build the new select element 
 61              $(element).html(html);
 62               
 63              // now update the selected value
 64              if (selected) {
 65                $(element).val(selected);
 66                selected = null;
 67              }
 68               
 69            }
 70          }
 71          // now turn on the advanced options so they are picked up
 72          $('#'+prefix+'ffmpeg-'+type+'-advanced').attr('checked', true);
 73        }            
 74        else {
 75          // is the configuration data being passed back was the default data?
 76          // turn off the advanced configuration so that nothing nasty 
 77          // happens without user action
 78          $('#'+prefix+'ffmpeg-audio-advanced').attr('checked', false);
 79          $('#'+prefix+'ffmpeg-video-advanced').attr('checked', false);        
 80        }
 81      }
 82    });
 83  }
 84}
 85
 86/**
 87 * this builds the html for the output of the selects
 88 * simple function but makes code easier
 89 * @param string $value is the key value
 90 * @param string $description is the text value
 91 */
 92 function ffmpeg_wrapper_select_builder(value, description) {
 93   if (selected) { var selected = 'selected'; }
 94   else { var selected = ''; }   
 95   return '<option value="'+value+'">'+description+'</option>';
 96 }
 97 
 98 
 99 /**
100  * This shows and hides the frame size other option in the 
101  * video options dropdown
102  */
103 if (Drupal.jsEnabled) {
104  $(document).ready(function () {
105    // get the video size element      
106    $('#edit-ffmpeg-video-size').bind('change', function () {
107      if ($(this).val() == 'other'){
108        $('#edit-ffmpeg-video-size-other').show('slow');
109       }
110       else {
111         $('#edit-ffmpeg-video-size-other').hide('slow');
112       }
113     });
114  });
115}