⌈⌋ ⎇ branch:  Bitrhythm


Hex Artifact Content

Artifact 7fb3b95cb66c0820e4013995a2471de26c50214e4dd8619c14df30f1ee471cf6:


0000: 69 6d 70 6f 72 74 20 7b 0a 20 20 5f 5f 54 41 47  import {.  __TAG
0010: 53 5f 43 41 43 48 45 2c 0a 20 20 41 54 54 52 53  S_CACHE,.  ATTRS
0020: 5f 50 52 45 46 49 58 2c 0a 20 20 52 49 4f 54 5f  _PREFIX,.  RIOT_
0030: 45 56 45 4e 54 53 5f 4b 45 59 0a 7d 20 66 72 6f  EVENTS_KEY.} fro
0040: 6d 20 27 2e 2f 2e 2e 2f 2e 2e 2f 63 6f 6d 6d 6f  m './../../commo
0050: 6e 2f 67 6c 6f 62 61 6c 2d 76 61 72 69 61 62 6c  n/global-variabl
0060: 65 73 27 0a 0a 69 6d 70 6f 72 74 20 72 65 6d 6f  es'..import remo
0070: 76 65 41 74 74 72 69 62 75 74 65 20 66 72 6f 6d  veAttribute from
0080: 20 27 2e 2f 2e 2e 2f 2e 2e 2f 63 6f 6d 6d 6f 6e   './../../common
0090: 2f 75 74 69 6c 2f 64 6f 6d 2f 72 65 6d 6f 76 65  /util/dom/remove
00a0: 2d 61 74 74 72 69 62 75 74 65 27 0a 69 6d 70 6f  -attribute'.impo
00b0: 72 74 20 77 61 6c 6b 41 74 74 72 69 62 75 74 65  rt walkAttribute
00c0: 73 20 66 72 6f 6d 20 27 2e 2f 2e 2e 2f 2e 2e 2f  s from './../../
00d0: 63 6f 6d 6d 6f 6e 2f 75 74 69 6c 2f 64 6f 6d 2f  common/util/dom/
00e0: 77 61 6c 6b 2d 61 74 74 72 69 62 75 74 65 73 27  walk-attributes'
00f0: 0a 69 6d 70 6f 72 74 20 73 65 74 49 6e 6e 65 72  .import setInner
0100: 48 54 4d 4c 20 66 72 6f 6d 20 27 2e 2f 2e 2e 2f  HTML from './../
0110: 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69 6c 2f 64  ../common/util/d
0120: 6f 6d 2f 73 65 74 2d 69 6e 6e 65 72 2d 48 54 4d  om/set-inner-HTM
0130: 4c 27 0a 69 6d 70 6f 72 74 20 65 61 63 68 20 66  L'.import each f
0140: 72 6f 6d 20 27 2e 2f 2e 2e 2f 2e 2e 2f 63 6f 6d  rom './../../com
0150: 6d 6f 6e 2f 75 74 69 6c 2f 6d 69 73 63 2f 65 61  mon/util/misc/ea
0160: 63 68 27 0a 69 6d 70 6f 72 74 20 73 74 61 72 74  ch'.import start
0170: 73 57 69 74 68 20 66 72 6f 6d 20 27 2e 2f 2e 2e  sWith from './..
0180: 2f 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69 6c 2f  /../common/util/
0190: 6d 69 73 63 2f 73 74 61 72 74 73 2d 77 69 74 68  misc/starts-with
01a0: 27 0a 0a 69 6d 70 6f 72 74 20 75 6e 6d 6f 75 6e  '..import unmoun
01b0: 74 41 6c 6c 20 66 72 6f 6d 20 27 2e 2f 2e 2e 2f  tAll from './../
01c0: 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69 6c 2f 74  ../common/util/t
01d0: 61 67 73 2f 75 6e 6d 6f 75 6e 74 2d 61 6c 6c 27  ags/unmount-all'
01e0: 0a 0a 69 6d 70 6f 72 74 20 61 72 72 61 79 69 73  ..import arrayis
01f0: 68 52 65 6d 6f 76 65 20 66 72 6f 6d 20 27 2e 2f  hRemove from './
0200: 2e 2e 2f 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69  ../../common/uti
0210: 6c 2f 74 61 67 73 2f 61 72 72 61 79 69 73 68 2d  l/tags/arrayish-
0220: 72 65 6d 6f 76 65 27 0a 69 6d 70 6f 72 74 20 67  remove'.import g
0230: 65 74 49 6d 6d 65 64 69 61 74 65 43 75 73 74 6f  etImmediateCusto
0240: 6d 50 61 72 65 6e 74 20 66 72 6f 6d 20 27 2e 2f  mParent from './
0250: 2e 2e 2f 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69  ../../common/uti
0260: 6c 2f 74 61 67 73 2f 67 65 74 2d 69 6d 6d 65 64  l/tags/get-immed
0270: 69 61 74 65 2d 63 75 73 74 6f 6d 2d 70 61 72 65  iate-custom-pare
0280: 6e 74 27 0a 69 6d 70 6f 72 74 20 73 65 74 4d 6f  nt'.import setMo
0290: 75 6e 74 53 74 61 74 65 20 66 72 6f 6d 20 27 2e  untState from '.
02a0: 2f 2e 2e 2f 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74  /../../common/ut
02b0: 69 6c 2f 74 61 67 73 2f 73 65 74 2d 6d 6f 75 6e  il/tags/set-moun
02c0: 74 2d 73 74 61 74 65 27 0a 0a 0a 2f 2a 2a 0a 20  t-state'.../**. 
02d0: 2a 20 55 6e 6d 6f 75 6e 74 20 74 68 65 20 74 61  * Unmount the ta
02e0: 67 20 69 6e 73 74 61 6e 63 65 0a 20 2a 20 40 70  g instance. * @p
02f0: 61 72 61 6d 20 7b 20 42 6f 6f 6c 65 61 6e 20 7d  aram { Boolean }
0300: 20 6d 75 73 74 4b 65 65 70 52 6f 6f 74 20 2d 20   mustKeepRoot - 
0310: 69 66 20 69 74 27 73 20 74 72 75 65 20 74 68 65  if it's true the
0320: 20 72 6f 6f 74 20 6e 6f 64 65 20 77 69 6c 6c 20   root node will 
0330: 6e 6f 74 20 62 65 20 72 65 6d 6f 76 65 64 0a 20  not be removed. 
0340: 2a 20 40 72 65 74 75 72 6e 73 20 7b 20 54 61 67  * @returns { Tag
0350: 20 7d 20 74 68 65 20 63 75 72 72 65 6e 74 20 74   } the current t
0360: 61 67 20 69 6e 73 74 61 6e 63 65 0a 20 2a 2f 0a  ag instance. */.
0370: 65 78 70 6f 72 74 20 64 65 66 61 75 6c 74 20 66  export default f
0380: 75 6e 63 74 69 6f 6e 20 74 61 67 55 6e 6d 6f 75  unction tagUnmou
0390: 6e 74 28 74 61 67 2c 20 6d 75 73 74 4b 65 65 70  nt(tag, mustKeep
03a0: 52 6f 6f 74 2c 20 65 78 70 72 65 73 73 69 6f 6e  Root, expression
03b0: 73 29 20 7b 0a 20 20 63 6f 6e 73 74 20 5f 5f 20  s) {.  const __ 
03c0: 3d 20 74 61 67 2e 5f 5f 0a 20 20 63 6f 6e 73 74  = tag.__.  const
03d0: 20 72 6f 6f 74 20 3d 20 5f 5f 2e 72 6f 6f 74 0a   root = __.root.
03e0: 20 20 63 6f 6e 73 74 20 74 61 67 49 6e 64 65 78    const tagIndex
03f0: 20 3d 20 5f 5f 54 41 47 53 5f 43 41 43 48 45 2e   = __TAGS_CACHE.
0400: 69 6e 64 65 78 4f 66 28 74 61 67 29 0a 20 20 63  indexOf(tag).  c
0410: 6f 6e 73 74 20 70 20 3d 20 72 6f 6f 74 2e 70 61  onst p = root.pa
0420: 72 65 6e 74 4e 6f 64 65 0a 0a 20 20 69 66 20 28  rentNode..  if (
0430: 21 5f 5f 2e 73 6b 69 70 41 6e 6f 6e 79 6d 6f 75  !__.skipAnonymou
0440: 73 29 20 74 61 67 2e 74 72 69 67 67 65 72 28 27  s) tag.trigger('
0450: 62 65 66 6f 72 65 2d 75 6e 6d 6f 75 6e 74 27 29  before-unmount')
0460: 0a 0a 20 20 2f 2f 20 63 6c 65 61 72 20 61 6c 6c  ..  // clear all
0470: 20 61 74 74 72 69 62 75 74 65 73 20 63 6f 6d 69   attributes comi
0480: 6e 67 20 66 72 6f 6d 20 74 68 65 20 6d 6f 75 6e  ng from the moun
0490: 74 65 64 20 74 61 67 0a 20 20 77 61 6c 6b 41 74  ted tag.  walkAt
04a0: 74 72 69 62 75 74 65 73 28 5f 5f 2e 69 6d 70 6c  tributes(__.impl
04b0: 2e 61 74 74 72 73 2c 20 28 6e 61 6d 65 29 20 3d  .attrs, (name) =
04c0: 3e 20 7b 0a 20 20 20 20 69 66 20 28 73 74 61 72  > {.    if (star
04d0: 74 73 57 69 74 68 28 6e 61 6d 65 2c 20 41 54 54  tsWith(name, ATT
04e0: 52 53 5f 50 52 45 46 49 58 29 29 0a 20 20 20 20  RS_PREFIX)).    
04f0: 20 20 6e 61 6d 65 20 3d 20 6e 61 6d 65 2e 73 6c    name = name.sl
0500: 69 63 65 28 41 54 54 52 53 5f 50 52 45 46 49 58  ice(ATTRS_PREFIX
0510: 2e 6c 65 6e 67 74 68 29 0a 0a 20 20 20 20 72 65  .length)..    re
0520: 6d 6f 76 65 41 74 74 72 69 62 75 74 65 28 72 6f  moveAttribute(ro
0530: 6f 74 2c 20 6e 61 6d 65 29 0a 20 20 7d 29 0a 0a  ot, name).  })..
0540: 20 20 2f 2f 20 72 65 6d 6f 76 65 20 61 6c 6c 20    // remove all 
0550: 74 68 65 20 65 76 65 6e 74 20 6c 69 73 74 65 6e  the event listen
0560: 65 72 73 0a 20 20 74 61 67 2e 5f 5f 2e 6c 69 73  ers.  tag.__.lis
0570: 74 65 6e 65 72 73 2e 66 6f 72 45 61 63 68 28 28  teners.forEach((
0580: 64 6f 6d 29 20 3d 3e 20 7b 0a 20 20 20 20 4f 62  dom) => {.    Ob
0590: 6a 65 63 74 2e 6b 65 79 73 28 64 6f 6d 5b 52 49  ject.keys(dom[RI
05a0: 4f 54 5f 45 56 45 4e 54 53 5f 4b 45 59 5d 29 2e  OT_EVENTS_KEY]).
05b0: 66 6f 72 45 61 63 68 28 28 65 76 65 6e 74 4e 61  forEach((eventNa
05c0: 6d 65 29 20 3d 3e 20 7b 0a 20 20 20 20 20 20 64  me) => {.      d
05d0: 6f 6d 2e 72 65 6d 6f 76 65 45 76 65 6e 74 4c 69  om.removeEventLi
05e0: 73 74 65 6e 65 72 28 65 76 65 6e 74 4e 61 6d 65  stener(eventName
05f0: 2c 20 64 6f 6d 5b 52 49 4f 54 5f 45 56 45 4e 54  , dom[RIOT_EVENT
0600: 53 5f 4b 45 59 5d 5b 65 76 65 6e 74 4e 61 6d 65  S_KEY][eventName
0610: 5d 29 0a 20 20 20 20 7d 29 0a 20 20 7d 29 0a 0a  ]).    }).  })..
0620: 20 20 2f 2f 20 72 65 6d 6f 76 65 20 74 61 67 20    // remove tag 
0630: 69 6e 73 74 61 6e 63 65 20 66 72 6f 6d 20 74 68  instance from th
0640: 65 20 67 6c 6f 62 61 6c 20 74 61 67 73 20 63 61  e global tags ca
0650: 63 68 65 20 63 6f 6c 6c 65 63 74 69 6f 6e 0a 20  che collection. 
0660: 20 69 66 20 28 74 61 67 49 6e 64 65 78 20 21 3d   if (tagIndex !=
0670: 3d 20 2d 31 29 20 5f 5f 54 41 47 53 5f 43 41 43  = -1) __TAGS_CAC
0680: 48 45 2e 73 70 6c 69 63 65 28 74 61 67 49 6e 64  HE.splice(tagInd
0690: 65 78 2c 20 31 29 0a 0a 20 20 2f 2f 20 63 6c 65  ex, 1)..  // cle
06a0: 61 6e 20 75 70 20 74 68 65 20 70 61 72 65 6e 74  an up the parent
06b0: 20 74 61 67 73 20 6f 62 6a 65 63 74 0a 20 20 69   tags object.  i
06c0: 66 20 28 5f 5f 2e 70 61 72 65 6e 74 20 26 26 20  f (__.parent && 
06d0: 21 5f 5f 2e 69 73 41 6e 6f 6e 79 6d 6f 75 73 29  !__.isAnonymous)
06e0: 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 70 74 61   {.    const pta
06f0: 67 20 3d 20 67 65 74 49 6d 6d 65 64 69 61 74 65  g = getImmediate
0700: 43 75 73 74 6f 6d 50 61 72 65 6e 74 28 5f 5f 2e  CustomParent(__.
0710: 70 61 72 65 6e 74 29 0a 0a 20 20 20 20 69 66 20  parent)..    if 
0720: 28 5f 5f 2e 69 73 56 69 72 74 75 61 6c 29 20 7b  (__.isVirtual) {
0730: 0a 20 20 20 20 20 20 4f 62 6a 65 63 74 0a 20 20  .      Object.  
0740: 20 20 20 20 20 20 2e 6b 65 79 73 28 74 61 67 2e        .keys(tag.
0750: 74 61 67 73 29 0a 20 20 20 20 20 20 20 20 2e 66  tags).        .f
0760: 6f 72 45 61 63 68 28 74 61 67 4e 61 6d 65 20 3d  orEach(tagName =
0770: 3e 20 61 72 72 61 79 69 73 68 52 65 6d 6f 76 65  > arrayishRemove
0780: 28 70 74 61 67 2e 74 61 67 73 2c 20 74 61 67 4e  (ptag.tags, tagN
0790: 61 6d 65 2c 20 74 61 67 2e 74 61 67 73 5b 74 61  ame, tag.tags[ta
07a0: 67 4e 61 6d 65 5d 29 29 0a 20 20 20 20 7d 20 65  gName])).    } e
07b0: 6c 73 65 20 7b 0a 20 20 20 20 20 20 61 72 72 61  lse {.      arra
07c0: 79 69 73 68 52 65 6d 6f 76 65 28 70 74 61 67 2e  yishRemove(ptag.
07d0: 74 61 67 73 2c 20 5f 5f 2e 74 61 67 4e 61 6d 65  tags, __.tagName
07e0: 2c 20 74 61 67 29 0a 20 20 20 20 7d 0a 20 20 7d  , tag).    }.  }
07f0: 0a 0a 20 20 2f 2f 20 75 6e 6d 6f 75 6e 74 20 61  ..  // unmount a
0800: 6c 6c 20 74 68 65 20 76 69 72 74 75 61 6c 20 64  ll the virtual d
0810: 69 72 65 63 74 69 76 65 73 0a 20 20 69 66 20 28  irectives.  if (
0820: 74 61 67 2e 5f 5f 2e 76 69 72 74 73 29 20 7b 0a  tag.__.virts) {.
0830: 20 20 20 20 65 61 63 68 28 74 61 67 2e 5f 5f 2e      each(tag.__.
0840: 76 69 72 74 73 2c 20 28 76 29 20 3d 3e 20 7b 0a  virts, (v) => {.
0850: 20 20 20 20 20 20 69 66 20 28 76 2e 70 61 72 65        if (v.pare
0860: 6e 74 4e 6f 64 65 29 20 76 2e 70 61 72 65 6e 74  ntNode) v.parent
0870: 4e 6f 64 65 2e 72 65 6d 6f 76 65 43 68 69 6c 64  Node.removeChild
0880: 28 76 29 0a 20 20 20 20 7d 29 0a 20 20 7d 0a 0a  (v).    }).  }..
0890: 20 20 2f 2f 20 61 6c 6c 6f 77 20 65 78 70 72 65    // allow expre
08a0: 73 73 69 6f 6e 73 20 74 6f 20 75 6e 6d 6f 75 6e  ssions to unmoun
08b0: 74 20 74 68 65 6d 73 65 6c 76 65 73 0a 20 20 75  t themselves.  u
08c0: 6e 6d 6f 75 6e 74 41 6c 6c 28 65 78 70 72 65 73  nmountAll(expres
08d0: 73 69 6f 6e 73 29 0a 20 20 65 61 63 68 28 5f 5f  sions).  each(__
08e0: 2e 69 6e 73 74 41 74 74 72 73 2c 20 61 20 3d 3e  .instAttrs, a =>
08f0: 20 61 2e 65 78 70 72 20 26 26 20 61 2e 65 78 70   a.expr && a.exp
0900: 72 2e 75 6e 6d 6f 75 6e 74 20 26 26 20 61 2e 65  r.unmount && a.e
0910: 78 70 72 2e 75 6e 6d 6f 75 6e 74 28 29 29 0a 0a  xpr.unmount())..
0920: 20 20 2f 2f 20 63 6c 65 61 72 20 74 68 65 20 74    // clear the t
0930: 61 67 20 68 74 6d 6c 20 69 66 20 69 74 27 73 20  ag html if it's 
0940: 6e 65 63 65 73 73 61 72 79 0a 20 20 69 66 20 28  necessary.  if (
0950: 6d 75 73 74 4b 65 65 70 52 6f 6f 74 29 20 73 65  mustKeepRoot) se
0960: 74 49 6e 6e 65 72 48 54 4d 4c 28 72 6f 6f 74 2c  tInnerHTML(root,
0970: 20 27 27 29 0a 20 20 2f 2f 20 6f 74 68 65 72 77   '').  // otherw
0980: 69 73 65 20 64 65 74 61 63 68 20 74 68 65 20 72  ise detach the r
0990: 6f 6f 74 20 74 61 67 20 66 72 6f 6d 20 74 68 65  oot tag from the
09a0: 20 44 4f 4d 0a 20 20 65 6c 73 65 20 69 66 20 28   DOM.  else if (
09b0: 70 29 20 70 2e 72 65 6d 6f 76 65 43 68 69 6c 64  p) p.removeChild
09c0: 28 72 6f 6f 74 29 0a 0a 20 20 2f 2f 20 63 75 73  (root)..  // cus
09d0: 74 6f 6d 20 69 6e 74 65 72 6e 61 6c 20 75 6e 6d  tom internal unm
09e0: 6f 75 6e 74 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ount function to
09f0: 20 61 76 6f 69 64 20 72 65 6c 79 69 6e 67 20 6f   avoid relying o
0a00: 6e 20 74 68 65 20 6f 62 73 65 72 76 61 62 6c 65  n the observable
0a10: 0a 20 20 69 66 20 28 5f 5f 2e 6f 6e 55 6e 6d 6f  .  if (__.onUnmo
0a20: 75 6e 74 29 20 5f 5f 2e 6f 6e 55 6e 6d 6f 75 6e  unt) __.onUnmoun
0a30: 74 28 29 0a 0a 20 20 2f 2f 20 77 65 69 72 64 20  t()..  // weird 
0a40: 66 69 78 20 66 6f 72 20 61 20 77 65 69 72 64 20  fix for a weird 
0a50: 65 64 67 65 20 63 61 73 65 20 23 32 34 30 39 20  edge case #2409 
0a60: 61 6e 64 20 23 32 34 33 36 0a 20 20 2f 2f 20 73  and #2436.  // s
0a70: 6f 6d 65 20 75 73 65 72 73 20 6d 69 67 68 74 20  ome users might 
0a80: 75 73 65 20 79 6f 75 72 20 73 6f 66 74 77 61 72  use your softwar
0a90: 65 20 6e 6f 74 20 61 73 20 79 6f 75 27 76 65 20  e not as you've 
0aa0: 65 78 70 65 63 74 65 64 0a 20 20 2f 2f 20 73 6f  expected.  // so
0ab0: 20 49 20 6e 65 65 64 20 74 6f 20 61 64 64 20 74   I need to add t
0ac0: 68 65 73 65 20 64 69 72 74 79 20 68 61 63 6b 73  hese dirty hacks
0ad0: 20 74 6f 20 6d 69 74 69 67 61 74 65 20 75 6e 65   to mitigate une
0ae0: 78 70 65 63 74 65 64 20 69 73 73 75 65 73 0a 20  xpected issues. 
0af0: 20 69 66 20 28 21 74 61 67 2e 69 73 4d 6f 75 6e   if (!tag.isMoun
0b00: 74 65 64 29 20 73 65 74 4d 6f 75 6e 74 53 74 61  ted) setMountSta
0b10: 74 65 2e 63 61 6c 6c 28 74 61 67 2c 20 74 72 75  te.call(tag, tru
0b20: 65 29 0a 0a 20 20 73 65 74 4d 6f 75 6e 74 53 74  e)..  setMountSt
0b30: 61 74 65 2e 63 61 6c 6c 28 74 61 67 2c 20 66 61  ate.call(tag, fa
0b40: 6c 73 65 29 0a 0a 20 20 64 65 6c 65 74 65 20 72  lse)..  delete r
0b50: 6f 6f 74 2e 5f 74 61 67 0a 0a 20 20 72 65 74 75  oot._tag..  retu
0b60: 72 6e 20 74 61 67 0a 7d                          rn tag.}