⌈⌋ ⎇ branch:  Bitrhythm


Hex Artifact Content

Artifact abc4c4c57b0c99e2263f45740c66500e72d4c4b321ecfee01fca52ad5c15f0ed:


0000: 69 6d 70 6f 72 74 20 6f 62 73 65 72 76 61 62 6c  import observabl
0010: 65 20 66 72 6f 6d 20 27 72 69 6f 74 2d 6f 62 73  e from 'riot-obs
0020: 65 72 76 61 62 6c 65 27 0a 69 6d 70 6f 72 74 20  ervable'.import 
0030: 6d 6b 64 6f 6d 20 66 72 6f 6d 20 27 2e 2f 6d 6b  mkdom from './mk
0040: 64 6f 6d 27 0a 69 6d 70 6f 72 74 20 73 65 74 74  dom'.import sett
0050: 69 6e 67 73 20 66 72 6f 6d 20 27 2e 2e 2f 2e 2e  ings from '../..
0060: 2f 73 65 74 74 69 6e 67 73 27 0a 69 6d 70 6f 72  /settings'.impor
0070: 74 20 69 73 53 76 67 20 66 72 6f 6d 20 27 2e 2f  t isSvg from './
0080: 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69 6c 2f 63  ../common/util/c
0090: 68 65 63 6b 73 2f 69 73 2d 73 76 67 27 0a 69 6d  hecks/is-svg'.im
00a0: 70 6f 72 74 20 65 78 74 65 6e 64 20 66 72 6f 6d  port extend from
00b0: 20 27 2e 2f 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74   './../common/ut
00c0: 69 6c 2f 6d 69 73 63 2f 65 78 74 65 6e 64 27 0a  il/misc/extend'.
00d0: 69 6d 70 6f 72 74 20 75 69 64 20 66 72 6f 6d 20  import uid from 
00e0: 27 2e 2f 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69  './../common/uti
00f0: 6c 2f 6d 69 73 63 2f 75 69 64 27 0a 69 6d 70 6f  l/misc/uid'.impo
0100: 72 74 20 64 65 66 69 6e 65 20 66 72 6f 6d 20 27  rt define from '
0110: 2e 2f 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69 6c  ./../common/util
0120: 2f 6d 69 73 63 2f 64 65 66 69 6e 65 27 0a 69 6d  /misc/define'.im
0130: 70 6f 72 74 20 67 65 74 54 61 67 4e 61 6d 65 20  port getTagName 
0140: 66 72 6f 6d 20 27 2e 2f 2e 2e 2f 63 6f 6d 6d 6f  from './../commo
0150: 6e 2f 75 74 69 6c 2f 74 61 67 73 2f 67 65 74 2d  n/util/tags/get-
0160: 6e 61 6d 65 27 0a 69 6d 70 6f 72 74 20 63 6f 6d  name'.import com
0170: 70 6f 6e 65 6e 74 55 70 64 61 74 65 20 66 72 6f  ponentUpdate fro
0180: 6d 20 27 2e 2f 63 6f 6d 70 6f 6e 65 6e 74 2f 75  m './component/u
0190: 70 64 61 74 65 27 0a 69 6d 70 6f 72 74 20 63 6f  pdate'.import co
01a0: 6d 70 6f 6e 65 6e 74 4d 69 78 69 6e 20 66 72 6f  mponentMixin fro
01b0: 6d 20 27 2e 2f 63 6f 6d 70 6f 6e 65 6e 74 2f 6d  m './component/m
01c0: 69 78 69 6e 27 0a 69 6d 70 6f 72 74 20 63 6f 6d  ixin'.import com
01d0: 70 6f 6e 65 6e 74 4d 6f 75 6e 74 20 66 72 6f 6d  ponentMount from
01e0: 20 27 2e 2f 63 6f 6d 70 6f 6e 65 6e 74 2f 6d 6f   './component/mo
01f0: 75 6e 74 27 0a 69 6d 70 6f 72 74 20 63 6f 6d 70  unt'.import comp
0200: 6f 6e 65 6e 74 55 6e 6d 6f 75 6e 74 20 66 72 6f  onentUnmount fro
0210: 6d 20 27 2e 2f 63 6f 6d 70 6f 6e 65 6e 74 2f 75  m './component/u
0220: 6e 6d 6f 75 6e 74 27 0a 0a 2f 2a 2a 0a 20 2a 20  nmount'../**. * 
0230: 54 61 67 20 63 72 65 61 74 69 6f 6e 20 66 61 63  Tag creation fac
0240: 74 6f 72 79 20 66 75 6e 63 74 69 6f 6e 0a 20 2a  tory function. *
0250: 20 40 63 6f 6e 73 74 72 75 63 74 6f 72 0a 20 2a   @constructor. *
0260: 20 40 70 61 72 61 6d 20 7b 20 4f 62 6a 65 63 74   @param { Object
0270: 20 7d 20 69 6d 70 6c 20 2d 20 69 74 20 63 6f 6e   } impl - it con
0280: 74 61 69 6e 73 20 74 68 65 20 74 61 67 20 74 65  tains the tag te
0290: 6d 70 6c 61 74 65 2c 20 61 6e 64 20 6c 6f 67 69  mplate, and logi
02a0: 63 0a 20 2a 20 40 70 61 72 61 6d 20 7b 20 4f 62  c. * @param { Ob
02b0: 6a 65 63 74 20 7d 20 63 6f 6e 66 20 2d 20 74 61  ject } conf - ta
02c0: 67 20 6f 70 74 69 6f 6e 73 0a 20 2a 20 40 70 61  g options. * @pa
02d0: 72 61 6d 20 7b 20 53 74 72 69 6e 67 20 7d 20 69  ram { String } i
02e0: 6e 6e 65 72 48 54 4d 4c 20 2d 20 68 74 6d 6c 20  nnerHTML - html 
02f0: 74 68 61 74 20 65 76 65 6e 74 75 61 6c 6c 79 20  that eventually 
0300: 77 65 20 6e 65 65 64 20 74 6f 20 69 6e 6a 65 63  we need to injec
0310: 74 20 69 6e 20 74 68 65 20 74 61 67 0a 20 2a 2f  t in the tag. */
0320: 0a 65 78 70 6f 72 74 20 64 65 66 61 75 6c 74 20  .export default 
0330: 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 65 54  function createT
0340: 61 67 28 69 6d 70 6c 20 3d 20 7b 7d 2c 20 63 6f  ag(impl = {}, co
0350: 6e 66 20 3d 20 7b 7d 2c 20 69 6e 6e 65 72 48 54  nf = {}, innerHT
0360: 4d 4c 29 20 7b 0a 20 20 63 6f 6e 73 74 20 74 61  ML) {.  const ta
0370: 67 20 3d 20 63 6f 6e 66 2e 63 6f 6e 74 65 78 74  g = conf.context
0380: 20 7c 7c 20 7b 7d 0a 20 20 63 6f 6e 73 74 20 6f   || {}.  const o
0390: 70 74 73 20 3d 20 63 6f 6e 66 2e 6f 70 74 73 20  pts = conf.opts 
03a0: 7c 7c 20 7b 7d 0a 20 20 63 6f 6e 73 74 20 70 61  || {}.  const pa
03b0: 72 65 6e 74 20 3d 20 63 6f 6e 66 2e 70 61 72 65  rent = conf.pare
03c0: 6e 74 0a 20 20 63 6f 6e 73 74 20 69 73 4c 6f 6f  nt.  const isLoo
03d0: 70 20 3d 20 63 6f 6e 66 2e 69 73 4c 6f 6f 70 0a  p = conf.isLoop.
03e0: 20 20 63 6f 6e 73 74 20 69 73 41 6e 6f 6e 79 6d    const isAnonym
03f0: 6f 75 73 20 3d 20 21 21 63 6f 6e 66 2e 69 73 41  ous = !!conf.isA
0400: 6e 6f 6e 79 6d 6f 75 73 0a 20 20 63 6f 6e 73 74  nonymous.  const
0410: 20 73 6b 69 70 41 6e 6f 6e 79 6d 6f 75 73 20 3d   skipAnonymous =
0420: 20 73 65 74 74 69 6e 67 73 2e 73 6b 69 70 41 6e   settings.skipAn
0430: 6f 6e 79 6d 6f 75 73 54 61 67 73 20 26 26 20 69  onymousTags && i
0440: 73 41 6e 6f 6e 79 6d 6f 75 73 0a 20 20 63 6f 6e  sAnonymous.  con
0450: 73 74 20 69 74 65 6d 20 3d 20 63 6f 6e 66 2e 69  st item = conf.i
0460: 74 65 6d 0a 20 20 2f 2f 20 61 76 61 69 6c 61 62  tem.  // availab
0470: 6c 65 20 6f 6e 6c 79 20 66 6f 72 20 74 68 65 20  le only for the 
0480: 6c 6f 6f 70 65 64 20 6e 6f 64 65 73 0a 20 20 63  looped nodes.  c
0490: 6f 6e 73 74 20 69 6e 64 65 78 20 3d 20 63 6f 6e  onst index = con
04a0: 66 2e 69 6e 64 65 78 0a 20 20 2f 2f 20 41 6c 6c  f.index.  // All
04b0: 20 61 74 74 72 69 62 75 74 65 73 20 6f 6e 20 74   attributes on t
04c0: 68 65 20 54 61 67 20 77 68 65 6e 20 69 74 27 73  he Tag when it's
04d0: 20 66 69 72 73 74 20 70 61 72 73 65 64 0a 20 20   first parsed.  
04e0: 63 6f 6e 73 74 20 69 6e 73 74 41 74 74 72 73 20  const instAttrs 
04f0: 3d 20 5b 5d 0a 20 20 2f 2f 20 65 78 70 72 65 73  = [].  // expres
0500: 73 69 6f 6e 73 20 6f 6e 20 74 68 69 73 20 74 79  sions on this ty
0510: 70 65 20 6f 66 20 54 61 67 0a 20 20 63 6f 6e 73  pe of Tag.  cons
0520: 74 20 69 6d 70 6c 41 74 74 72 73 20 3d 20 5b 5d  t implAttrs = []
0530: 0a 20 20 63 6f 6e 73 74 20 74 6d 70 6c 20 3d 20  .  const tmpl = 
0540: 69 6d 70 6c 2e 74 6d 70 6c 0a 20 20 63 6f 6e 73  impl.tmpl.  cons
0550: 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 3d 20  t expressions = 
0560: 5b 5d 0a 20 20 63 6f 6e 73 74 20 72 6f 6f 74 20  [].  const root 
0570: 3d 20 63 6f 6e 66 2e 72 6f 6f 74 0a 20 20 63 6f  = conf.root.  co
0580: 6e 73 74 20 74 61 67 4e 61 6d 65 20 3d 20 63 6f  nst tagName = co
0590: 6e 66 2e 74 61 67 4e 61 6d 65 20 7c 7c 20 67 65  nf.tagName || ge
05a0: 74 54 61 67 4e 61 6d 65 28 72 6f 6f 74 29 0a 20  tTagName(root). 
05b0: 20 63 6f 6e 73 74 20 69 73 56 69 72 74 75 61 6c   const isVirtual
05c0: 20 3d 20 74 61 67 4e 61 6d 65 20 3d 3d 3d 20 27   = tagName === '
05d0: 76 69 72 74 75 61 6c 27 0a 20 20 63 6f 6e 73 74  virtual'.  const
05e0: 20 69 73 49 6e 6c 69 6e 65 20 3d 20 21 69 73 56   isInline = !isV
05f0: 69 72 74 75 61 6c 20 26 26 20 21 74 6d 70 6c 0a  irtual && !tmpl.
0600: 20 20 6c 65 74 20 64 6f 6d 0a 0a 20 20 69 66 20    let dom..  if 
0610: 28 69 73 49 6e 6c 69 6e 65 20 7c 7c 20 69 73 4c  (isInline || isL
0620: 6f 6f 70 20 26 26 20 69 73 41 6e 6f 6e 79 6d 6f  oop && isAnonymo
0630: 75 73 29 20 7b 0a 20 20 20 20 64 6f 6d 20 3d 20  us) {.    dom = 
0640: 72 6f 6f 74 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  root.  } else {.
0650: 20 20 20 20 69 66 20 28 21 69 73 56 69 72 74 75      if (!isVirtu
0660: 61 6c 29 20 72 6f 6f 74 2e 69 6e 6e 65 72 48 54  al) root.innerHT
0670: 4d 4c 20 3d 20 27 27 0a 20 20 20 20 64 6f 6d 20  ML = ''.    dom 
0680: 3d 20 6d 6b 64 6f 6d 28 74 6d 70 6c 2c 20 69 6e  = mkdom(tmpl, in
0690: 6e 65 72 48 54 4d 4c 2c 20 69 73 53 76 67 28 72  nerHTML, isSvg(r
06a0: 6f 6f 74 29 29 0a 20 20 7d 0a 0a 20 20 2f 2f 20  oot)).  }..  // 
06b0: 6d 61 6b 65 20 74 68 69 73 20 74 61 67 20 6f 62  make this tag ob
06c0: 73 65 72 76 61 62 6c 65 0a 20 20 69 66 20 28 21  servable.  if (!
06d0: 73 6b 69 70 41 6e 6f 6e 79 6d 6f 75 73 29 20 6f  skipAnonymous) o
06e0: 62 73 65 72 76 61 62 6c 65 28 74 61 67 29 0a 0a  bservable(tag)..
06f0: 20 20 2f 2f 20 6f 6e 6c 79 20 63 61 6c 6c 20 75    // only call u
0700: 6e 6d 6f 75 6e 74 20 69 66 20 77 65 20 68 61 76  nmount if we hav
0710: 65 20 61 20 76 61 6c 69 64 20 5f 5f 54 41 47 5f  e a valid __TAG_
0720: 49 4d 50 4c 20 28 68 61 73 20 6e 61 6d 65 20 70  IMPL (has name p
0730: 72 6f 70 65 72 74 79 29 0a 20 20 69 66 20 28 69  roperty).  if (i
0740: 6d 70 6c 2e 6e 61 6d 65 20 26 26 20 72 6f 6f 74  mpl.name && root
0750: 2e 5f 74 61 67 29 20 72 6f 6f 74 2e 5f 74 61 67  ._tag) root._tag
0760: 2e 75 6e 6d 6f 75 6e 74 28 74 72 75 65 29 0a 0a  .unmount(true)..
0770: 20 20 64 65 66 69 6e 65 28 74 61 67 2c 20 27 5f    define(tag, '_
0780: 5f 27 2c 20 7b 0a 20 20 20 20 69 6d 70 6c 2c 0a  _', {.    impl,.
0790: 20 20 20 20 72 6f 6f 74 2c 0a 20 20 20 20 73 6b      root,.    sk
07a0: 69 70 41 6e 6f 6e 79 6d 6f 75 73 2c 0a 20 20 20  ipAnonymous,.   
07b0: 20 69 6d 70 6c 41 74 74 72 73 2c 0a 20 20 20 20   implAttrs,.    
07c0: 69 73 41 6e 6f 6e 79 6d 6f 75 73 2c 0a 20 20 20  isAnonymous,.   
07d0: 20 69 6e 73 74 41 74 74 72 73 2c 0a 20 20 20 20   instAttrs,.    
07e0: 69 6e 6e 65 72 48 54 4d 4c 2c 0a 20 20 20 20 74  innerHTML,.    t
07f0: 61 67 4e 61 6d 65 2c 0a 20 20 20 20 69 6e 64 65  agName,.    inde
0800: 78 2c 0a 20 20 20 20 69 73 4c 6f 6f 70 2c 0a 20  x,.    isLoop,. 
0810: 20 20 20 69 73 49 6e 6c 69 6e 65 2c 0a 20 20 20     isInline,.   
0820: 20 69 74 65 6d 2c 0a 20 20 20 20 70 61 72 65 6e   item,.    paren
0830: 74 2c 0a 20 20 20 20 2f 2f 20 74 61 67 73 20 68  t,.    // tags h
0840: 61 76 69 6e 67 20 65 76 65 6e 74 20 6c 69 73 74  aving event list
0850: 65 6e 65 72 73 0a 20 20 20 20 2f 2f 20 69 74 20  eners.    // it 
0860: 77 6f 75 6c 64 20 62 65 20 62 65 74 74 65 72 20  would be better 
0870: 74 6f 20 75 73 65 20 77 65 61 6b 20 6d 61 70 73  to use weak maps
0880: 20 68 65 72 65 20 62 75 74 20 77 65 20 63 61 6e   here but we can
0890: 20 6e 6f 74 20 69 6e 74 72 6f 64 75 63 65 20 62   not introduce b
08a0: 72 65 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 20  reaking changes 
08b0: 6e 6f 77 0a 20 20 20 20 6c 69 73 74 65 6e 65 72  now.    listener
08c0: 73 3a 20 5b 5d 2c 0a 20 20 20 20 2f 2f 20 74 68  s: [],.    // th
08d0: 65 73 65 20 76 61 72 73 20 77 69 6c 6c 20 62 65  ese vars will be
08e0: 20 6e 65 65 64 65 64 20 6f 6e 6c 79 20 66 6f 72   needed only for
08f0: 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 67   the virtual tag
0900: 73 0a 20 20 20 20 76 69 72 74 73 3a 20 5b 5d 2c  s.    virts: [],
0910: 0a 20 20 20 20 77 61 73 43 72 65 61 74 65 64 3a  .    wasCreated:
0920: 20 66 61 6c 73 65 2c 0a 20 20 20 20 74 61 69 6c   false,.    tail
0930: 3a 20 6e 75 6c 6c 2c 0a 20 20 20 20 68 65 61 64  : null,.    head
0940: 3a 20 6e 75 6c 6c 0a 20 20 7d 29 0a 0a 20 20 2f  : null.  })..  /
0950: 2f 20 74 61 67 20 70 72 6f 74 65 63 74 65 64 20  / tag protected 
0960: 70 72 6f 70 65 72 74 69 65 73 0a 20 20 72 65 74  properties.  ret
0970: 75 72 6e 20 5b 0a 20 20 20 20 5b 27 69 73 4d 6f  urn [.    ['isMo
0980: 75 6e 74 65 64 27 2c 20 66 61 6c 73 65 5d 2c 0a  unted', false],.
0990: 20 20 20 20 2f 2f 20 63 72 65 61 74 65 20 61 20      // create a 
09a0: 75 6e 69 71 75 65 20 69 64 20 74 6f 20 74 68 69  unique id to thi
09b0: 73 20 74 61 67 0a 20 20 20 20 2f 2f 20 69 74 20  s tag.    // it 
09c0: 63 6f 75 6c 64 20 62 65 20 68 61 6e 64 79 20 74  could be handy t
09d0: 6f 20 75 73 65 20 69 74 20 61 6c 73 6f 20 74 6f  o use it also to
09e0: 20 69 6d 70 72 6f 76 65 20 74 68 65 20 76 69 72   improve the vir
09f0: 74 75 61 6c 20 64 6f 6d 20 72 65 6e 64 65 72 69  tual dom renderi
0a00: 6e 67 20 73 70 65 65 64 0a 20 20 20 20 5b 27 5f  ng speed.    ['_
0a10: 72 69 6f 74 5f 69 64 27 2c 20 75 69 64 28 29 5d  riot_id', uid()]
0a20: 2c 0a 20 20 20 20 5b 27 72 6f 6f 74 27 2c 20 72  ,.    ['root', r
0a30: 6f 6f 74 5d 2c 0a 20 20 20 20 5b 27 6f 70 74 73  oot],.    ['opts
0a40: 27 2c 20 6f 70 74 73 2c 20 7b 20 77 72 69 74 61  ', opts, { writa
0a50: 62 6c 65 3a 20 74 72 75 65 2c 20 65 6e 75 6d 65  ble: true, enume
0a60: 72 61 62 6c 65 3a 20 74 72 75 65 20 7d 5d 2c 0a  rable: true }],.
0a70: 20 20 20 20 5b 27 70 61 72 65 6e 74 27 2c 20 70      ['parent', p
0a80: 61 72 65 6e 74 20 7c 7c 20 6e 75 6c 6c 5d 2c 0a  arent || null],.
0a90: 20 20 20 20 2f 2f 20 70 72 6f 74 65 63 74 20 74      // protect t
0aa0: 68 65 20 22 74 61 67 73 22 20 61 6e 64 20 22 72  he "tags" and "r
0ab0: 65 66 73 22 20 70 72 6f 70 65 72 74 79 20 66 72  efs" property fr
0ac0: 6f 6d 20 62 65 69 6e 67 20 6f 76 65 72 72 69 64  om being overrid
0ad0: 64 65 6e 0a 20 20 20 20 5b 27 74 61 67 73 27 2c  den.    ['tags',
0ae0: 20 7b 7d 5d 2c 0a 20 20 20 20 5b 27 72 65 66 73   {}],.    ['refs
0af0: 27 2c 20 7b 7d 5d 2c 0a 20 20 20 20 5b 27 75 70  ', {}],.    ['up
0b00: 64 61 74 65 27 2c 20 64 61 74 61 20 3d 3e 20 63  date', data => c
0b10: 6f 6d 70 6f 6e 65 6e 74 55 70 64 61 74 65 28 74  omponentUpdate(t
0b20: 61 67 2c 20 64 61 74 61 2c 20 65 78 70 72 65 73  ag, data, expres
0b30: 73 69 6f 6e 73 29 5d 2c 0a 20 20 20 20 5b 27 6d  sions)],.    ['m
0b40: 69 78 69 6e 27 2c 20 28 2e 2e 2e 6d 69 78 69 6e  ixin', (...mixin
0b50: 73 29 20 3d 3e 20 63 6f 6d 70 6f 6e 65 6e 74 4d  s) => componentM
0b60: 69 78 69 6e 28 74 61 67 2c 20 2e 2e 2e 6d 69 78  ixin(tag, ...mix
0b70: 69 6e 73 29 5d 2c 0a 20 20 20 20 5b 27 6d 6f 75  ins)],.    ['mou
0b80: 6e 74 27 2c 20 28 29 20 3d 3e 20 63 6f 6d 70 6f  nt', () => compo
0b90: 6e 65 6e 74 4d 6f 75 6e 74 28 74 61 67 2c 20 64  nentMount(tag, d
0ba0: 6f 6d 2c 20 65 78 70 72 65 73 73 69 6f 6e 73 2c  om, expressions,
0bb0: 20 6f 70 74 73 29 5d 2c 0a 20 20 20 20 5b 27 75   opts)],.    ['u
0bc0: 6e 6d 6f 75 6e 74 27 2c 20 6d 75 73 74 4b 65 65  nmount', mustKee
0bd0: 70 52 6f 6f 74 20 3d 3e 20 63 6f 6d 70 6f 6e 65  pRoot => compone
0be0: 6e 74 55 6e 6d 6f 75 6e 74 28 74 61 67 2c 20 6d  ntUnmount(tag, m
0bf0: 75 73 74 4b 65 65 70 52 6f 6f 74 2c 20 65 78 70  ustKeepRoot, exp
0c00: 72 65 73 73 69 6f 6e 73 29 5d 0a 20 20 5d 2e 72  ressions)].  ].r
0c10: 65 64 75 63 65 28 28 61 63 63 2c 20 5b 6b 65 79  educe((acc, [key
0c20: 2c 20 76 61 6c 75 65 2c 20 6f 70 74 73 5d 29 20  , value, opts]) 
0c30: 3d 3e 20 7b 0a 20 20 20 20 64 65 66 69 6e 65 28  => {.    define(
0c40: 74 61 67 2c 20 6b 65 79 2c 20 76 61 6c 75 65 2c  tag, key, value,
0c50: 20 6f 70 74 73 29 0a 20 20 20 20 72 65 74 75 72   opts).    retur
0c60: 6e 20 61 63 63 0a 20 20 7d 2c 20 65 78 74 65 6e  n acc.  }, exten
0c70: 64 28 74 61 67 2c 20 69 74 65 6d 29 29 0a 7d 0a  d(tag, item)).}.