"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.showModal = function(body,opt,cb) { var self = this; if ( !this.onKeys ) { this.onKeys = function(e) { if ( e.keyCode == 13 ) { // Find the first primary button and click it var actions = self._reqModal._actions; for (var i = 0 ; i < actions.length ; i++ ) { if ( actions[i].primary ) self.modalAction(actions[i].id); } } else if ( e.keyCode == 27 ) { var actions = self._reqModal._actions; for (var i = 0 ; i < actions.length ; i++ ) { if ( actions[i].cancel ) self.modalAction(actions[i].id); } } return true; }.bind(this); } this.hideModal(); if ( !body ) { body = '
'; } opt.body = body; var modalHtml = Handlebars.templates['modal'](opt); var modal = $(modalHtml); this._reqModal = modal; $('.modal-dialog',modal).css('width',opt.width||'750px'); this.setModalActions(opt.actions); modal.bind('keydown', this.onKeys); modal.modal({backdrop: 'static', keyboard: false}); if ( cb ) { modal.on('shown.bs.modal', function() { cb(modal); }); } } HTMLApi.prototype.replaceModal = function(html) { $('.modal-body', this._reqModal).html(html); } HTMLApi.prototype.modalAction = function(id) { var action; this._reqModal._actions.forEach(function(candidate) { if ( candidate.id == id ) action = candidate; }); if ( action && action.onClick) { action.onClick(); } else if ( action && action.cancel ) { this.hideModal(); } } HTMLApi.prototype.hideModal = function() { var self = this; var old = self._reqModal; self._reqModal = null; if ( !old ) return; old.unbind('keydown', self.onKeys); old.modal('hide'); old.on('hidden.bs.modal', function() { old.remove(); }); } HTMLApi.prototype.setModalActions = function(actions) { actions = actions || []; this._reqModal._actions = actions; var html = ''; actions.forEach(function(action) { color = 'btn-default'; if ( action.primary ) color = 'btn-primary'; else if ( action.cancel ) color = 'btn-link'; html += ''; }); $('.modal-footer', 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