Best JavaScript code snippet using storybook-root
bifocals.js
Source:bifocals.js
...233 this.template = template;234 }235 }236 } else {237 this.cancelRender();238 this.render_state = root_module.RENDER_STATES.RENDER_REQUESTED;239 this.template = template;240 this.render(template, false);241 }242};243/**244 * Stops a render from occurring, and attempts to stop all child elements too.245 */246Bifocals.prototype.cancelRender = function bifocals_cancelRender() {247 var key = null;248 this.render_state = root_module.RENDER_STATES.RENDER_CANCELED;249 for (key in this._child_views) {250 this._child_views[key].cancelRender();251 }252 this._child_views = {};253};254/**255 * Builds a Renderer with all necessary data pulled from the view256 * 257 * @return {Renderer}258 */259Bifocals.prototype.buildRenderer = function bifocals_buildRenderer(RendererConstructor) {260 var _self = this;261 var renderer = new RendererConstructor();262 renderer.data = this._data;263 renderer.response = this._response;264 renderer.error(function (error, template) {265 _self.render_state = root_module.RENDER_STATES.RENDER_FAILED;266 // If the template is the default 500 template, don't get stuck in an infinite loop. Log the issue and stop trying to error.267 if (template === _self._defaultTemplates[500]) {268 console.log(error);269 _self.cancelRender();270 _self._response.end();271 } else {272 _self._error(error);273 }274 }).end(function () {275 _self.render_state = root_module.RENDER_STATES.RENDER_COMPLETE;276 _self._end();277 });278 return renderer;279};280/**281 * Sets an error handler which will be called any time an error occurs in this view282 * 283 * @param {Function} fn takes a single parameter, the error284 * @return {Bifocals} this, used for chaining285 */286Bifocals.prototype.error = function bifocals_error(fn) {287 this._error = fn;288 return this;289};290/**291 * Sets an end handler which will be called when the view is done rendering292 * 293 * @param {Function} fn takes no parameters294 * @return {[type]} this, used for chaining295 */296Bifocals.prototype.end = function bifocals_end(fn) {297 this._end = fn;298 return this;299};300/**301 * Create a child view relative to this view302 * 303 * @param {String} key required, the key the parent will render the data in304 * @param {String} template required, the template file to be rendered305 * @returns {Bifocals}306 */307Bifocals.prototype.child = function bifocals_child(key, template) {308 // Makes a fake response that writes to the parent instead of to an actual response object309 var new_view = new Bifocals({310 buffer: '',311 write: function (chunk) {312 if (typeof chunk === "object") {313 // Objects don't replace the data, they override it. We might want to build an array here in the future.314 // This allows for a heirarchy of non-string views, like json315 this.buffer = chunk;316 } else {317 this.buffer += chunk; 318 }319 },320 end: function () { 321 // flag the child view as rendered322 new_view.render_state = root_module.RENDER_STATES.RENDER_COMPLETE;323 // set the child data into the parent view, and then render the parent if possible324 new_view.parent.set(key, this.buffer); 325 if(new_view.parent.canRender()) {326 // Break up render flow by processing any parent renders on the next tick327 process.nextTick(function () {328 new_view.parent.render();329 });330 }331 }332 });333 new_view.content_type = this.content_type;334 new_view.parent = this;335 new_view.root = this.root;336 new_view.dir = this.dir;337 new_view.error(this._error);338 339 if (template) {340 new_view.template = template;341 }342 this._child_views[key] = new_view;343 return this._child_views[key];344};345/**346 * Set the response status code in the response tied to the parent most view347 * 348 * @param {int} code349 * @return {Bifocals} this, used for chaining350 */351Bifocals.prototype.setStatusCode = function bifocals_setStatusCode(code) {352 this.root._response.statusCode = code;353 return this;354};355/**356 * Set a collection of headers in the response tied to the parent most view357 * 358 * @param {Object} headers 359 * @return {Bifocals} this, used for chaining360 */361Bifocals.prototype.setHeaders = function bifocals_setHeaders(headers) {362 var key = null;363 for(key in headers) {364 this.root._response.setHeader(key, headers[key]);365 }366 return this;367};368/**369 * Return a 404: Not found code, and overwrite the existing template with the one provided370 * 371 * @param {string} template information passed to the root rendererer to be immediately rendered372 */373Bifocals.prototype.statusNotFound = function bifocals_statusNotFound(template) {374 var status = 404;375 this.setStatusCode(status);376 if (typeof template === "string") {377 this.root.render(template, true);378 } else if (typeof this.root._defaultTemplates[status] === "string") {379 this.root.render(this.root._defaultTemplates[status], true);380 } else {381 this.root.cancelRender();382 this.root._response.end();383 }384};385/**386 * Return a 500: Error code, and overwrite the existing template with the one provided387 * 388 * @param {Error} error the error object you wish to provide to the view389 * @param {String} template information passed to the root renderer to be immediately rendered390 */391Bifocals.prototype.statusError = function bifocals_statusError(error, template) {392 var status = 500;393 this.setStatusCode(status);394 this.root.set('error', error);395 if (typeof template === "string") {396 this.root.render(template, true);397 } else if (typeof this.root._defaultTemplates[status] === "string") {398 this.root.render(this.root._defaultTemplates[status], true);399 } else {400 this.root.cancelRender();401 this.root._response.end();402 }403};404/**405 * Return a 201: Created code, and redirect the user to the provided url406 * 407 * This should be used any time you create a resource per request of a user.408 * 409 * for example, if I call410 * 411 * PUT /users412 * name=aaron&email=aaron@dashron.com413 * 414 * which successfully creates user 1, aaron415 * the view at /users should end as view.created('/users/1');416 * 417 * @param {string} redirect_url418 */419Bifocals.prototype.statusCreated = function bifocals_statusCreated(redirect_url) {420 this.setStatusCode(201);421 this.setHeaders({422 Location : redirect_url423 });424 this.root.cancelRender();425 this.root._response.end();426};427/**428 * Return a 302: Found code, 429 * 430 * @todo add support for other 300's within this function431 * @todo describe how this would be properly used432 * @param {string} redirect_url433 */434Bifocals.prototype.statusRedirect = function bifocals_statusRedirect(redirect_url) {435 this.setStatusCode(302);436 this.setHeaders({437 Location : redirect_url438 });439 this.root.cancelRender();440 this.root._response.end();441};442/**443 * Returns a 304: Not found code,444 * 445 * This tells the browser to use a previously cached version of this page.446 * @todo : as a parameter take some headers to control this? date, etag, expires, cache control447 */448Bifocals.prototype.statusNotModified = function bifocals_statusNotModified() {449 this.setStatusCode(304);450 this.root.cancelRender();451 // date452 // etag453 // expires454 // cache control455 this.root._response.end();456};457/**458 * Returns a 405: Unsupported Method code,459 * 460 * This is used to state that the method (GET, POST, PUT, PATCH, DELETE, HEAD) is not supported for the461 * requested uri. You must provide a list of acceptable methods462 * 463 * @param {Array} supported_methods 464 */465Bifocals.prototype.statusUnsupportedMethod = function bifocals_statusUnsupportedMethod(supported_methods) {466 this.setStatusCode(405);467 this.setHeaders({468 Allow : supported_methods.join(',')469 });470 this.root.cancelRender();471 this.root._response.end();472};473/**474 * [bifocals_statusUnauthorized description]475 * @param {[type]} template [description]476 * @return {[type]} [description]477 */478Bifocals.prototype.statusUnauthorized = function bifocals_statusUnauthorized(template) {479 var status = 401;480 this.setStatusCode(status);481 if (typeof template === "string") {482 this.root.render(template, true);483 } else if (typeof this.root._defaultTemplates[status] === "string") {484 this.root.render(this.root._defaultTemplates[status], true);485 } else {486 this.root.cancelRender();487 this.root._response.end();488 }...
Fonts.jsx
Source:Fonts.jsx
...77 super()78 this.cancelRender = this.cancelRender.bind(this)79 this.setFonts = this.setFonts.bind(this)80 }81 cancelRender() {82 this.props.stopRender()83 //browserHistory.push('/')84 }85 getFontsList() {86 return this.props.fonts.map((item) => {87 return <FontForm 88 key={item.fName}89 data={item} 90 updateInput={this.props.updateInput} 91 changeOrigin={this.props.updateFontOrigin} />92 })93 }94 setFonts() {95 this.props.setFonts()...
Dot.js
Source:Dot.js
...16 }17 showPath(pathElement) {18 this.setState(Object.assign(this.state, {position: pathElement}));19 }20 cancelRender() {21 if(this.state.renderTask) {22 //console.log("canceled render");23 clearInterval(this.state.renderTask);24 this.setState(Object.assign(this.state, {renderTask: null}));25 }26 else {27 //console.log("canceled NON EXISTENT render");28 }29 }30 renderPath() {31 this.cancelRender();32 //console.log("called renderPath");33 let path = this.props.path;34 if(!path || path.length == 0) {35 return;36 }37 path = [...path];38 let renderFn = () => {39 let pathElement = path.shift();40 this.showPath(pathElement);41 if(path.length == 0) {42 this.cancelRender();43 }44 }45 renderFn();46 if(path.length != 0) {47 //console.log("seting rendertask ID");48 this.setState(Object.assign(this.state, {49 renderTask: setInterval(renderFn, this.props.time || 1000)50 }));51 }52 }53 componentWillUnmount() {54 this.cancelRender();55 }56 mathcPath(path0, path1) {57 if(path0.length != path1.length) {58 return false;59 }60 for(let i = 0; i < path0.length; ++i) {61 if(path0.x !== path1.x || path0.y !== path1.y)62 return false;63 }64 return true;65 }66 componentDidUpdate(prevProps, prevState) {67 if(this.props !== prevProps && !this.mathcPath(prevProps.path, this.props.path)) {68 this.renderPath();...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!