UNPKG

lisn.js

Version:

Simply handle user gestures and actions. Includes widgets.

1 lines 11.4 kB
{"version":3,"file":"views.cjs","names":["MC","_interopRequireWildcard","require","MH","_validation","_bitSpaces","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","isValidScrollOffset","offset","match","OFFSET_REGEX","exports","isValidView","view","includes","VIEWS","isValidViewList","views","isValidStrList","getOppositeViews","usageError","bitmask","getViewsBitmask","oppositeBitmask","VIEWS_SPACE","bit","at","above","below","left","right","getViewsFromBitmask","viewsStr","viewsBitmask","validateStrList","v","parseScrollOffset","input","reference","value","bugError","S_AT","S_ABOVE","S_BELOW","S_LEFT","S_RIGHT","createBitSpace","newBitSpaces","RegExp","start","end","name","nameOf","push"],"sources":["../../../src/ts/utils/views.ts"],"sourcesContent":["/**\n * @module Utils\n */\n\nimport * as MC from \"@lisn/globals/minification-constants\";\nimport * as MH from \"@lisn/globals/minification-helpers\";\n\nimport { ScrollOffset, View, CommaSeparatedStr } from \"@lisn/globals/types\";\n\nimport { isValidStrList, validateStrList } from \"@lisn/utils/validation\";\n\nimport { newBitSpaces, createBitSpace } from \"@lisn/modules/bit-spaces\";\n\n/**\n * Returns true if the given string is a valid {@link ScrollOffset}.\n *\n * @category Validation\n */\nexport const isValidScrollOffset = (offset: string): offset is ScrollOffset =>\n offset.match(OFFSET_REGEX) !== null;\n\n/**\n * Returns true if the given string is a valid \"view\".\n *\n * @category Validation\n */\nexport const isValidView = (view: string): view is View =>\n MH.includes(VIEWS, view);\n\n/**\n * Returns true if the given string or array is a list of valid views.\n *\n * @category Validation\n */\nexport const isValidViewList = (views: string | string[]) =>\n isValidStrList(views, isValidView, false);\n\n/**\n * Returns the views that are opposite to the given set of views.\n *\n * Above and below are opposites, and so are left and right.\n *\n * \"at\" is a special case. It is considered opposite to any view in the sense\n * that if it is not present in `views` it will always be included in the\n * returned array. However it is not \"strongly\" opposite in the sense that it\n * will not cause other views to be included in the result unless it is the\n * only view in `views`. That is, there are two sets of strongly opposite pairs\n * (\"above\"/\"below\" and \"left\"/\"right\") and at least one of the two opposing\n * views of a pair must be present for the other one to be included, _except in\n * the special case of `views` being \"at\"_. See examples below for\n * clarification.\n *\n * **Note** that the order of the returned array is not defined.\n *\n * @example\n * Returns [\"above\", \"below\", \"left\", \"right\"] (not definite order), since\n * \"at\" is the only view present and is opposite to all:\n *\n * ```javascript\n * getOppositeViews(\"at\"); // -> [\"above\", \"below\", \"left\", \"right\"] (not necessarily in this order)\n * ```\n *\n * @example\n * Returns [\"below\"]. \"left\" and \"right\" are NOT included even though \"at\" is\n * given, because at least one of the two opposing views of a pair must be\n * present for the other one to be included (except in the special case of\n * `views` being \"at\").\n *\n * ```javascript\n * getOppositeViews(\"at,above\"); // -> [\"below\"]\n * ```\n *\n * @example\n * ```javascript\n * getOppositeViews(\"above\"); // -> [\"at\", \"below\"] (not necessarily in this order)\n * ```\n *\n * @example\n * ```javascript\n * getOppositeViews(\"above,below\"); // -> [\"at\"]\n * ```\n *\n * @example\n * ```javascript\n * getOppositeViews(\"at,above,below\"); // -> []\n * ```\n *\n * @example\n * ```javascript\n * getOppositeViews(\"above,right\"); // -> [\"at\", \"below\", \"left\"] (not necessarily in this order)\n * ```\n *\n * @example\n * ```javascript\n * getOppositeViews(\"at,above,right\"); // -> [\"below\", \"left\"] (not necessarily in this order)\n * ```\n *\n * @throws {@link Errors.LisnUsageError | LisnUsageError}\n * If the given view is not valid, including if it's empty \"\".\n *\n * @category Views\n */\nexport const getOppositeViews = (\n views: CommaSeparatedStr<View> | View[],\n): View[] => {\n if (!views) {\n throw MH.usageError(\"'views' cannot be empty\");\n }\n\n const bitmask = getViewsBitmask(views);\n let oppositeBitmask = VIEWS_SPACE.bitmask & ~bitmask; // initial, all not present in bitmask\n\n // If the given view is \"at\", then include all the other ones.\n // Otherwise include only the opposite views of those directional\n // (above/below/left/right) that are present. I.e. if neither left not right\n // is given, then don't include them\n if (bitmask !== VIEWS_SPACE.bit.at) {\n // remove the opposite ones to those not present\n if (!(bitmask & VIEWS_SPACE.bit.above)) {\n oppositeBitmask &= ~VIEWS_SPACE.bit.below;\n }\n\n if (!(bitmask & VIEWS_SPACE.bit.below)) {\n oppositeBitmask &= ~VIEWS_SPACE.bit.above;\n }\n\n if (!(bitmask & VIEWS_SPACE.bit.left)) {\n oppositeBitmask &= ~VIEWS_SPACE.bit.right;\n }\n\n if (!(bitmask & VIEWS_SPACE.bit.right)) {\n oppositeBitmask &= ~VIEWS_SPACE.bit.left;\n }\n }\n\n return getViewsFromBitmask(oppositeBitmask);\n};\n\n/**\n * @ignore\n * @internal\n */\nexport const getViewsBitmask = (\n viewsStr: View[] | string | undefined,\n): number => {\n let viewsBitmask = 0;\n const views = validateStrList(\"views\", viewsStr, isValidView);\n\n if (views) {\n for (const v of views) {\n if (!isValidView(v)) {\n throw MH.usageError(`Unknown view '${v}'`);\n }\n\n viewsBitmask |= VIEWS_SPACE.bit[v];\n }\n } else {\n viewsBitmask = VIEWS_SPACE.bitmask; // default: all\n }\n\n return viewsBitmask;\n};\n\n/**\n * @ignore\n * @internal\n */\nexport const parseScrollOffset = (input: string) => {\n const match = input.match(OFFSET_REGEX);\n if (!match) {\n throw MH.usageError(`Invalid offset: '${input}'`);\n }\n\n const reference = match[1];\n const value = match[2];\n /* istanbul ignore next */ // shouldn't happen\n if (!reference || !value) {\n throw MH.bugError(\"Offset regex: blank capture groups\");\n }\n\n return { reference, value };\n};\n\nconst VIEWS: View[] = [\n MC.S_AT,\n MC.S_ABOVE,\n MC.S_BELOW,\n MC.S_LEFT,\n MC.S_RIGHT,\n] as const;\n\n/**\n * @ignore\n * @internal\n */\nexport const VIEWS_SPACE = createBitSpace<View>(newBitSpaces(), ...VIEWS);\n\n// --------------------\n\n// Don't use capture groups for old browser support\nconst OFFSET_REGEX = RegExp(\"(top|bottom|left|right): *([^ ].+)\");\n\nconst getViewsFromBitmask = (bitmask: number): View[] => {\n const views: View[] = [];\n for (let bit = VIEWS_SPACE.start; bit <= VIEWS_SPACE.end; bit++) {\n const value = 1 << bit;\n if (bitmask & value) {\n const name = VIEWS_SPACE.nameOf(value);\n if (name) {\n views.push(name);\n }\n }\n }\n\n return views;\n};\n"],"mappings":";;;;;;AAIA,IAAAA,EAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,EAAA,GAAAF,uBAAA,CAAAC,OAAA;AAIA,IAAAE,WAAA,GAAAF,OAAA;AAEA,IAAAG,UAAA,GAAAH,OAAA;AAAwE,SAAAD,wBAAAK,CAAA,EAAAC,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAP,uBAAA,YAAAA,CAAAK,CAAA,EAAAC,CAAA,SAAAA,CAAA,IAAAD,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,MAAAM,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAC,OAAA,EAAAV,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAQ,CAAA,MAAAF,CAAA,GAAAL,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAG,CAAA,CAAAK,GAAA,CAAAX,CAAA,UAAAM,CAAA,CAAAM,GAAA,CAAAZ,CAAA,GAAAM,CAAA,CAAAO,GAAA,CAAAb,CAAA,EAAAQ,CAAA,gBAAAP,CAAA,IAAAD,CAAA,gBAAAC,CAAA,OAAAa,cAAA,CAAAC,IAAA,CAAAf,CAAA,EAAAC,CAAA,OAAAM,CAAA,IAAAD,CAAA,GAAAU,MAAA,CAAAC,cAAA,KAAAD,MAAA,CAAAE,wBAAA,CAAAlB,CAAA,EAAAC,CAAA,OAAAM,CAAA,CAAAK,GAAA,IAAAL,CAAA,CAAAM,GAAA,IAAAP,CAAA,CAAAE,CAAA,EAAAP,CAAA,EAAAM,CAAA,IAAAC,CAAA,CAAAP,CAAA,IAAAD,CAAA,CAAAC,CAA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