123

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.

            
              <section class="bcls-section">
<h2 id="accountInputs">Video Search App</h2>

<fieldset class="bcls-fieldset"><legend>Inputs</legend>

<p>Account ID: <input id="account_id" type="text" value="" data-bind="value: account_id" /></p>

<p>Client ID: <input id="client_id" type="text" value="" data-bind="value: client_id" /></p>

<p>Client Secret: <input id="client_secret" type="text" value="" data-bind="value: client_secret" /></p>

<p>Proxy URL: <input id="proxyURL" type="text" data-bind="value: proxyURL" /></p>
</fieldset>
</section>

<section class="bcls-section">
<h2 id="searchInput">Search Inputs</h2>

<fieldset id="searchInputs" data-bind="enable: haveAccountInputs"><legend>Search Inputs</legend>

<p><input type="checkbox" id="exactMatch" data-bind="checked: exactMatch" /> Require exact match on search terms</p>

<p><input type="checkbox" id="requiredTerms" data-bind="checked: requiredTerms" /> Only return videos that have <strong>all</strong> the search terms</p>

<p><input type="checkbox" id="excludeTerms" data-bind="checked: excludeTerms" /> Only return videos that do <strong>not</strong> have the search terms</p>

<p><input type="checkbox" id="searchComplete" data-bind="checked: searchComplete" /><strong>Only</strong> return videos that have been completely processed</p>

<p>Limit search to this field: <select id="searchField" data-bind="disable: haveCustomField(), options: searchField, value: selectedSearchField, optionsCaption: 'Any'"></select></p>

<p>Limit search to this custom field: <input type="text" id="customField" data-bind="value: customField, disable: haveSearchField" /></p>

<div data-bind="visible: !haveSearchState() &amp;&amp; !havePlayable()">Search terms: <input id="searchTerms" type="text" data-bind="value: searchTerms" /></div>

<div data-bind="visible: haveSearchState">State: <select id="searchStates" data-bind="options: searchState, value: selectedSearchState"></select></div>

<div data-bind="visible: havePlayable">State: <select id="searchPlayable" data-bind="options: searchPlayable, value: selectedSearchState"></select></div>

<table class="bcls-table">
	<caption>Limit search by dates:</caption>
	<tbody>
		<tr>
			<td>Date type</td>
			<td>From date</td>
			<td>To date</td>
		</tr>
		<tr>
			<td><select class="date" id="dateRangeType" data-bind="options: dateRangeType, value: selectedDateRangeType, optionsCaption: 'Do not limit by date'"></select></td>
			<td><input id="fromDate" class="date" data-bind="enable: haveDateType" /></td>
			<td><input id="toDate" class="date" data-bind="enable: haveDateType" /></td>
		</tr>
	</tbody>
</table>

<p>Items found:<br />
Offset (number of items to skip): <select id="offset" data-bind="options: offsets, value: selectedOffset"></select></p>

<p>Number of items to return: <select id="limit" data-bind="options: limit, value: selectedLimit"></select></p>

<p>Sort results by: <select id="sort" data-bind="options: sortOptions, value: selectedSortOption"></select></p>

<p><button class="bcls-button" id="submitRequest" data-bind="click: setupURL, enable: haveSearchTermsOrDateRange">Submit Request</button></p>
</fieldset>
</section>

<section class="bcls-section">
<h2 id="outputLog">Results</h2>

<fieldset class="bcls-fieldset"><legend>Outputs</legend>

<p>Note that if you request multiple result sets using the offset, new items will be appended to the existing ones.</p>

<p>API request:</p>
<textarea id="apiURL" class="bcls-code">API request will appear here...</textarea>

<h3>Response</h3>

<pre>
<code id="response" class="language-json">Response will appear here...</code>
</pre>
</fieldset>
</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: 2.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;
}
.rd-container-attachment {
  color: #333;
}
code {
  color: #E4B842;
  font-family: 'Source Code Pro',monospace;
}
            
          
!
            
              // knockout view model
function cmsSearchModel() {
        var self = this,
            i, iMax, item, itemsArray, termsArray,
            APIresponse = document.getElementById("response");
        // non-editable data
        // date pickers
        rome(fromDate);
        rome(toDate);
        self.apiURL = document.getElementById("apiURL");
        self.searchField = ["name", "text", "tags", "reference_id", "custom_fields", "state", "playable"];
        self.searchState = ["ACTIVE", "INACTIVE", "PENDING"];
        self.searchPlayable = ["true", "false"];
        self.limit = [10, 25, 50, 100];
        self.dateRangeType = ["updated_at", "created_at", "published_at", "schedule.starts_at", "schedule.ends_at"];
        self.sortOptions = ["name", "reference_id", "created_at", "published_at", "updated_at", "schedule_starts_at", "schedule_ends_at", "state", "plays_total", "plays_trailing_week"];
        self.proxyURL = ko.observable("https://solutions.brightcove.com/bcls/bcls-proxy/bcls-proxy-v2.php");

        // editable data
        self.client_id = ko.observable("");
        self.client_secret = ko.observable("");
        self.account_id = ko.observable("");
        self.exactMatch = ko.observable(false);
        self.requiredTerms = ko.observable(false);
        self.excludeTerms = ko.observable(false);
        self.selectedSearchState = ko.observable();
        self.selectedSearchField = ko.observable();
        self.selectedDateRangeType = ko.observable();
        self.searchComplete = ko.observable(true);
        self.customField = ko.observable("");
        self.searchTerms = ko.observable("");
        self.selectedLimit = ko.observable(10);
        self.itemCount = ko.observable("");
        self.offsets = ko.observableArray([0]);
        self.selectedOffset = ko.observable();
        self.selectedSortOption = ko.observable();
        // computed data
        self.haveAccountInputs = ko.computed(function() {
            if (self.account_id() === "" || self.client_id() === "" || self.client_secret() === "") {
                return false;
            } else {
                return true;
            }
        });
        self.haveSearchState = ko.computed(function() {
            if (self.selectedSearchField() === "state") {
                return true;
            } else {
                return false;
            }
        });
        self.havePlayable = ko.computed(function() {
            if (self.selectedSearchField() === "playable") {
                return true;
            } else {
                return false;
            }
        });
        self.haveItemCount = ko.computed(function() {
            if (self.itemCount() === "") {
                return false;
            } else {
                return true;
            }
        });
        self.haveSearchField = ko.computed(function() {
            if (self.selectedSearchField() === undefined) {
                return false;
            } else {
                return true;
            }
        });
        self.haveCustomField = ko.computed(function() {
            if (self.customField() === "") {
                return false;
            } else {
                return true;
            }
        });
        self.haveDateType = ko.computed(function() {
            if (self.selectedDateRangeType() === undefined) {
                return false;
            } else {
                return true;
            }
        });
        self.haveSearchTerms = ko.computed(function() {
            if (self.searchTerms() === "") {
                return false;
            } else {
                return true;
            }
        });
        self.haveSearchTermsOrDateRange = ko.computed(function() {
            if (self.haveSearchTerms() || self.haveDateType() || self.haveSearchState() || self.havePlayable()) {
                return true;
            } else {
                return false;
            }
        });

        // operations
        // function to write the request to the page
        self.logResponse = function(data) {
            data = JSON.parse(data);
            response.textContent += JSON.stringify(data, null, '  ') + "\n\n";
        };
        // function to set up the request
        self.setupURL = (function() {
            var str = "https://cms.api.brightcove.com/v1/accounts/" + self.account_id() + "/counts/videos?q=",
                fromDateValue = rome(fromDate).getDate(),
                toDateValue = rome(toDate).getDate();
            if (self.haveSearchField()) {
                if (self.haveSearchState()) {
                    str += self.selectedSearchField() + ":" + self.selectedSearchState();
                } else if (self.haveSearchTerms()) {
                    termsArray = self.searchTerms().split(" ");
                    imax = termsArray.length;
                    if (self.exactMatch()) {
                        for (i = 0; i < iMax; i++) {
                            termsArray[i] = '\"' + termsArray[i] + '\"';
                        }
                    }
                    for (i = 0; i < iMax; i++) {
                        item = termsArray[i];
                        item = self.selectedSeachField() + ":" + item;
                        if (self.excludeTerms()) {
                            item = "%2D" + item;
                        } else if (self.requiredTerms()) {
                            item = "%2B" + item;
                        }
                    }
                    str += termsArray.join("+");
                }
            } else if (self.haveCustomField()) {
                if (self.haveSearchTerms()) {
                    termsArray = self.searchTerms().split(" ");
                    iMax = termsArray.length;
                    if (self.exactMatch()) {
                        for (i = 0; i < iMax; i++) {
                            termsArray[i] = '\"' + termsArray[i] + '\"';
                        }
                    }
                    for (i = 0; i < iMax; i++) {
                        termsArray[i] = self.customField() + ":" + termsArray[i];
                        if (self.excludeTerms()) {
                            item = "%2D" + item;
                        } else if (self.requiredTerms()) {
                            item = "%2B" + item;
                        }
                    }
                    str += termsArray.join("+");
                }
            } else if (self.haveSearchTerms()) {
                // don't have field type
                if (self.exactMatch()) {
                    if (self.excludeTerms()) {
                        str += '%2D"' + self.searchTerms() + '"';
                    } else if (self.requiredTerms()) {
                        str += '%2B"' + self.searchTerms() + '"';
                    } else {
                        str += '"' + self.searchTerms() + '"';
                    }
                } else if (self.excludeTerms()) {
                    for (i = 0; i < iMax; i++) {
                        termsArray[i] = '%2D' + termsArray[i];
                        str += termsArray.join(" ");
                    }
                } else if (self.requiredTerms()) {
                    for (i = 0; i < iMax; i++) {
                        termsArray[i] = '%2B' + termsArray[i];
                        str += termsArray.join(" ");
                    }
                } else {
                    str += self.searchTerms();
                }
            }
            if (self.haveDateType()) {
                // add a plus if there are also search terms
                if (self.haveSearchTerms) {
                    str += "+";
                }
                if (fromDateValue !== null) {
                    str += self.selectedDateRangeType() + ":";
                    str += fromDateValue.toISOString();
                }
                str += "..";
                if (toDateValue !== null) {
                    str += toDateValue.toISOString();
                }
            }
            str += "&limit=" + self.selectedLimit();
            str += "&offset=" + self.selectedOffset();
            str += "&sort=" + self.selectedSortOption();
            url = str;
            self.apiURL.value = url;
            self.submitRequest("counts", function(count) {
                var totalSets = Math.ceil(count / self.selectedLimit()),
                    i;
                self.itemCount(count);
                for (i = 1; i < totalSets; i++) {
                    self.offsets.push(i * self.selectedLimit());
                }
                url = url.replace("/counts/videos", "/videos");
                self.apiURL.value = url;
                self.submitRequest("items", function(response) {
                    self.logResponse(response);
                });
            });
        });
        self.submitRequest = function(type, callback) {
            var httpRequest = new XMLHttpRequest(),
                requestData,
                options = {},
                // handler for response from the proxy
                getResponse = function() {
                    try {
                        if (httpRequest.readyState === 4) {
                            if (httpRequest.status >= 200 && httpRequest.status < 300) {
                                switch (type) {
                                    case "counts":
                                        var count = JSON.parse(httpRequest.responseText).count;
                                        callback(count);
                                        break;
                                    case "items":
                                        callback(httpRequest.responseText);
                                        break;
                                    default:
                                        self.logResponse("Unexpected response: " + httpRequest.responseText);
                                        break;
                                }
                            } else {
                                alert("There was a problem with the request. Request returned " + httpRequest.status);
                            }
                        }
                    } catch (e) {
                        alert('Caught Exception: ' + e);
                    }
                };
            // check for required account info
            if (!self.haveAccountInputs) {
                alert("You must provide an account id, client id, and client secret");
                return;
            }
            // set up request data
            options.account_id = self.account_id();
            options.client_id = self.client_id();
            options.client_secret = self.client_secret();
            options.url = self.apiURL.value;
            options.requestType = 'GET';
            requestData = JSON.stringify(options);
            // set response handler
            httpRequest.onreadystatechange = getResponse;
            // open the request
            httpRequest.open("POST", self.proxyURL());
            // open and send request
            httpRequest.send(requestData);
        };
    }
    // apply knockout bindings
ko.applyBindings(new cmsSearchModel());

            
          
!
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.

Console