⌈⌋ ⎇ branch:  Bitrhythm


Hex Artifact Content

Artifact 320e49406c1a453edaa0bb9f92fcbbfb78c20614114469c7e5b1c7579d00f152:


0000: 27 75 73 65 20 73 74 72 69 63 74 27 3b 0a 0a 69  'use strict';..i
0010: 6d 70 6f 72 74 20 64 6f 6d 20 66 72 6f 6d 20 27  mport dom from '
0020: 2e 2e 2f 75 74 69 6c 2f 64 6f 6d 27 3b 0a 69 6d  ../util/dom';.im
0030: 70 6f 72 74 20 49 6e 74 65 72 66 61 63 65 73 20  port Interfaces 
0040: 66 72 6f 6d 20 27 2e 2e 2f 69 6e 74 65 72 66 61  from '../interfa
0050: 63 65 73 2f 27 3b 0a 0a 6c 65 74 20 63 72 65 61  ces/';..let crea
0060: 74 65 49 6e 74 65 72 66 61 63 65 49 44 20 3d 20  teInterfaceID = 
0070: 28 77 69 64 67 65 74 2c 69 6e 74 65 72 66 61 63  (widget,interfac
0080: 65 49 44 73 29 20 3d 3e 20 7b 0a 20 20 6c 65 74  eIDs) => {.  let
0090: 20 74 79 70 65 20 3d 20 77 69 64 67 65 74 2e 74   type = widget.t
00a0: 79 70 65 3b 0a 20 20 69 66 20 28 69 6e 74 65 72  ype;.  if (inter
00b0: 66 61 63 65 49 44 73 5b 74 79 70 65 5d 29 20 7b  faceIDs[type]) {
00c0: 0a 20 20 20 20 69 6e 74 65 72 66 61 63 65 49 44  .    interfaceID
00d0: 73 5b 74 79 70 65 5d 2b 2b 3b 0a 20 20 7d 20 65  s[type]++;.  } e
00e0: 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 65 72 66  lse {.    interf
00f0: 61 63 65 49 44 73 5b 74 79 70 65 5d 20 3d 20 31  aceIDs[type] = 1
0100: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
0110: 20 74 79 70 65 20 2b 20 69 6e 74 65 72 66 61 63   type + interfac
0120: 65 49 44 73 5b 74 79 70 65 5d 20 29 3b 0a 7d 3b  eIDs[type] );.};
0130: 0a 0a 6c 65 74 20 65 6c 65 6d 65 6e 74 20 3d 20  ..let element = 
0140: 28 65 6c 65 6d 65 6e 74 2c 74 79 70 65 2c 6f 70  (element,type,op
0150: 74 69 6f 6e 73 29 20 3d 3e 20 7b 0a 20 20 6f 70  tions) => {.  op
0160: 74 69 6f 6e 73 20 3d 20 6f 70 74 69 6f 6e 73 20  tions = options 
0170: 7c 7c 20 7b 7d 3b 0a 20 20 66 6f 72 20 28 6c 65  || {};.  for (le
0180: 74 20 69 20 3d 20 30 3b 20 69 20 3c 20 65 6c 65  t i = 0; i < ele
0190: 6d 65 6e 74 2e 61 74 74 72 69 62 75 74 65 73 2e  ment.attributes.
01a0: 6c 65 6e 67 74 68 3b 20 69 2b 2b 29 7b 0a 20 20  length; i++){.  
01b0: 20 20 6c 65 74 20 61 74 74 20 3d 20 65 6c 65 6d    let att = elem
01c0: 65 6e 74 2e 61 74 74 72 69 62 75 74 65 73 5b 69  ent.attributes[i
01d0: 5d 3b 0a 20 20 2f 2f 20 20 74 72 79 20 7b 0a 20  ];.  //  try {. 
01e0: 20 2f 2f 20 20 20 20 6f 70 74 69 6f 6e 73 5b 61   //    options[a
01f0: 74 74 2e 6e 6f 64 65 4e 61 6d 65 5d 20 3d 20 65  tt.nodeName] = e
0200: 76 61 6c 28 61 74 74 2e 6e 6f 64 65 56 61 6c 75  val(att.nodeValu
0210: 65 29 3b 0a 20 20 2f 2f 20 20 7d 20 63 61 74 63  e);.  //  } catc
0220: 68 28 65 29 20 7b 0a 20 20 20 20 20 20 6f 70 74  h(e) {.      opt
0230: 69 6f 6e 73 5b 61 74 74 2e 6e 6f 64 65 4e 61 6d  ions[att.nodeNam
0240: 65 5d 20 3d 20 61 74 74 2e 6e 6f 64 65 56 61 6c  e] = att.nodeVal
0250: 75 65 3b 0a 20 20 2f 2f 20 20 7d 0a 20 20 7d 0a  ue;.  //  }.  }.
0260: 20 20 74 79 70 65 20 3d 20 74 79 70 65 5b 30 5d    type = type[0]
0270: 2e 74 6f 55 70 70 65 72 43 61 73 65 28 29 20 2b  .toUpperCase() +
0280: 20 74 79 70 65 2e 73 6c 69 63 65 28 31 29 3b 0a   type.slice(1);.
0290: 20 20 6c 65 74 20 77 69 64 67 65 74 20 3d 20 6e    let widget = n
02a0: 65 77 20 49 6e 74 65 72 66 61 63 65 73 5b 74 79  ew Interfaces[ty
02b0: 70 65 5d 28 65 6c 65 6d 65 6e 74 2c 6f 70 74 69  pe](element,opti
02c0: 6f 6e 73 29 3b 0a 20 20 77 69 64 67 65 74 2e 69  ons);.  widget.i
02d0: 64 20 3d 20 65 6c 65 6d 65 6e 74 2e 69 64 3b 0a  d = element.id;.
02e0: 20 20 72 65 74 75 72 6e 20 77 69 64 67 65 74 3b    return widget;
02f0: 0a 7d 3b 0a 0a 0a 6c 65 74 20 73 65 63 74 69 6f  .};...let sectio
0300: 6e 20 3d 20 28 70 61 72 65 6e 74 2c 6b 65 79 77  n = (parent,keyw
0310: 6f 72 64 29 20 3d 3e 20 7b 0a 0a 20 20 6b 65 79  ord) => {..  key
0320: 77 6f 72 64 20 3d 20 6b 65 79 77 6f 72 64 20 7c  word = keyword |
0330: 7c 20 27 6e 65 78 75 73 2d 75 69 27 3b 0a 0a 20  | 'nexus-ui';.. 
0340: 20 6c 65 74 20 69 6e 74 65 72 66 61 63 65 49 44   let interfaceID
0350: 73 20 3d 20 7b 7d 3b 0a 0a 20 20 6c 65 74 20 63  s = {};..  let c
0360: 6f 6e 74 61 69 6e 65 72 20 3d 20 64 6f 6d 2e 70  ontainer = dom.p
0370: 61 72 73 65 45 6c 65 6d 65 6e 74 28 70 61 72 65  arseElement(pare
0380: 6e 74 29 3b 0a 0a 20 20 6c 65 74 20 75 69 20 3d  nt);..  let ui =
0390: 20 7b 7d 3b 0a 0a 20 20 6c 65 74 20 68 74 6d 6c   {};..  let html
03a0: 45 6c 65 6d 65 6e 74 73 20 3d 20 63 6f 6e 74 61  Elements = conta
03b0: 69 6e 65 72 2e 67 65 74 45 6c 65 6d 65 6e 74 73  iner.getElements
03c0: 42 79 54 61 67 4e 61 6d 65 28 27 2a 27 29 3b 0a  ByTagName('*');.
03d0: 20 20 6c 65 74 20 65 6c 65 6d 65 6e 74 73 20 3d    let elements =
03e0: 20 5b 5d 3b 0a 20 20 66 6f 72 20 28 6c 65 74 20   [];.  for (let 
03f0: 69 3d 30 3b 20 69 3c 68 74 6d 6c 45 6c 65 6d 65  i=0; i<htmlEleme
0400: 6e 74 73 2e 6c 65 6e 67 74 68 3b 20 69 2b 2b 29  nts.length; i++)
0410: 20 7b 0a 20 20 20 20 65 6c 65 6d 65 6e 74 73 2e   {.    elements.
0420: 70 75 73 68 28 68 74 6d 6c 45 6c 65 6d 65 6e 74  push(htmlElement
0430: 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72  s[i]);.  }.  for
0440: 20 28 6c 65 74 20 69 3d 30 3b 69 3c 65 6c 65 6d   (let i=0;i<elem
0450: 65 6e 74 73 2e 6c 65 6e 67 74 68 3b 69 2b 2b 29  ents.length;i++)
0460: 20 7b 0a 20 20 20 20 6c 65 74 20 74 79 70 65 20   {.    let type 
0470: 3d 20 65 6c 65 6d 65 6e 74 73 5b 69 5d 2e 67 65  = elements[i].ge
0480: 74 41 74 74 72 69 62 75 74 65 28 6b 65 79 77 6f  tAttribute(keywo
0490: 72 64 29 3b 0a 20 20 20 20 69 66 20 28 74 79 70  rd);.    if (typ
04a0: 65 29 20 7b 0a 20 20 20 20 20 20 6c 65 74 20 66  e) {.      let f
04b0: 6f 72 6d 61 74 74 65 64 54 79 70 65 20 3d 20 66  ormattedType = f
04c0: 61 6c 73 65 3b 0a 20 20 20 20 20 20 66 6f 72 20  alse;.      for 
04d0: 28 6c 65 74 20 6b 65 79 20 69 6e 20 49 6e 74 65  (let key in Inte
04e0: 72 66 61 63 65 73 29 20 7b 0a 20 20 20 20 20 20  rfaces) {.      
04f0: 20 20 69 66 20 28 74 79 70 65 2e 74 6f 4c 6f 77    if (type.toLow
0500: 65 72 43 61 73 65 28 29 3d 3d 3d 6b 65 79 2e 74  erCase()===key.t
0510: 6f 4c 6f 77 65 72 43 61 73 65 28 29 29 20 7b 0a  oLowerCase()) {.
0520: 20 20 20 20 20 20 20 20 20 20 66 6f 72 6d 61 74            format
0530: 74 65 64 54 79 70 65 20 3d 20 6b 65 79 3b 0a 20  tedType = key;. 
0540: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
0550: 0a 20 20 20 20 20 20 63 6f 6e 73 6f 6c 65 2e 6c  .      console.l
0560: 6f 67 28 66 6f 72 6d 61 74 74 65 64 54 79 70 65  og(formattedType
0570: 29 3b 0a 20 20 20 20 20 20 6c 65 74 20 77 69 64  );.      let wid
0580: 67 65 74 20 3d 20 65 6c 65 6d 65 6e 74 28 65 6c  get = element(el
0590: 65 6d 65 6e 74 73 5b 69 5d 2c 66 6f 72 6d 61 74  ements[i],format
05a0: 74 65 64 54 79 70 65 29 3b 0a 20 20 20 20 20 20  tedType);.      
05b0: 69 66 20 28 77 69 64 67 65 74 2e 69 64 29 20 7b  if (widget.id) {
05c0: 0a 20 20 20 20 20 20 20 20 75 69 5b 77 69 64 67  .        ui[widg
05d0: 65 74 2e 69 64 5d 20 3d 20 77 69 64 67 65 74 3b  et.id] = widget;
05e0: 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  .      } else {.
05f0: 20 20 20 20 20 20 20 20 6c 65 74 20 69 64 20 3d          let id =
0600: 20 63 72 65 61 74 65 49 6e 74 65 72 66 61 63 65   createInterface
0610: 49 44 28 77 69 64 67 65 74 2c 69 6e 74 65 72 66  ID(widget,interf
0620: 61 63 65 49 44 73 29 3b 0a 20 20 20 20 20 20 20  aceIDs);.       
0630: 20 75 69 5b 69 64 5d 20 3d 20 77 69 64 67 65 74   ui[id] = widget
0640: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
0650: 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 75 69    }..  return ui
0660: 3b 0a 0a 7d 3b 0a 0a 6c 65 74 20 61 64 64 20 3d  ;..};..let add =
0670: 20 28 74 79 70 65 2c 70 61 72 65 6e 74 2c 6f 70   (type,parent,op
0680: 74 69 6f 6e 73 29 20 3d 3e 20 7b 0a 20 20 6c 65  tions) => {.  le
0690: 74 20 74 61 72 67 65 74 20 3d 20 64 6f 63 75 6d  t target = docum
06a0: 65 6e 74 2e 63 72 65 61 74 65 45 6c 65 6d 65 6e  ent.createElemen
06b0: 74 28 27 64 69 76 27 29 3b 0a 20 20 6f 70 74 69  t('div');.  opti
06c0: 6f 6e 73 20 3d 20 6f 70 74 69 6f 6e 73 20 7c 7c  ons = options ||
06d0: 20 7b 7d 3b 0a 20 20 69 66 20 28 70 61 72 65 6e   {};.  if (paren
06e0: 74 29 20 7b 0a 20 20 20 20 70 61 72 65 6e 74 20  t) {.    parent 
06f0: 3d 20 64 6f 6d 2e 70 61 72 73 65 45 6c 65 6d 65  = dom.parseEleme
0700: 6e 74 28 70 61 72 65 6e 74 29 3b 0a 20 20 7d 20  nt(parent);.  } 
0710: 65 6c 73 65 20 7b 0a 20 20 20 20 70 61 72 65 6e  else {.    paren
0720: 74 20 3d 20 64 6f 63 75 6d 65 6e 74 2e 62 6f 64  t = document.bod
0730: 79 3b 0a 20 20 7d 0a 20 20 70 61 72 65 6e 74 2e  y;.  }.  parent.
0740: 61 70 70 65 6e 64 43 68 69 6c 64 28 74 61 72 67  appendChild(targ
0750: 65 74 29 3b 0a 20 20 6f 70 74 69 6f 6e 73 2e 74  et);.  options.t
0760: 61 72 67 65 74 20 3d 20 74 61 72 67 65 74 3b 0a  arget = target;.
0770: 20 20 69 66 20 28 6f 70 74 69 6f 6e 73 2e 73 69    if (options.si
0780: 7a 65 29 20 7b 0a 20 20 20 20 74 61 72 67 65 74  ze) {.    target
0790: 2e 73 74 79 6c 65 2e 77 69 64 74 68 20 3d 20 6f  .style.width = o
07a0: 70 74 69 6f 6e 73 2e 73 69 7a 65 5b 30 5d 20 2b  ptions.size[0] +
07b0: 20 27 70 78 27 3b 0a 20 20 20 20 74 61 72 67 65   'px';.    targe
07c0: 74 2e 73 74 79 6c 65 2e 68 65 69 67 68 74 20 3d  t.style.height =
07d0: 20 6f 70 74 69 6f 6e 73 2e 73 69 7a 65 5b 31 5d   options.size[1]
07e0: 20 2b 20 27 70 78 27 3b 0a 20 20 7d 0a 20 20 72   + 'px';.  }.  r
07f0: 65 74 75 72 6e 20 65 6c 65 6d 65 6e 74 28 74 61  eturn element(ta
0800: 72 67 65 74 2c 74 79 70 65 2c 6f 70 74 69 6f 6e  rget,type,option
0810: 73 29 3b 0a 7d 3b 0a 0a 65 78 70 6f 72 74 20 7b  s);.};..export {
0820: 20 65 6c 65 6d 65 6e 74 20 7d 3b 0a 65 78 70 6f   element };.expo
0830: 72 74 20 7b 20 73 65 63 74 69 6f 6e 20 7d 3b 0a  rt { section };.
0840: 65 78 70 6f 72 74 20 7b 20 61 64 64 20 7d 3b 0a  export { add };.