Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div ng-app="WikipediaArticleReader" class="jumbotron jumbotron-main" ng-cloak>
    <loading class="loading-indicator"></loading>
    <div class="container" ng-controller="WikipediaController as wikiCtrl">
      <h1 class="text-center"> {{wikiCtrl.title}}</h1>
      <wikipedia-search-component></wikipedia-search-component>
    </div>
  </div>
              
            
!

CSS

              
                [ng\:cloak], [ng-cloak], .ng-cloak {
  display: none !important;
}

.loading-indicator {
    display: block;
    width: 70px;
    position: fixed;
    left: 0;
    right: 0;
    margin: 0 auto;
    top: 40%;
    z-index: 100;
    background-color: #FFF;
}

.loading-indicator img {
    width: 70px;
    background-color: #FFF;
}

.jumbotron-main {
  height: 100%;
  min-height: 100vh;
  margin-top: 0;
  padding-top: 0;
  margin-bottom: 0;
  padding-bottom: 0;
  background: #ffffff url("https://res.cloudinary.com/jawadrashid/image/upload/c_scale,w_1920/v1480670076/Wikipedia/pexels-photo-46274.jpg") no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

.center-block {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

.panel-body {
  text-decoration: none;
}

.searchResults, .searchResults:hover {
  text-decoration: none;
  color: black;
}

.searchResults:hover .panel {
  background-color: beige;
}
              
            
!

JS

              
                (function () {

	'use strict';

	angular.module('WikipediaArticleReader', ['ngSanitize'])
		.controller('WikipediaController', WikipediaController)
		.component('loading', {
			template: '<img src="https://res.cloudinary.com/jawadrashid/image/upload/v1479984923/Weather/spinner.svg" ng-if="$ctrl.show">',
			controller: LoadingController
		})
		.component('wikipediaSearchComponent', {
			template: '<div>\
							<button type="button" class="btn btn-primary btn-lg center-block" ng-click="$ctrl.getRandomArticle()"><i class="fa fa-random" aria-hidden="true"></i> &nbsp;Get Random Wikipedia Article</button>\
					   </div>\
					   <br>\
					   <div>\
					   		<form class="form-horizontal">\
							<div class="form-group">\
								<div class="col-sm-offset-2 col-sm-8 col-xs-offset-1 col-xs-10">\
									<input type="text" class="form-control" id="searchTerm" placeholder="Search Wikipedia" ng-model="$ctrl.searchTerm" ng-keypress="$ctrl.searchTermInput($event)">\
								</div>\
							</div>\
							<div class="form-group">\
								<div class="col-sm-offset-2 col-sm-8 col-xs-offset-1 col-xs-10" >\
									<button type="button" class="btn btn-success btn-lg center-block" ng-click="$ctrl.searchWikipedia()" ng-disabled="$ctrl.searchTerm == \'\'">Search</button>\
								</div>\
							</div>\
							</form>\
					   </div>\
					   <div class="alert alert-danger text-center col-sm-offset-2 col-sm-8 col-xs-offset-1 col-xs-10" ng-if="$ctrl.searchError">\
							<strong>Error!</strong> No results found.\
						</div>\
						<wikipedia-list list="$ctrl.searchResults" ng-if="$ctrl.searchResults.length > 0"></wikipedia-list>\
					  ',
			bindings: {},
			controller: WikipediaSearchController
		})
		.component('wikipediaList', {
			template: '<a href="https://en.wikipedia.org/wiki/{{result.title}}" target="_blank" ng-repeat="result in $ctrl.list" class="searchResults" >\
							<div class="panel panel-info">\
								<div class="panel-heading">\
									<h3 class="panel-title">{{result.title}}</h3>\
								</div>\
								<div class="panel-body" ng-bind-html="result.snippet">\
								</div>\
					  		</div>\
						</a>',
			bindings: {
				list: '<'
			}
		})
		.factory('loadingHttpInterceptor', LoadingHttpInterceptor)
		.service('WikipediaSearchService', WikipediaSearchService)
		.constant('WikipediaBaseUrl', "https://en.wikipedia.org/")
		.config(config);


	WikipediaController.$inject = []

	function WikipediaController() {
		var ctrl = this;
		ctrl.title = "Wikipedia Article Reader";
	}

	WikipediaSearchController.$inject = ['WikipediaSearchService', '$window']

	function WikipediaSearchController(WikipediaSearchService, $window) {
		var ctrl = this;
		ctrl.searchTerm = "";
		ctrl.searchError = false;
		ctrl.searchResults = [];

		ctrl.getRandomArticle = function () {
			var url = WikipediaSearchService.getRandomArticleUrl();
			window.open(url, '_blank');
		}

		ctrl.searchTermInput = function (keyEvent) {
			if (keyEvent.which == 13) {
				ctrl.searchWikipedia();
			}
		}

		ctrl.searchWikipedia = function () {
			ctrl.searchError = false;
			ctrl.searchResults = [];
			WikipediaSearchService.searchWikipedia(ctrl.searchTerm).then(ctrl.searchWikipediaSuccess, ctrl.searchWikipediaError);
		}

		ctrl.searchWikipediaSuccess = function (response) {
			console.log(response);
			ctrl.searchResults = response;
		}

		ctrl.searchWikipediaError = function (response) {
			ctrl.searchError = true;
		}
	}

	WikipediaSearchService.$inject = ['$http', 'WikipediaBaseUrl', '$q']

	function WikipediaSearchService($http, WikipediaBaseUrl, $q) {
		var service = this;

		service.getRandomArticleUrl = function () {
			return "https://en.wikipedia.org/wiki/Special:Random";
		}

		service.searchWikipedia = function (searchTerm) {
			return $http({
				method: "GET",
				url: (WikipediaBaseUrl + "w/api.php"),
				params: {
					action: "query",
					format: "json",
					origin: "*",
					list: "search",
					iwurl: 1,
					srsearch: searchTerm,
					srprop: "snippet|titlesnippet"
				}
			}).then(service.successFunction);
		}

		service.successFunction = function (response) {
			var data = response.data;
			var query = data.query;
			if(query.searchinfo.totalhits == 0) {
				return $q.reject('No Results Found');
			} else {
				return query.search;
			}
			
		}
	}

	LoadingController.$inject = ['$rootScope']

	function LoadingController($rootScope) {
		var $ctrl = this;
		var listener;

		$ctrl.$onInit = function () {
			$ctrl.show = false;
			listener = $rootScope.$on('spinner:activate', onSpinnerActivate);
		}

		$ctrl.$onDestroy = function () {
			listener();
		}

		function onSpinnerActivate(event, data) {
			$ctrl.show = data.on;
		}
	}

	LoadingHttpInterceptor.$inject = ['$rootScope', '$q']

	function LoadingHttpInterceptor($rootScope, $q) {
		var loadingCount = 0;
		var loadingEventName = 'spinner:activate';

		return {
			request: function (config) {
				if (++loadingCount == 1) {
					$rootScope.$broadcast(loadingEventName, {
						on: true
					});
				}

				return config;
			},

			response: function (response) {
				if (--loadingCount == 0) {
					$rootScope.$broadcast(loadingEventName, {
						on: false
					});
				}

				return response;
			},

			responseError: function (response) {
				if (--loadingCount == 0) {
					$rootScope.$broadcast(loadingEventName, {
						on: false
					});
				}

				return $q.reject(response);
			}
		}
	}

	config.$inject = ['$httpProvider'];

	function config($httpProvider) {
		$httpProvider.interceptors.push('loadingHttpInterceptor');
	}

	function isKeyInObject(key, object) {
		return (key in object && object[key] != "" && object[key] != undefined);
	}

})();
              
            
!
999px

Console