PageRenderTime 83ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/domainr-search-box/0.0.9/domainr-search-box.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 552 lines | 447 code | 93 blank | 12 comment | 88 complexity | 452939bd5e22f8b91d29490d52e3ee4f MD5 | raw file
  1. (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.domainr = 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){
  2. 'use strict';
  3. var util = require('./util');
  4. var sequence = 0;
  5. // Detect CORS support
  6. var cors = false;
  7. if ('withCredentials' in xhr()) {
  8. cors = true;
  9. }
  10. // ----------
  11. function getJSON(url, callback) {
  12. if (!callback) {
  13. throw new Error('[domainr] Missing callback');
  14. }
  15. if (cors) {
  16. getCORS(url, callback);
  17. return;
  18. }
  19. getJSONP(url, callback);
  20. }
  21. // ----------
  22. // Minimal fallback for IE
  23. // http://toddmotto.com/writing-a-standalone-ajax-xhr-javascript-micro-library/
  24. function xhr() {
  25. try {
  26. var XHR = window.XMLHttpRequest || window.ActiveXObject;
  27. return new XHR('MSXML2.XMLHTTP.3.0');
  28. } catch (e) {
  29. return {};
  30. }
  31. }
  32. // ----------
  33. function getCORS(url, callback) {
  34. var x = xhr();
  35. x.onreadystatechange = function() {
  36. if (x.readyState != 4) {
  37. return;
  38. }
  39. if (x.status != 200) {
  40. util.error('Error fetching data: ' + x.responseText);
  41. return;
  42. }
  43. var result;
  44. try {
  45. result = JSON.parse(x.responseText);
  46. } catch (e) {
  47. util.error('Unable to parse data: ' + x.responseText + '; ' + e);
  48. return;
  49. }
  50. callback(result);
  51. };
  52. x.open('GET', url, true);
  53. x.send();
  54. }
  55. // ----------
  56. function getJSONP(url, callback) {
  57. var script = document.createElement('script');
  58. script.async = true;
  59. var id = '_jsonp' + sequence++;
  60. var timeout = setTimeout(function() {
  61. util.error('Timeout trying to retrieve ' + url);
  62. }, 5000);
  63. window[id] = function(data) {
  64. clearTimeout(timeout);
  65. setTimeout(function() {
  66. var scr = document.getElementById(id);
  67. scr.parentNode.removeChild(scr);
  68. window[id] = undefined;
  69. try {
  70. delete window[id];
  71. } catch (e) {}
  72. }, 0);
  73. callback(data);
  74. };
  75. var c = url.indexOf('?') >= 0 ? '&' : '?';
  76. script.setAttribute('src', url + c + 'callback=' + id);
  77. script.id = id;
  78. var head = document.head || document.getElementsByTagName('head')[0] || document.documentElement;
  79. head.insertBefore(script, head.firstChild);
  80. }
  81. // ----------
  82. module.exports = {
  83. getJSON: getJSON
  84. };
  85. },{"./util":6}],2:[function(require,module,exports){
  86. 'use strict';
  87. var ajax = require('./ajax');
  88. var util = require('./util');
  89. function Client(options) {
  90. options = options || {};
  91. this.clientId = options.clientId;
  92. this.mashapeKey = options.mashapeKey;
  93. if (!this.clientId && !this.mashapeKey) {
  94. throw new Error('[domainr] Missing mashapeKey or clientId');
  95. }
  96. this.baseURL = options.baseURL || (this.clientId ? 'https://api.domainr.com/v2' : 'https://domainr.p.mashape.com/v2');
  97. }
  98. Client.prototype = {
  99. search: function(params, callback) {
  100. this._get('/search', this.searchParams(params), callback);
  101. },
  102. searchParams: function(p) {
  103. return util.extract(p, ['query', 'registrar', 'location', 'defaults']);
  104. },
  105. status: function(domains, callback) {
  106. if (!domains) {
  107. throw new Error('[domainr] domains array is required');
  108. }
  109. util.uniq(domains);
  110. var params = {
  111. domain: domains.join(',')
  112. };
  113. this._get('/status', params, callback);
  114. },
  115. options: function(domain, callback) {
  116. this._get('/options', {
  117. domain: domain,
  118. }, callback);
  119. },
  120. zones: function(callback) {
  121. this._get('/zones', {}, callback);
  122. },
  123. registerURL: function(domain, options) {
  124. var params = util.extract(options, ['registrar']);
  125. params.domain = domain;
  126. return this._url('/register', params);
  127. },
  128. _get: function(path, params, callback) {
  129. var url = this.baseURL + path + '?' + util.qs(params || {}, this._key());
  130. ajax.getJSON(url, callback);
  131. },
  132. _url: function (path, params) {
  133. return this.baseURL + path + '?' + util.qs(params || {}, this._key());
  134. },
  135. _key: function() {
  136. if (this.clientId) {
  137. return {
  138. client_id: this.clientId
  139. };
  140. }
  141. return {
  142. 'mashape-key': this.mashapeKey
  143. };
  144. }
  145. };
  146. module.exports = Client;
  147. },{"./ajax":1,"./util":6}],3:[function(require,module,exports){
  148. 'use strict'
  149. var Client = require('./client');
  150. var SearchBox = require('./search-box');
  151. require('./jquery');
  152. module.exports = {
  153. Client: Client,
  154. SearchBox: SearchBox
  155. };
  156. },{"./client":2,"./jquery":4,"./search-box":5}],4:[function(require,module,exports){
  157. 'use strict';
  158. var SearchBox = require('./search-box');
  159. var util = require('./util');
  160. if (window.jQuery) {
  161. var $ = window.jQuery;
  162. $.fn.domainrSearchBox = function(config) {
  163. this.each(function(i, el) {
  164. var searchBoxConfig = util.extract(config, ['clientId', 'mashapeKey',
  165. 'renderWith', 'limit', 'registrar', 'defaults', 'onSelect']);
  166. searchBoxConfig.observe = el;
  167. if (config.renderTo) {
  168. var $el = $(config.renderTo);
  169. if ($el.length > 0) {
  170. searchBoxConfig.renderTo = $el[0];
  171. }
  172. }
  173. var searchBox = new domainr.SearchBox(searchBoxConfig);
  174. });
  175. return this;
  176. };
  177. }
  178. },{"./search-box":5,"./util":6}],5:[function(require,module,exports){
  179. 'use strict'
  180. var Client = require('./client');
  181. var util = require('./util');
  182. var SearchBox = function(options) {
  183. var self = this;
  184. options = options || {};
  185. if (!options.observe) {
  186. throw new Error('[domainr] "observe" is required');
  187. }
  188. this._client = new Client(util.extract(options, ['clientId', 'mashapeKey']));
  189. this._state = {
  190. query: '',
  191. results: [],
  192. limit: 20,
  193. selection: -1
  194. };
  195. this._seq = 0;
  196. this._last = 0;
  197. this._cache = {};
  198. this._listeners = {};
  199. this._renderer = defaultRenderer;
  200. this._in = options.observe;
  201. on(this._in, 'keyup', this._input, this);
  202. on(this._in, 'input', this._input, this);
  203. on(this._in, 'change', this._input, this);
  204. on(this._in, 'keydown', this._keydown, this);
  205. if (options.renderTo !== undefined) {
  206. this._out = options.renderTo;
  207. addClass(this._out, 'domainr-results-container');
  208. on(this._out, 'click', this._click, this);
  209. }
  210. if (options.renderWith !== undefined) {
  211. this._renderer = options.renderWith;
  212. }
  213. if (options.limit !== undefined) {
  214. this._state.limit = options.limit;
  215. }
  216. if (options.registrar !== undefined) {
  217. this._state.registrar = options.registrar;
  218. }
  219. if (options.defaults !== undefined) {
  220. this._state.defaults = options.defaults.join(',');
  221. }
  222. if (options.onSelect !== undefined) {
  223. this._onSelect = options.onSelect;
  224. } else {
  225. this._onSelect = function(result) {
  226. self._in.value = result.domain;
  227. window.open(self._client.registerURL(result.domain));
  228. };
  229. }
  230. };
  231. SearchBox.prototype = {
  232. _input: function() {
  233. if (this._state.query != this._in.value) {
  234. this._state.query = this._in.value;
  235. this._search();
  236. }
  237. },
  238. _keydown: function(event) {
  239. event = event || window.event;
  240. var handled = false;
  241. if (event.keyCode === 38) { // Up arrow
  242. handled = true;
  243. this._state.selection--;
  244. if (this._state.selection < 0) {
  245. this._state.selection = this._state.results.length - 1;
  246. }
  247. this._update();
  248. } else if (event.keyCode === 40) { // Down arrow
  249. handled = true;
  250. this._state.selection++;
  251. if (this._state.selection >= this._state.results.length) {
  252. this._state.selection = 0;
  253. }
  254. this._update();
  255. } else if (event.keyCode === 13) { // Enter key
  256. if (this._state.selection !== -1) {
  257. handled = true;
  258. this._choose(this._state.results[this._state.selection]);
  259. }
  260. }
  261. if (handled && event.preventDefault) {
  262. event.preventDefault();
  263. }
  264. },
  265. _click: function(event) {
  266. event = event || window.event;
  267. var rs = this._state.results;
  268. for (var e = event.target || event.srcElement; e && e != document; e = e.parentNode) {
  269. var d = e.getAttribute('data-domain');
  270. if (d) {
  271. for (var i = 0; i < rs.length; i++) {
  272. var r = rs[i];
  273. if (r.domain == d) {
  274. this._choose(r);
  275. return;
  276. }
  277. }
  278. }
  279. }
  280. },
  281. _render: function() {
  282. if (!this._out) {
  283. return;
  284. }
  285. this._out.innerHTML = this._renderer(this._state);
  286. return this;
  287. },
  288. _search: function() {
  289. var self = this;
  290. this._state.selection = -1;
  291. // Try cache first
  292. var key = util.qs(this._client.searchParams(this._state));
  293. var res = this._cache[key];
  294. if (res !== undefined) {
  295. this._state.results = res.results;
  296. this._update();
  297. return;
  298. }
  299. // Make network request
  300. var seq = this._seq++;
  301. this._client.search(this._state, function(res) {
  302. self._cache[key] = res;
  303. if (self._last > seq) {
  304. return;
  305. }
  306. self._last = seq;
  307. self._state.results = res.results;
  308. self._update();
  309. });
  310. },
  311. _update: function() {
  312. this._limit();
  313. this._status();
  314. this._render();
  315. },
  316. _limit: function() {
  317. if (this._state.limit >= 0 && this._state.results.length > this._state.limit) {
  318. this._state.results.length = this._state.limit;
  319. }
  320. },
  321. _status: function() {
  322. var self = this;
  323. // Extract domains without status
  324. var d = [];
  325. var MAX_STATUS_DOMAINS = 10;
  326. var rs = this._state.results;
  327. for (var i = 0; i < rs.length && d.length < MAX_STATUS_DOMAINS; i++) {
  328. var r = rs[i];
  329. r.status = this._cache[r.domain + ':status'] || r.status;
  330. if (!r.status) {
  331. r.status = 'unknown';
  332. d.push(r.domain);
  333. }
  334. }
  335. if (d.length === 0) {
  336. return;
  337. }
  338. // Make network request
  339. this._client.status(d, function(res) {
  340. var ss = res.status;
  341. for (var i = 0; i < ss.length; i++) {
  342. var s = ss[i];
  343. self._cache[s.domain + ':status'] = s.status;
  344. }
  345. self._update();
  346. });
  347. },
  348. _choose: function(result) {
  349. if (this._onSelect) {
  350. this._onSelect(result);
  351. }
  352. }
  353. };
  354. function defaultRenderer(state) {
  355. var rs = state.results;
  356. var l = rs.length;
  357. if (l === 0) {
  358. return '';
  359. }
  360. var h = ['<div class="domainr-results">'];
  361. for (var i = 0; i < l; i++) {
  362. var r = rs[i];
  363. var classNames = [
  364. 'domainr-result',
  365. r.status
  366. ];
  367. if (state.selection === i) {
  368. classNames.push('selected');
  369. }
  370. h.push(
  371. '<div class="' + classNames.join(' ') + '" data-domain="' + r.domain + '">' +
  372. '<span class="domainr-result-domain">' +
  373. '<span class="domainr-result-host">' + r.host + '</span>' +
  374. '<span class="domainr-result-subdomain">' + r.subdomain + '</span>' +
  375. '<span class="domainr-result-zone">' + r.zone + '</span>' +
  376. '</span>' +
  377. '<span class="domainr-result-path">' + r.path + '</span>' +
  378. '</div>'
  379. );
  380. }
  381. h.push('</div>');
  382. return h.join('');
  383. }
  384. function on(e, ev, cb, obj) {
  385. if (obj) {
  386. var original = cb;
  387. cb = function() {
  388. return original.apply(obj, arguments);
  389. };
  390. }
  391. if (e.addEventListener) {
  392. e.addEventListener(ev, cb, false);
  393. } else if (e.attachEvent) {
  394. e.attachEvent('on' + ev, cb);
  395. } else {
  396. e['on' + ev] = cb;
  397. }
  398. }
  399. function addClass(e, className) {
  400. if (e.classList) {
  401. e.classList.add(className);
  402. } else {
  403. e.className += ' ' + className;
  404. }
  405. }
  406. module.exports = SearchBox;
  407. },{"./client":2,"./util":6}],6:[function(require,module,exports){
  408. 'use strict'
  409. var euc = encodeURIComponent;
  410. function extract(p, keys) {
  411. var x = {};
  412. if (p) {
  413. for (var i = 0; i < keys.length; i++) {
  414. var k = keys[i];
  415. if (p[k] !== undefined) {
  416. x[k] = p[k];
  417. }
  418. }
  419. }
  420. return x;
  421. }
  422. function qs() {
  423. var q = [];
  424. for (var i = 0; i < arguments.length; i++) {
  425. var p = arguments[i];
  426. for (var k in p) {
  427. q.push(euc(k) + '=' + euc(p[k]));
  428. }
  429. }
  430. return q.join('&');
  431. }
  432. function error(message) {
  433. if (window.console && window.console.error) {
  434. window.console.error('[domainr] ' + message);
  435. }
  436. }
  437. function uniq(a) {
  438. if (!a) {
  439. return;
  440. }
  441. var i, j;
  442. for (i = 0; i < a.length; i++) {
  443. for (j = i + 1; j < a.length; j++) {
  444. if (a[i] === a[j]) {
  445. a.splice(j, 1);
  446. j--;
  447. }
  448. }
  449. }
  450. }
  451. module.exports = {
  452. euc: euc,
  453. extract: extract,
  454. qs: qs,
  455. error: error,
  456. uniq: uniq
  457. };
  458. },{}]},{},[3])(3)
  459. });