Skip to content

Instantly share code, notes, and snippets.

@samrit
Last active April 19, 2018 02:08
Show Gist options
  • Save samrit/6262be429435cb8ff621bf55a801425f to your computer and use it in GitHub Desktop.
Save samrit/6262be429435cb8ff621bf55a801425f to your computer and use it in GitHub Desktop.
{"version":3,"names":[],"mappings":"","sources":["ajcSponsors.js"],"sourcesContent":["(function(f){if(typeof exports===\"object\"&&typeof module!==\"undefined\"){module.exports=f()}else if(typeof define===\"function\"&&define.amd){define([],f)}else{var g;if(typeof window!==\"undefined\"){g=window}else if(typeof global!==\"undefined\"){g=global}else if(typeof self!==\"undefined\"){g=self}else{g=this}g.ajcSponsors = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error(\"Cannot find module '\"+o+\"'\");throw f.code=\"MODULE_NOT_FOUND\",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require==\"function\"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){\n\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst sponsorship_1 = require(\"../models/sponsorship\");\nconst banner_1 = require(\"../models/banner\");\nconst article_1 = require(\"../models/article\");\nconst header_1 = require(\"../models/header\");\nconst logger_1 = require(\"../logger\");\nclass Sheet {\n constructor(sheetId, sheetName, apiKey) {\n this.sheetId = sheetId;\n this.sheetName = sheetName;\n this.apiKey = apiKey;\n this.sponsorSearchUrl = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${sheetName}!A:A?key=${apiKey}`;\n logger_1.log(`Search url is ${this.sponsorSearchUrl}`);\n this.sponsorDataUrl = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${sheetName}!{0}?key=${apiKey}`;\n logger_1.log(`Search url is ${this.sponsorDataUrl}`);\n }\n // Load sponsorship details from google sheet\n getSponsorship(sponsorId) {\n return __awaiter(this, void 0, void 0, function* () {\n logger_1.log('Querying google spreadsheet');\n const sponsorSearchResponse = yield fetch(this.sponsorSearchUrl);\n const sponsorSearch = yield sponsorSearchResponse.json();\n logger_1.log(`Sponsor search:\\n${JSON.stringify(sponsorSearch, null, 2)}`);\n let sponsorIndex = 0;\n for (sponsorIndex; sponsorIndex < sponsorSearch.values.length; sponsorIndex++) {\n const values = sponsorSearch.values[sponsorIndex];\n if (values.length > 0 && values[0] === sponsorId) {\n break;\n }\n }\n logger_1.log(`Sponsor Index is ${sponsorIndex}`);\n if (sponsorIndex === sponsorSearch.values.length) {\n logger_1.log(`Could not find sponsor ${sponsorId}`);\n return;\n }\n const row = sponsorIndex + 1;\n logger_1.log(`Fetching data from B${row}:K${row}`);\n const dataResponse = yield fetch(this.sponsorDataUrl.replace(\"{0}\", `B${row}:L${row}`));\n const data = yield dataResponse.json();\n if (data.values.length === 0) {\n logger_1.log(\"No values returned\");\n return;\n }\n const sponsorDetails = data.values[0];\n if (sponsorDetails.length < 11) {\n logger_1.log(\"Invalid data in spreadsheet\");\n return;\n }\n const banner = new banner_1.Banner(sponsorDetails[0], sponsorDetails[1]);\n const articles = [];\n for (let i = 5; i < 11; i += 2) {\n articles.push(new article_1.Article(sponsorDetails[i], sponsorDetails[i + 1]));\n }\n const header = new header_1.Header(sponsorDetails[2], sponsorDetails[3], sponsorDetails[4], articles);\n const sponsorship = new sponsorship_1.Sponsorship(banner, header);\n return sponsorship;\n });\n }\n}\nexports.Sheet = Sheet;\n\n},{\"../logger\":2,\"../models/article\":4,\"../models/banner\":5,\"../models/header\":6,\"../models/sponsorship\":7}],2:[function(require,module,exports){\n\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nfunction log(logText) {\n return __awaiter(this, void 0, void 0, function* () {\n //console.log(`AJC SPONSORS: ${logText}`);\n });\n}\nexports.log = log;\n\n},{}],3:[function(require,module,exports){\n\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst sheet_1 = require(\"./data/sheet\");\nconst logger_1 = require(\"./logger\");\nconst sheetId = \"1RBp7t1F6OGINh7rK3qOT4hBoio30OB5d9WpBbRql6zM\";\nconst sheetName = \"Sheet1\";\nconst apiKey = \"AIzaSyDwbTfRGdrCM3SBDHJ2HKdbqHSD-mS1d8M\";\nfunction loadSponsorship(sponsorId) {\n return __awaiter(this, void 0, void 0, function* () {\n logger_1.log(\"Loading sponsorships\");\n const sheet = new sheet_1.Sheet(sheetId, sheetName, apiKey);\n const sponsorship = yield sheet.getSponsorship(sponsorId);\n sponsorship.renderBanner();\n sponsorship.renderArticles();\n });\n}\nexports.loadSponsorship = loadSponsorship;\n\n},{\"./data/sheet\":1,\"./logger\":2}],4:[function(require,module,exports){\n\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst logger_1 = require(\"../logger\");\nclass Article {\n constructor(articleText, articleUrl) {\n this.articleText = articleText;\n this.articleUrl = articleUrl;\n logger_1.log(`Found article ${this.articleText}, ${this.articleUrl}`);\n }\n getHtmlElement() {\n const container = document.createElement(\"p\");\n const link = document.createElement(\"a\");\n link.href = this.articleUrl;\n link.innerHTML = `<b>${this.articleText}</b>`;\n container.appendChild(link);\n return container;\n }\n}\nexports.Article = Article;\n\n},{\"../logger\":2}],5:[function(require,module,exports){\n\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst logger_1 = require(\"../logger\");\nclass Banner {\n constructor(imageUrl, href) {\n this.imageUrl = imageUrl;\n this.href = href;\n logger_1.log(`Found banner ${this.imageUrl}, ${this.href}`);\n }\n getHtmlElement() {\n const container = document.createElement(\"div\");\n const link = document.createElement(\"a\");\n link.href = this.href;\n const image = document.createElement(\"img\");\n image.src = this.imageUrl;\n image.setAttribute(\"style\", \"width: 100%\");\n link.appendChild(image);\n container.appendChild(link);\n return container;\n }\n}\nexports.Banner = Banner;\n\n},{\"../logger\":2}],6:[function(require,module,exports){\n\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst logger_1 = require(\"../logger\");\nclass Header {\n constructor(headerText, sponsorLogoUrl, sponsorLogoHref, articles) {\n this.headerText = headerText;\n this.sponsorLogoUrl = sponsorLogoUrl;\n this.sponsorLogoHref = sponsorLogoHref;\n this.articles = articles;\n logger_1.log(`Found header text ${this.headerText}`);\n logger_1.log(`Found sponsor logo ${this.sponsorLogoUrl}`);\n }\n getHtmlElement() {\n const container = document.createElement(\"div\");\n container\n .classList\n .add(\"row\");\n container.setAttribute(\"style\", `border: 1px solid rgb(225, 232, 237); \\\n padding: 8px; \\\n margin-bottom: 4px; \\\n display: -webkit-box; \\\n display: -webkit-flex; \\\n display: -ms-flexbox; \\\n display: flex; \\\n align-items: center;`);\n const firstCol = document.createElement(\"div\");\n firstCol\n .classList\n .add(\"col-md-9\");\n firstCol\n .classList\n .add(\"col-sm-12\");\n const header = document.createElement(\"h4\");\n header.innerText = this.headerText;\n firstCol.appendChild(header);\n firstCol.appendChild(document.createElement(\"br\"));\n this\n .articles\n .forEach(element => {\n firstCol.appendChild(element.getHtmlElement());\n });\n const secondCol = document.createElement(\"div\");\n secondCol\n .classList\n .add(\"col-md-3\");\n secondCol\n .classList\n .add(\"col-sm-12\");\n secondCol.setAttribute(\"style\", \"text-align:center; vertical-align: middle; display: inline-block; float: none;\");\n const sponsorImage = document.createElement(\"img\");\n sponsorImage.src = this.sponsorLogoUrl;\n sponsorImage.height = 120;\n sponsorImage.width = 120;\n const imageAnchor = document.createElement(\"a\");\n imageAnchor.href = this.sponsorLogoHref;\n imageAnchor.target = \"_blank\";\n imageAnchor.appendChild(sponsorImage);\n secondCol.appendChild(imageAnchor);\n container.appendChild(firstCol);\n container.appendChild(secondCol);\n return container;\n }\n}\nexports.Header = Header;\n\n},{\"../logger\":2}],7:[function(require,module,exports){\n\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst logger_1 = require(\"../logger\");\nclass Sponsorship {\n constructor(banner, header) {\n this.banner = banner;\n this.header = header;\n }\n renderBanner() {\n const theParent = document.querySelector(\"heaader.story-header > div.lead-video\");\n if (theParent) {\n // fail silently\n logger_1.log('Could not find parent DOM element for banner');\n return;\n }\n logger_1.log('Inserting banner');\n theParent.insertBefore(this.banner.getHtmlElement(), theParent.firstChild);\n }\n renderArticles() {\n const theSibling = document.querySelector(\"div.story-text > p:nth-child(3)\");\n if (!theSibling) {\n logger_1.log(\"Could not find siblings for articles\");\n //fail silently\n return;\n }\n logger_1.log(\"Inserting header\");\n theSibling.insertAdjacentElement(\"afterend\", this.header.getHtmlElement());\n }\n}\nexports.Sponsorship = Sponsorship;\n\n},{\"../logger\":2}]},{},[3])(3)\n});"],"file":"ajcSponsors.js","sourceRoot":"/source/"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment