Guest User

Untitled

a guest
Mar 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. module UrlPad {
  2.  
  3. const map = (o: OmniboxFull): CustomOmniboxMap => {
  4. return {
  5. protocol: o.protocol,
  6. host: o.host,
  7. port: o.port,
  8. pathnames: o.pathname.split('/').filter(char => char),
  9. hash: o.hash,
  10. query: o.query
  11. };
  12. }
  13.  
  14. class FormBuilder {
  15.  
  16. private template: string[] = [];
  17. private for: string = '';
  18.  
  19. constructor(forClass?: string) {
  20. this.for = forClass;
  21. this.template.push('<div class="form-group">');
  22. }
  23.  
  24. label(title: string) {
  25. this.template.push(`<label class="col-sm-2">${title}</label>`);
  26. return this;
  27. }
  28.  
  29. input(value: string, id?: string, size: number = 10) {
  30. let id_att = id ? 'id="'+ id +'"' : '';
  31. this.template.push(`<div class="col-sm-${size}">
  32. <input ${id_att} spellcheck="false" type="text" class="form-control ${this.for}" value="${value}"></div>`);
  33. return this;
  34. }
  35.  
  36. build() {
  37. this.template.push('</div>')
  38. return this.template.join('');
  39. }
  40. }
  41.  
  42. export function parse(url: string): string {
  43. let o = map(omnibox.parse(url));
  44.  
  45. return Object.keys(o)
  46. .filter((key, index) => o[key])
  47. .map((key, index) => {
  48. let value = o[key];
  49. switch (key) {
  50. case 'query':
  51. return Object.keys(value)
  52. .map((key, index) => new FormBuilder('query').label(`Querystring #${index}`).input(key, '', 5).input(value[key], '', 5).build())
  53. .join('');
  54.  
  55. case 'pathnames':
  56. return value
  57. .map((value, index) => new FormBuilder('path').label(`Pathname #${index}`).input(value).build())
  58. .join('');
  59.  
  60. default:
  61. return new FormBuilder().label(key).input(value, key).build();
  62. }
  63. })
  64. .join('');
  65. }
  66.  
  67. export function make(o: CustomOmniboxMap): string {
  68. let querystrings = Object.keys(o.query)
  69. .map((key, index) => `${key}=${o.query[key]}`)
  70. .join('&'),
  71. paths = o.pathnames.join('/');
  72.  
  73. return `${o.protocol}://${o.host}${o.port ? ':' + o.port : ''}/${paths}${querystrings ? '?' + querystrings : ''}${o.hash ? o.hash : ''}`;
  74. }
  75.  
  76. }
  77.  
  78. window["UrlPad"] = UrlPad;
Add Comment
Please, Sign In to add comment