"use strict"; //data, schemasUrl, docs, user, curlUser, cb function HTMLApi(opt, cb) { var self = this; this._schemas = null; this._schemaDocs = null; this._data = opt.data; this._docsPage = opt.docsPage; this._docsJson = opt.docsJson; this._user = opt.user; this._curlUser = opt.curlUser || '${GDAPI_ACCESS_KEY}:${GDAPI_SECRET_KEY}'; this._logout = opt.logout !== false; this._filterId = 0; this._reqModal = null; this._editSchema = null; this._editData = null; this._lastMethod = null; this._lastMode = null; this._lastType = null; this._lastOpt = null; this._lastRequestBody = null; this._error = null; this._referenceDropdownLimit = 100; this._magicNull = "__-*NULL*-__"; this._magicNullRegex= new RegExp(this._escapeRegex(this._magicNull)+'$'); this._formatter = new JSONFormatter({ baseUrl: window.location.protocol +"//" + window.location.host, keyFormatter: this.keyFormatter.bind(this), valueFormatter: this.valueFormatter.bind(this) }); async.auto({ title: this.titleUpdate.bind(this), rawSchema: this.schemasLoad.bind(this, opt.schemasUrl), schema: ['rawSchema', this.schemasMunge.bind(this) ], docs: ['schema', this.docsLoad.bind(this, opt.docsJson) ], }, initDone); function initDone(err, results) { self._error = err; cb(); } } HTMLApi.prototype.show = function(cb) { var self = this; async.auto({ render: this.render.bind(this) , filters: ['render', this.filterInit.bind(this) ], }, showDone); function showDone(err, results) { if ( err ) { } if ( self._error ) { $('#header-body').css('display','none'); $('#header-error').css('display',''); } else { $('#header-body').css('visibility','visible'); } if ( cb ) cb(); } } HTMLApi.prototype._setupModal = function(html) { if ( this._reqModal ) { this._reqModal.sfDialog('close'); this._reqModal.remove(); this._reqModal = null; } if ( !html ) { html = '
'; } this._reqModal = $('').html(html); return this._reqModal; } HTMLApi.prototype.showModal = function(html,in_opt,cb) { var self = this; function onMoreKeys(e) { if ( e.keyCode == 13 ) { // Find the first primary button and click it var buttons = self._reqModal.sfDialog('getButtons'); var button; for (var i = 0 ; i < buttons.length ; i++ ) { button = buttons[i].button; if ( button.hasClass('sf-btn-primary') ) { button.trigger('click'); break; } } } return true; } var opt = { destroyOnClose: false, dialogHeightMin: 100, buttons: [ {id: 'cancel', text: 'Cancel', cancel: true } ], onOpen: function() { $(this.context.document).bind('keydown', onMoreKeys); }, onClose: function() { $(this.context.document).unbind('keydown', onMoreKeys); } }; var k = Object.keys(in_opt); for ( var i = 0 ; i < k.length ; i++ ) { opt[ k[i] ] = in_opt[ k[i] ]; } self._setupModal(html); require('starfield/sf.dialog', function() { self._reqModal.sfDialog(opt); if ( cb ) cb(self._reqModal); }); } HTMLApi.prototype.replaceModal = function(html) { this._reqModal.html(html); } HTMLApi.prototype.titleUpdate = function(cb) { var title = "API"; if ( this._data ) title += ": " + (this._data.displayName || this._data.name || this._data.id || this._data.resourceType || this._data.type); document.title = title; if ( cb ) async.nextTick(cb); } HTMLApi.prototype.schemasLoad = function(link, cb) { if ( !this._data ) return async.nextTick(function() { cb("No data") }); // Link may come from the page