css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>Delete Player</h1>
<section id="accountinput" class="bcls-section">
<h2>Account input</h2>

<p>Account ID: <input id="account_id_input" type="text" size="40" name="account_id" /></p>

<p>Client ID: <input id="client_id_input" type="text" size="100" name="account_username" /></p>

<p>Client Secret: <input id="client_secret_input" type="password" size="40" name="account_password" /></p>

<p><button id="get_players" class="bcls-button">Display Players</button></p>
</section>

<section id="jsonText" class="bcls-section">
<h2>JSON response</h2>

<textarea id="api_response" class="bcls-code">Response will appear here...</textarea>
</section>

<section id="processedResponse" class="bcls-section">
<h2>Processed response</h2>
<p>Below is simple HTML output of some of the player information. Select a player to delete it.</p>
<p>
  <button class="bcls-button" id="show_more_players">Show Next Set of Players</button>
</p>

<div id="player_list" class="inline-div"></div>
<div id="player_details" class="inline-div"></div>

<p><button class="bcls-button" id="delete_player">Delete Selected Player
</button></p>
</section>

            
          
!
            
              /* * The body style is just for the
 * background color of the codepen.
 * Do not include in your code.
 */
body {
  background-color: #111;
  color: #f5f5f5;
  font-family: sans-serif;
  margin: 2em;
}
/*
 * Styles essential to the sample
 * are below
 */
.bcls-code {
  color: #111;
  font-family: 'Source Code Pro',monospace;
  padding: 1em;
  width: 90%;
  min-height: 5em;
  background-color: #cfcfcf;
  font-size: 1rem;
}
.bcls-button {
  padding: .5em;
  background-color: #333;
  border: 1px yellow solid;
  border-radius: .5em;
  color: yellow;
  cursor: pointer;
}
fieldset {
  color: #64AAB2;
  border: 1px #64AAB2 solid;
  border-radius: .5em;
}
code {
  color: #E4B842;
  font-family: 'Source Code Pro',monospace;
}
.inline-div {
  display: inline-block;
}
            
          
!
            
              var BCLS = ( function (window, document) {
  // account id value is the default Brightcove Learning account
  var account_id = '1752604059001',
    account_id_input = document.getElementById('account_id_input'),
    client_id_input = document.getElementById('client_id_input'),
    client_secrect_input = document.getElementById('client_secrect_input'),
    api_response = document.getElementById('api_response'),
    player_embedded = document.getElementById('player_embedded'),
    get_players = document.getElementById('get_players'),
    show_more_players = document.getElementById('show_more_players'),
    radioGroup = document.getElementsByName('playerList'),
    player_list = document.getElementById('player_list'),
    delete_player = document.getElementById('delete_player'),
    playerCount = 0,
    nextPlayerStart = 0,
    players,
    playerToDelete;
    // event handlers
    get_players.addEventListener('click', function() {
      createRequest('getPlayers');
    });

    show_more_players.addEventListener('click', function() {
      // show players in groups of 3 (the number can be whatever you like)
      showNextPlayerList();
    });

  /**
   * get value of a selected radio buttom
   * @param {htmlElementCollection} rgroup the collection of radio buttom elements
   */
  function getRadioValue(rgroup) {
    var i = 0,
    iMax = rgroup.length;
    for (i; i < iMax; i++) {
        if (rgroup[i].checked) {
            return rgroup[i].value;
        }
    }
  }

  /**
   * find index of an object in array of objects
   * based on some property value
   *
   * @param {array} targetArray array to search
   * @param {string} objProperty object property to search
   * @param {string} value of the property to search for
   * @return {integer} index of first instance if found, otherwise returns -1
  */
  function findObjectInArray(targetArray, objProperty, value) {
      var i, totalItems = targetArray.length, objFound = false;
      for (i = 0; i < totalItems; i++) {
          if (targetArray[i][objProperty] === value) {
              objFound = true;
              return i;
          }
      }
      if (objFound === false) {
          return -1;
      }
  }

  /**
   * inject a list of 3 players into the page as a radio button group
   */
  function showNextPlayerList() {
    var radio,
      label,
      br,
      frag = document.createDocumentFragment(),
      i,
      iMax;
    // clear player list div
    player_list.innerHTML = '';
    // make sure count doesn't exceed number of players
    if (playerCount > 0) {
      if ((playerCount - nextPlayerStart) < 5) {
        iMax = playerCount - nextPlayerStart;
      } else {
        iMax = nextPlayerStart + 5;
      }
    } else {
      iMax = nextPlayerStart + 5;
    }
    for (i = nextPlayerStart; i < iMax; i++) {
      // create the elements, add to doc fragment
      radio = document.createElement('input');
      radio.setAttribute('type', 'radio');
      radio.setAttribute('name', 'playerList');
      radio.setAttribute('value', players[i].id);
      radio.setAttribute('id', players[i].id);
      label = document.createElement('label');
      label.setAttribute('for', players[i].id);
      label.textContent = ' ' + players[i].name;
      br = document.createElement('br');
      frag.appendChild(radio);
      frag.appendChild(label);
      frag.appendChild(br);
    }
    // inject fragment
    player_list.appendChild(frag);
    // set the next starting count
    nextPlayerStart = nextPlayerStart + 5;
    // note that because getElementsByName gets a dynamic collection,
    // the new elements are automatically added to it
    iMax = radioGroup.length;
    // set event listeners on the new set of radio buttons
    for (i = 0; i < iMax; i++) {
      radioGroup[i].addEventListener('change', function() {
        playerToDelete = getRadioValue(radioGroup);
        createRequest('deletePlayer');
      });
    }
  }

  /**
   * createRequest sets up requests, send them to makeRequest(), and handles responses
   * @param  {string} type the request type
   */
  function createRequest(type) {
    var options   = {},
      requestBody = {},
      proxyURL = 'https://solutions.brightcove.com/bcls/bcls-proxy/doc-samples-proxy-v2.php',
      baseURL = 'https://players.api.brightcove.com/v1/accounts/',
      endpoint,
      responseDecoded;
    // the proxy alreay has credentials for the default Brightcove Learning account
    // so no need to send them
    if (client_id_input.value.length > 0 && client_secrect_input.value.length > 0) {
      options.client_id     = client_id_input.value;
      options.client_secret = client_secrect_input.value;
    }

    options.account_id = (account_id_input.value.length > 0) ? account_id_input.value : account_id;
    options.proxyURL = proxyURL;

    switch (type) {
        case 'getPlayers':
          endpoint            = '/' + options.account_id + '/players';
          options.url         = baseURL + endpoint;
          options.requestType = 'GET';
          makeRequest(options, function(response) {
              responseDecoded = JSON.parse(response);
              // save the players array
              players = responseDecoded.items;
              // save the total player count
              playerCount = responseDecoded.item_count;
              api_response.textContent = JSON.stringify(responseDecoded, null, 2);
              // show the first 3 players - note that the API request
              // returns them in ascending order by date created,
              // so if you wanted to show the newest players first
              // you would start with the last item instead of the first
              showNextPlayerList();
          });
          break;

        case 'deletePlayer':
          endpoint            = '/' + options.account_id + '/players/' + playerToDelete;
          options.url         = baseURL + endpoint;
          options.requestType = 'DELETE';
          api_response.textContent = 'We are not actually going to delete player ' + playerToDelete + ' here, as that is an irreversible action. If you need to create an app that really deletes players, you will find the code in the JavaScript source code here, commented out.'

          /**
           * If you really wanted to delete the player
           * the lines commented out below would do it
           */
          // makeRequest(options, function(response) {
          //     responseDecoded = JSON.parse(response);
          //     api_response.textContent = JSON.stringify(responseDecoded, null, 2);
          //     showNextPlayerList();
          // });
          break;

        // additional cases
        default:
          console.log('Should not be getting to the default case - bad request type sent');
          break;
    }
  }

  /**
   * send API request to the proxy
   * @param {Object} options for the request
   * @param {String} options.url the full API request URL
   * @param {String = "GET", "POST", "PATCH", "PUT", "DELETE"} requestData[options.requestType = "GET"] HTTP type for the request
   * @param {String} options.proxyURL proxyURL to send the request to
   * @param {String} options.client_id client id for the account( default is in the proxy)
   * @param {String} options.client_secret client secret for the account(default is in the proxy)
   * @param {JSON} [options.requestBody] Data to be sent in the request body in the form of a JSON string
   *
     @param {Function} [callback] callback function that will process the response
   */

   function makeRequest(options, callback) {
     var httpRequest = new XMLHttpRequest(),
       response,
       requestParams,
       dataString,
       proxyURL = options.proxyURL,
       // response handler
       getResponse = function() {
         try {
           if (httpRequest.readyState === 4) {
             // any response status code in the 200 range indicates success
             if (httpRequest.status >= 200 && httpRequest.status < 300) {
               response = httpRequest.responseText;
               // return the response
               callback(response);
             } else {
               alert('There was a problem with the request. Request returned ' + httpRequest.status);
             }
           }
         } catch (e) {
           alert('Caught Exception: ' + e);
         }
       };
     /**
      * set up request data
      * the proxy used here takes the following request body:
      * JSON.strinify(options)
      */
     // set response handler
     httpRequest.onreadystatechange = getResponse;
     // open the request
     httpRequest.open('POST', proxyURL);
     // set headers if there is a set header line, remove it
     // open and send request
     httpRequest.send(JSON.stringify(options));
   }

})(window, document);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console