/**
* @function loadJSON
* @summary
* Use this function to load JSON from the specified file path.
* @description
* Options Object:
* * filename:
* * Type: string
* * Requirements: Required
* * Description: The JSON file to load.
*
* * eventLabel:
* * Type: string
* * Requirements: Required
* * Description: The label for the events that will be emitted when JSON is loaded successfully or when an error occurs during loading.
* * Note: Events will use the following structure...
* * * Success: <eventLabel>_RESULTS
* * * Error: <eventLabel>_ERROR
*
* * root:
* * Type: string
* * Requirements: Optional
* * Description: Should you need to load JSON that is not hosted on tremorvideodsp.com, this is the full path to the JSON (minus the actual filename)
*
* * timeout:
* * Type: number
* * Requirements: Optional
* * Description: Amount of time (in seconds) to wait for the JSON results. (default 3 seconds)
*
* @param {Object=} opts Object containing all of the params to pass into the function
* @param {string=} opts.filename The JSON file to load.
* @param {string=} opts.eventLabel The label for the events that will be emitted when JSON is loaded successfully or when an error occurs during loading.
* @param {string=} opts.root Should you need to load JSON that is not hosted on tremorvideodsp.com, this is the full path to the JSON (minus the actual filename)
* @param {number=} opts.timeout (default 3 seconds) maximum seconds to wait for JSON results.
*/
function loadJSON (opts) {
if (typeof opts !== 'object') {
console.error('You must provide an (opts) object with parameters to pass into the function.')
return
}
if (!opts.filename || typeof opts.filename !== 'string') {
console.error('You must provide a (string) JSON filename to load.')
return
}
if (!opts.eventLabel || typeof opts.eventLabel !== 'string') {
console.error('You must provie a (string) label for the JSON_RESULTS and JSON_ERROR events that fire when JSON is loaded or when it cannot be loaded.')
return
}
var filename = opts.filename
var label = opts.eventLabel
var eventName = label + '_RESULTS'
var errorEvent = label + '_ERROR'
var baseUrl = ''
if (typeof window.$b === 'object') {
baseUrl = window.$b.baseUrl
}
var timeout = opts.timeout
if ((typeof timeout !== 'number') || (timeout <= 0)) {
timeout = 3000
} else {
timeout *= 1000
}
// Note: If no opts.root is provided, then the JSON path will be relative to the Starter Kit's src folder.
// To import JSON not stored on tremorvideodsp.com, provide the path as opts.root (minus the filename)
var root = opts.root === '' ? '' : opts.root || baseUrl + '/json/' || ''
return $.ajax({
url: root + filename,
crossDomain: true,
dataType: 'json',
timeout: timeout
})
.fail(function (err) {
console.error(errorEvent, err.statusText)
window.$b.emit(errorEvent, new Error(err.statusText))
})
.done(function (data) {
console.log(eventName, data)
window.$b.emit(eventName, data)
})
}
window.$b = window.$b || {}
window.$b.snippets = window.$b.snippets || {}
var snippets = window.$b.snippets
snippets.loadJSON = loadJSON