Artifact
30e539615e84c9afb96cac1abd57548e6fde9ba4dda271bb94e747fc759a2869:
0000: 69 6d 70 6f 72 74 20 7b 20 74 6d 70 6c 20 7d 20 import { tmpl }
0010: 66 72 6f 6d 20 27 72 69 6f 74 2d 74 6d 70 6c 27 from 'riot-tmpl'
0020: 0a 69 6d 70 6f 72 74 20 69 73 42 6c 61 6e 6b 20 .import isBlank
0030: 66 72 6f 6d 20 27 2e 2f 2e 2e 2f 63 6f 6d 6d 6f from './../commo
0040: 6e 2f 75 74 69 6c 2f 63 68 65 63 6b 73 2f 69 73 n/util/checks/is
0050: 2d 62 6c 61 6e 6b 27 0a 69 6d 70 6f 72 74 20 69 -blank'.import i
0060: 73 53 74 72 69 6e 67 20 66 72 6f 6d 20 27 2e 2f sString from './
0070: 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69 6c 2f 63 ../common/util/c
0080: 68 65 63 6b 73 2f 69 73 2d 73 74 72 69 6e 67 27 hecks/is-string'
0090: 0a 69 6d 70 6f 72 74 20 72 65 6d 6f 76 65 41 74 .import removeAt
00a0: 74 72 69 62 75 74 65 20 66 72 6f 6d 20 27 2e 2f tribute from './
00b0: 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69 6c 2f 64 ../common/util/d
00c0: 6f 6d 2f 72 65 6d 6f 76 65 2d 61 74 74 72 69 62 om/remove-attrib
00d0: 75 74 65 27 0a 69 6d 70 6f 72 74 20 73 65 74 41 ute'.import setA
00e0: 74 74 72 69 62 75 74 65 20 66 72 6f 6d 20 27 2e ttribute from '.
00f0: 2f 2e 2e 2f 63 6f 6d 6d 6f 6e 2f 75 74 69 6c 2f /../common/util/
0100: 64 6f 6d 2f 73 65 74 2d 61 74 74 72 69 62 75 74 dom/set-attribut
0110: 65 27 0a 0a 69 6d 70 6f 72 74 20 7b 0a 20 20 67 e'..import {. g
0120: 65 74 49 6d 6d 65 64 69 61 74 65 43 75 73 74 6f etImmediateCusto
0130: 6d 50 61 72 65 6e 74 54 61 67 2c 0a 20 20 61 72 mParentTag,. ar
0140: 72 61 79 69 73 68 41 64 64 2c 0a 20 20 61 72 72 rayishAdd,. arr
0150: 61 79 69 73 68 52 65 6d 6f 76 65 0a 7d 20 66 72 ayishRemove.} fr
0160: 6f 6d 20 27 2e 2f 2e 2e 2f 63 6f 6d 6d 6f 6e 2f om './../common/
0170: 75 74 69 6c 2f 74 61 67 73 27 0a 0a 65 78 70 6f util/tags'..expo
0180: 72 74 20 64 65 66 61 75 6c 74 20 7b 0a 20 20 69 rt default {. i
0190: 6e 69 74 28 64 6f 6d 2c 20 70 61 72 65 6e 74 2c nit(dom, parent,
01a0: 20 61 74 74 72 4e 61 6d 65 2c 20 61 74 74 72 56 attrName, attrV
01b0: 61 6c 75 65 29 20 7b 0a 20 20 20 20 74 68 69 73 alue) {. this
01c0: 2e 64 6f 6d 20 3d 20 64 6f 6d 0a 20 20 20 20 74 .dom = dom. t
01d0: 68 69 73 2e 61 74 74 72 20 3d 20 61 74 74 72 4e his.attr = attrN
01e0: 61 6d 65 0a 20 20 20 20 74 68 69 73 2e 72 61 77 ame. this.raw
01f0: 56 61 6c 75 65 20 3d 20 61 74 74 72 56 61 6c 75 Value = attrValu
0200: 65 0a 20 20 20 20 74 68 69 73 2e 70 61 72 65 6e e. this.paren
0210: 74 20 3d 20 70 61 72 65 6e 74 0a 20 20 20 20 74 t = parent. t
0220: 68 69 73 2e 68 61 73 45 78 70 20 3d 20 74 6d 70 his.hasExp = tmp
0230: 6c 2e 68 61 73 45 78 70 72 28 61 74 74 72 56 61 l.hasExpr(attrVa
0240: 6c 75 65 29 0a 20 20 20 20 72 65 74 75 72 6e 20 lue). return
0250: 74 68 69 73 0a 20 20 7d 2c 0a 20 20 75 70 64 61 this. },. upda
0260: 74 65 28 29 20 7b 0a 20 20 20 20 63 6f 6e 73 74 te() {. const
0270: 20 6f 6c 64 20 3d 20 74 68 69 73 2e 76 61 6c 75 old = this.valu
0280: 65 0a 20 20 20 20 63 6f 6e 73 74 20 63 75 73 74 e. const cust
0290: 6f 6d 50 61 72 65 6e 74 20 3d 20 74 68 69 73 2e omParent = this.
02a0: 70 61 72 65 6e 74 20 26 26 20 67 65 74 49 6d 6d parent && getImm
02b0: 65 64 69 61 74 65 43 75 73 74 6f 6d 50 61 72 65 ediateCustomPare
02c0: 6e 74 54 61 67 28 74 68 69 73 2e 70 61 72 65 6e ntTag(this.paren
02d0: 74 29 0a 20 20 20 20 2f 2f 20 69 66 20 74 68 65 t). // if the
02e0: 20 72 65 66 65 72 65 6e 63 65 64 20 65 6c 65 6d referenced elem
02f0: 65 6e 74 20 69 73 20 61 20 63 75 73 74 6f 6d 20 ent is a custom
0300: 74 61 67 2c 20 74 68 65 6e 20 77 65 20 73 65 74 tag, then we set
0310: 20 74 68 65 20 74 61 67 20 69 74 73 65 6c 66 2c the tag itself,
0320: 20 72 61 74 68 65 72 20 74 68 61 6e 20 44 4f 4d rather than DOM
0330: 0a 20 20 20 20 63 6f 6e 73 74 20 74 61 67 4f 72 . const tagOr
0340: 44 6f 6d 20 3d 20 74 68 69 73 2e 64 6f 6d 2e 5f Dom = this.dom._
0350: 5f 72 65 66 20 7c 7c 20 74 68 69 73 2e 74 61 67 _ref || this.tag
0360: 20 7c 7c 20 74 68 69 73 2e 64 6f 6d 0a 0a 20 20 || this.dom..
0370: 20 20 74 68 69 73 2e 76 61 6c 75 65 20 3d 20 74 this.value = t
0380: 68 69 73 2e 68 61 73 45 78 70 20 3f 20 74 6d 70 his.hasExp ? tmp
0390: 6c 28 74 68 69 73 2e 72 61 77 56 61 6c 75 65 2c l(this.rawValue,
03a0: 20 74 68 69 73 2e 70 61 72 65 6e 74 29 20 3a 20 this.parent) :
03b0: 74 68 69 73 2e 72 61 77 56 61 6c 75 65 0a 0a 20 this.rawValue..
03c0: 20 20 20 2f 2f 20 74 68 65 20 6e 61 6d 65 20 63 // the name c
03d0: 68 61 6e 67 65 64 2c 20 73 6f 20 77 65 20 6e 65 hanged, so we ne
03e0: 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 69 74 20 ed to remove it
03f0: 66 72 6f 6d 20 74 68 65 20 6f 6c 64 20 6b 65 79 from the old key
0400: 20 28 69 66 20 70 72 65 73 65 6e 74 29 0a 20 20 (if present).
0410: 20 20 69 66 20 28 21 69 73 42 6c 61 6e 6b 28 6f if (!isBlank(o
0420: 6c 64 29 20 26 26 20 63 75 73 74 6f 6d 50 61 72 ld) && customPar
0430: 65 6e 74 29 20 61 72 72 61 79 69 73 68 52 65 6d ent) arrayishRem
0440: 6f 76 65 28 63 75 73 74 6f 6d 50 61 72 65 6e 74 ove(customParent
0450: 2e 72 65 66 73 2c 20 6f 6c 64 2c 20 74 61 67 4f .refs, old, tagO
0460: 72 44 6f 6d 29 0a 20 20 20 20 69 66 20 28 21 69 rDom). if (!i
0470: 73 42 6c 61 6e 6b 28 74 68 69 73 2e 76 61 6c 75 sBlank(this.valu
0480: 65 29 20 26 26 20 69 73 53 74 72 69 6e 67 28 74 e) && isString(t
0490: 68 69 73 2e 76 61 6c 75 65 29 29 20 7b 0a 20 20 his.value)) {.
04a0: 20 20 20 20 2f 2f 20 61 64 64 20 69 74 20 74 6f // add it to
04b0: 20 74 68 65 20 72 65 66 73 20 6f 66 20 70 61 72 the refs of par
04c0: 65 6e 74 20 74 61 67 20 28 74 68 69 73 20 62 65 ent tag (this be
04d0: 68 61 76 69 6f 72 20 77 61 73 20 63 68 61 6e 67 havior was chang
04e0: 65 64 20 3e 3d 33 2e 30 29 0a 20 20 20 20 20 20 ed >=3.0).
04f0: 69 66 20 28 63 75 73 74 6f 6d 50 61 72 65 6e 74 if (customParent
0500: 29 20 61 72 72 61 79 69 73 68 41 64 64 28 0a 20 ) arrayishAdd(.
0510: 20 20 20 20 20 20 20 63 75 73 74 6f 6d 50 61 72 customPar
0520: 65 6e 74 2e 72 65 66 73 2c 0a 20 20 20 20 20 20 ent.refs,.
0530: 20 20 74 68 69 73 2e 76 61 6c 75 65 2c 0a 20 20 this.value,.
0540: 20 20 20 20 20 20 74 61 67 4f 72 44 6f 6d 2c 0a tagOrDom,.
0550: 20 20 20 20 20 20 20 20 2f 2f 20 75 73 65 20 61 // use a
0560: 6e 20 61 72 72 61 79 20 69 66 20 69 74 27 73 20 n array if it's
0570: 61 20 6c 6f 6f 70 65 64 20 6e 6f 64 65 20 61 6e a looped node an
0580: 64 20 74 68 65 20 72 65 66 20 69 73 20 6e 6f 74 d the ref is not
0590: 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 an expression.
05a0: 20 20 20 20 20 20 20 6e 75 6c 6c 2c 0a 20 20 20 null,.
05b0: 20 20 20 20 20 74 68 69 73 2e 70 61 72 65 6e 74 this.parent
05c0: 2e 5f 5f 2e 69 6e 64 65 78 0a 20 20 20 20 20 20 .__.index.
05d0: 29 0a 0a 20 20 20 20 20 20 69 66 20 28 74 68 69 ).. if (thi
05e0: 73 2e 76 61 6c 75 65 20 21 3d 3d 20 6f 6c 64 29 s.value !== old)
05f0: 20 7b 0a 20 20 20 20 20 20 20 20 73 65 74 41 74 {. setAt
0600: 74 72 69 62 75 74 65 28 74 68 69 73 2e 64 6f 6d tribute(this.dom
0610: 2c 20 74 68 69 73 2e 61 74 74 72 2c 20 74 68 69 , this.attr, thi
0620: 73 2e 76 61 6c 75 65 29 0a 20 20 20 20 20 20 7d s.value). }
0630: 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 . } else {.
0640: 20 20 20 20 72 65 6d 6f 76 65 41 74 74 72 69 62 removeAttrib
0650: 75 74 65 28 74 68 69 73 2e 64 6f 6d 2c 20 74 68 ute(this.dom, th
0660: 69 73 2e 61 74 74 72 29 0a 20 20 20 20 7d 0a 0a is.attr). }..
0670: 20 20 20 20 2f 2f 20 63 61 63 68 65 20 74 68 65 // cache the
0680: 20 72 65 66 20 62 6f 75 6e 64 20 74 6f 20 74 68 ref bound to th
0690: 69 73 20 64 6f 6d 20 6e 6f 64 65 0a 20 20 20 20 is dom node.
06a0: 2f 2f 20 74 6f 20 72 65 75 73 65 20 69 74 20 69 // to reuse it i
06b0: 6e 20 66 75 74 75 72 65 20 28 73 65 65 20 61 6c n future (see al
06c0: 73 6f 20 23 32 33 32 39 29 0a 20 20 20 20 69 66 so #2329). if
06d0: 20 28 21 74 68 69 73 2e 64 6f 6d 2e 5f 5f 72 65 (!this.dom.__re
06e0: 66 29 20 74 68 69 73 2e 64 6f 6d 2e 5f 5f 72 65 f) this.dom.__re
06f0: 66 20 3d 20 74 61 67 4f 72 44 6f 6d 0a 20 20 7d f = tagOrDom. }
0700: 2c 0a 20 20 75 6e 6d 6f 75 6e 74 28 29 20 7b 0a ,. unmount() {.
0710: 20 20 20 20 63 6f 6e 73 74 20 74 61 67 4f 72 44 const tagOrD
0720: 6f 6d 20 3d 20 74 68 69 73 2e 74 61 67 20 7c 7c om = this.tag ||
0730: 20 74 68 69 73 2e 64 6f 6d 0a 20 20 20 20 63 6f this.dom. co
0740: 6e 73 74 20 63 75 73 74 6f 6d 50 61 72 65 6e 74 nst customParent
0750: 20 3d 20 74 68 69 73 2e 70 61 72 65 6e 74 20 26 = this.parent &
0760: 26 20 67 65 74 49 6d 6d 65 64 69 61 74 65 43 75 & getImmediateCu
0770: 73 74 6f 6d 50 61 72 65 6e 74 54 61 67 28 74 68 stomParentTag(th
0780: 69 73 2e 70 61 72 65 6e 74 29 0a 20 20 20 20 69 is.parent). i
0790: 66 20 28 21 69 73 42 6c 61 6e 6b 28 74 68 69 73 f (!isBlank(this
07a0: 2e 76 61 6c 75 65 29 20 26 26 20 63 75 73 74 6f .value) && custo
07b0: 6d 50 61 72 65 6e 74 29 0a 20 20 20 20 20 20 61 mParent). a
07c0: 72 72 61 79 69 73 68 52 65 6d 6f 76 65 28 63 75 rrayishRemove(cu
07d0: 73 74 6f 6d 50 61 72 65 6e 74 2e 72 65 66 73 2c stomParent.refs,
07e0: 20 74 68 69 73 2e 76 61 6c 75 65 2c 20 74 61 67 this.value, tag
07f0: 4f 72 44 6f 6d 29 0a 20 20 7d 0a 7d OrDom). }.}