MCPcopy Create free account
hub / github.com/bigskysoftware/_hyperscript / toggle

Method toggle

www/js/_hyperscript.esm.js:8028–8089  ·  view source on GitHub ↗
(context, on, classRef, classRef2, classRefs, betweenValues)

Source from the content-addressed store, hash-verified

8026 return new _ToggleCommand(classRef, classRef2, classRefs, attributeRef, attributeRef2, onExpr, time, evt, from, visibility, betweenClass, betweenAttr, hideShowStrategy, betweenValues, toggleExpr, styleProp);
8027 }
8028 toggle(context, on, classRef, classRef2, classRefs, betweenValues) {
8029 if (this.betweenValues) {
8030 if (this.visibility) {
8031 context.meta.runtime.implicitLoop(on, (target) => {
8032 var current2 = target.style[this.styleProp] || getComputedStyle(target)[this.styleProp];
8033 var idx2 = betweenValues.findIndex((v) => v == current2);
8034 target.style[this.styleProp] = betweenValues[(idx2 + 1) % betweenValues.length];
8035 });
8036 } else {
8037 var current = this.toggleExpr.evaluate(context);
8038 var idx = betweenValues.findIndex((v) => v == current);
8039 var next = betweenValues[(idx + 1) % betweenValues.length];
8040 var lhsValues = {};
8041 for (var key in this.toggleExpr.lhs) {
8042 var val = this.toggleExpr.lhs[key];
8043 lhsValues[key] = val && val.evaluate ? val.evaluate(context) : val;
8044 }
8045 this.toggleExpr.set(context, lhsValues, next);
8046 }
8047 return;
8048 }
8049 context.meta.runtime.nullCheck(on, this.onExpr);
8050 if (this.visibility) {
8051 context.meta.runtime.implicitLoop(on, (target) => {
8052 this.hideShowStrategy("toggle", target, null, context.meta.runtime);
8053 });
8054 } else if (this.betweenClass) {
8055 context.meta.runtime.implicitLoop(on, (target) => {
8056 if (target.classList.contains(classRef.className)) {
8057 target.classList.remove(classRef.className);
8058 target.classList.add(classRef2.className);
8059 } else {
8060 target.classList.add(classRef.className);
8061 target.classList.remove(classRef2.className);
8062 }
8063 });
8064 } else if (this.betweenAttr) {
8065 context.meta.runtime.implicitLoop(on, (target) => {
8066 if (target.hasAttribute(this.attributeRef.name) && target.getAttribute(this.attributeRef.name) === this.attributeRef.value) {
8067 target.removeAttribute(this.attributeRef.name);
8068 target.setAttribute(this.attributeRef2.name, this.attributeRef2.value);
8069 } else {
8070 if (target.hasAttribute(this.attributeRef2.name)) target.removeAttribute(this.attributeRef2.name);
8071 target.setAttribute(this.attributeRef.name, this.attributeRef.value);
8072 }
8073 });
8074 } else if (classRefs) {
8075 context.meta.runtime.forEach(classRefs, (classRef3) => {
8076 context.meta.runtime.implicitLoop(on, (target) => {
8077 target.classList.toggle(classRef3.className);
8078 });
8079 });
8080 } else {
8081 context.meta.runtime.implicitLoop(on, (target) => {
8082 if (target.hasAttribute(this.attributeRef.name)) {
8083 target.removeAttribute(this.attributeRef.name);
8084 } else {
8085 target.setAttribute(this.attributeRef.name, this.attributeRef.value);

Callers 1

resolveMethod · 0.95

Calls 7

getComputedStyleFunction · 0.50
implicitLoopMethod · 0.45
evaluateMethod · 0.45
setMethod · 0.45
nullCheckMethod · 0.45
containsMethod · 0.45
forEachMethod · 0.45

Tested by

no test coverage detected