@stackoverflow/stacks 3.0.0-beta.27 → 3.0.0-beta.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/js/stacks.js CHANGED
@@ -51,7 +51,6 @@ __webpack_require__.r(__webpack_exports__);
51
51
  __webpack_require__.d(__webpack_exports__, {
52
52
  BannerController: () => (/* reexport */ BannerController),
53
53
  BasePopoverController: () => (/* reexport */ BasePopoverController),
54
- ExpandableController: () => (/* reexport */ ExpandableController),
55
54
  ModalController: () => (/* reexport */ ModalController),
56
55
  PopoverController: () => (/* reexport */ PopoverController),
57
56
  StacksApplication: () => (/* reexport */ StacksApplication),
@@ -60,7 +59,6 @@ __webpack_require__.d(__webpack_exports__, {
60
59
  TableController: () => (/* reexport */ TableController),
61
60
  ToastController: () => (/* reexport */ ToastController),
62
61
  TooltipController: () => (/* reexport */ TooltipController),
63
- UploaderController: () => (/* reexport */ UploaderController),
64
62
  addController: () => (/* reexport */ addController),
65
63
  application: () => (/* reexport */ application),
66
64
  attachPopover: () => (/* reexport */ attachPopover),
@@ -2832,200 +2830,6 @@ function toggleBanner(element, show) {
2832
2830
  show ? controller.show() : controller.hide();
2833
2831
  }
2834
2832
 
2835
- ;// ./lib/components/expandable/expandable.ts
2836
-
2837
- // Radio buttons only trigger a change event when they're *checked*, but not when
2838
- // they're *unchecked*. Therefore, if we have an active `s-expandable-control` in
2839
- // the document, we listen for change events on *all* radio buttons and find any
2840
- // other radio buttons in the same `name` group, triggering a custom event on all
2841
- // of them so the controller can re-evaluate.
2842
- //
2843
- // We're keeping a count of how many of these controllers are connected to the DOM,
2844
- // so only have this global listener when we actually need it.
2845
- const RADIO_OFF_EVENT = "s-expandable-control:radio-off";
2846
- function globalChangeListener(e) {
2847
- const target = e.target;
2848
- if (!(target instanceof HTMLInputElement) ||
2849
- target.nodeName !== "INPUT" ||
2850
- target.type !== "radio") {
2851
- return;
2852
- }
2853
- document
2854
- .querySelectorAll('input[type="radio"][name="' + target.name + '"]')
2855
- .forEach(function (other) {
2856
- if (other === e.target) {
2857
- return;
2858
- }
2859
- let customEvent;
2860
- try {
2861
- customEvent = new Event(RADIO_OFF_EVENT);
2862
- }
2863
- catch (_a) {
2864
- // Internet Explorer
2865
- customEvent = document.createEvent("Event");
2866
- customEvent.initEvent(RADIO_OFF_EVENT, true, true);
2867
- }
2868
- other.dispatchEvent(customEvent);
2869
- });
2870
- }
2871
- let refCount = 0;
2872
- function globalChangeListenerRequired(required) {
2873
- if (required) {
2874
- refCount++;
2875
- if (refCount === 1) {
2876
- document.body.addEventListener("change", globalChangeListener);
2877
- }
2878
- }
2879
- else {
2880
- refCount--;
2881
- if (refCount === 0) {
2882
- document.body.removeEventListener("change", globalChangeListener);
2883
- }
2884
- }
2885
- }
2886
- class ExpandableController extends StacksController {
2887
- constructor() {
2888
- super(...arguments);
2889
- this.lastKeydownClickTimestamp = 0;
2890
- }
2891
- initialize() {
2892
- if (this.element.nodeName === "INPUT" &&
2893
- ["radio", "checkbox"].indexOf(this.element.type) >= 0) {
2894
- this.isCollapsed = this._isCollapsedForCheckable.bind(this);
2895
- this.events = ["change", RADIO_OFF_EVENT];
2896
- this.isCheckable = true;
2897
- this.isRadio = this.element.type === "radio";
2898
- }
2899
- else {
2900
- this.isCollapsed = this._isCollapsedForClickable.bind(this);
2901
- this.events = ["click", "keydown"];
2902
- }
2903
- this.listener = this.listener.bind(this);
2904
- }
2905
- // for non-checkable elements, the initial source of truth is the collapsed/expanded
2906
- // state of the controlled element (unless the element doesn't exist)
2907
- _isCollapsedForClickable() {
2908
- const cc = this.controlledExpandables;
2909
- // the element is considered collapsed if *any* target element is collapsed
2910
- return cc.length > 0
2911
- ? !cc.every((element) => element.classList.contains("is-expanded"))
2912
- : this.element.getAttribute("aria-expanded") === "false";
2913
- }
2914
- // for checkable elements, the initial source of truth is the checked state
2915
- _isCollapsedForCheckable() {
2916
- return !this.element.checked;
2917
- }
2918
- get controlledExpandables() {
2919
- const attr = this.element.getAttribute("aria-controls");
2920
- if (!attr) {
2921
- throw `[aria-controls="targetId1 ... targetIdN"] attribute required`;
2922
- }
2923
- const result = attr
2924
- .split(/\s+/g)
2925
- .map((s) => document.getElementById(s))
2926
- .filter((e) => !!e);
2927
- if (!result.length) {
2928
- throw "couldn't find controls";
2929
- }
2930
- return result;
2931
- }
2932
- _dispatchShowHideEvent(isShow) {
2933
- this.triggerEvent(isShow ? "show" : "hide");
2934
- }
2935
- _toggleClass(doAdd) {
2936
- if (!this.data.has("toggle-class")) {
2937
- return;
2938
- }
2939
- const cl = this.element.classList;
2940
- const toggleClass = this.data.get("toggle-class");
2941
- if (!toggleClass) {
2942
- throw "couldn't find toggle class";
2943
- }
2944
- toggleClass.split(/\s+/).forEach(function (cls) {
2945
- cl.toggle(cls, !!doAdd);
2946
- });
2947
- }
2948
- listener(e) {
2949
- let newCollapsed;
2950
- if (this.isCheckable) {
2951
- newCollapsed = !this.element.checked;
2952
- }
2953
- else {
2954
- if (e.type == "keydown" &&
2955
- e instanceof KeyboardEvent &&
2956
- e.keyCode != 13 &&
2957
- e.keyCode != 32) {
2958
- return;
2959
- }
2960
- if (e.target !== e.currentTarget &&
2961
- ["A", "BUTTON"].indexOf(e.target.nodeName) >= 0) {
2962
- return;
2963
- }
2964
- e.preventDefault();
2965
- // Prevent "click" events from toggling the expandable within 300ms of "keydown".
2966
- // e.preventDefault() should have done the same, but https://bugzilla.mozilla.org/show_bug.cgi?id=1487102
2967
- // doesn't guarantee it.
2968
- if (e.type == "keydown") {
2969
- this.lastKeydownClickTimestamp = Date.now();
2970
- }
2971
- else if (e.type == "click" &&
2972
- Date.now() - this.lastKeydownClickTimestamp < 300) {
2973
- return;
2974
- }
2975
- newCollapsed =
2976
- this.element.getAttribute("aria-expanded") === "true";
2977
- if (e.type === "click") {
2978
- this.element.blur();
2979
- }
2980
- }
2981
- this.element.setAttribute("aria-expanded", newCollapsed ? "false" : "true");
2982
- for (const controlledElement of this.controlledExpandables) {
2983
- controlledElement.classList.toggle("is-expanded", !newCollapsed);
2984
- }
2985
- this._dispatchShowHideEvent(!newCollapsed);
2986
- this._toggleClass(!newCollapsed);
2987
- }
2988
- connect() {
2989
- this.events.forEach((e) => {
2990
- this.element.addEventListener(e, this.listener.bind(this));
2991
- }, this);
2992
- if (this.isRadio) {
2993
- globalChangeListenerRequired(true);
2994
- }
2995
- // synchronize state -- in all cases, this means setting the correct `aria-expanded`
2996
- // attribute; for checkable controls this also means setting the `is-collapsed` class.
2997
- // Note: aria-expanded is currently an invalid attribute on radio elements
2998
- // Support for aria-expanded is being debated by the W3C https://github.com/w3c/aria/issues/1404 as recently as June 2022
2999
- if (!this.isRadio) {
3000
- this.element.setAttribute("aria-expanded", this.isCollapsed() ? "false" : "true");
3001
- }
3002
- if (this.isCheckable) {
3003
- const cc = this.controlledExpandables;
3004
- if (cc.length) {
3005
- const expected = !this.isCollapsed();
3006
- // if any element does not match the expected state, set them all to the expected state
3007
- if (cc.some((element) => element.classList.contains("is-expanded") !==
3008
- expected)) {
3009
- for (const controlledElement of this
3010
- .controlledExpandables) {
3011
- controlledElement.classList.toggle("is-expanded", expected);
3012
- }
3013
- this._dispatchShowHideEvent(expected);
3014
- this._toggleClass(expected);
3015
- }
3016
- }
3017
- }
3018
- }
3019
- disconnect() {
3020
- this.events.forEach((e) => {
3021
- this.element.removeEventListener(e, this.listener.bind(this));
3022
- }, this);
3023
- if (this.isRadio) {
3024
- globalChangeListenerRequired(false);
3025
- }
3026
- }
3027
- }
3028
-
3029
2833
  ;// ./lib/components/modal/modal.ts
3030
2834
 
3031
2835
  class ModalController extends StacksController {
@@ -6620,170 +6424,6 @@ function applyOptionsAndTitleAttributes(element, options) {
6620
6424
  }
6621
6425
  }
6622
6426
 
6623
- ;// ./lib/components/uploader/uploader.ts
6624
-
6625
- class UploaderController extends StacksController {
6626
- connect() {
6627
- super.connect();
6628
- this.boundDragEnter = this.handleUploaderActive.bind(this, true);
6629
- this.boundDragLeave = this.handleUploaderActive.bind(this, false);
6630
- this.inputTarget.addEventListener("dragenter", this.boundDragEnter);
6631
- this.inputTarget.addEventListener("dragleave", this.boundDragLeave);
6632
- }
6633
- disconnect() {
6634
- this.inputTarget.removeEventListener("dragenter", this.boundDragEnter);
6635
- this.inputTarget.removeEventListener("dragleave", this.boundDragLeave);
6636
- super.disconnect();
6637
- }
6638
- /**
6639
- * Handles rendering the file preview state on input change
6640
- */
6641
- handleInput() {
6642
- this.previewsTarget.innerHTML = "";
6643
- if (!this.inputTarget.files) {
6644
- return;
6645
- }
6646
- const count = this.inputTarget.files.length;
6647
- this.getDataURLs(this.inputTarget.files, UploaderController.FILE_DISPLAY_LIMIT)
6648
- .then((res) => {
6649
- this.handleVisible(true);
6650
- const hasMultipleFiles = res.length > 1;
6651
- if (hasMultipleFiles) {
6652
- const headingElement = document.createElement("div");
6653
- headingElement.classList.add("s-uploader--previews-heading");
6654
- headingElement.innerText =
6655
- res.length < count
6656
- ? `Showing ${res.length} of ${count} files`
6657
- : `${count} items`;
6658
- this.previewsTarget.appendChild(headingElement);
6659
- this.previewsTarget.classList.add("has-multiple");
6660
- }
6661
- else {
6662
- this.previewsTarget.classList.remove("has-multiple");
6663
- }
6664
- res.forEach((file) => this.addFilePreview(file));
6665
- this.handleUploaderActive(true);
6666
- })
6667
- // TODO consider rendering an error message
6668
- .catch(() => null);
6669
- }
6670
- /**
6671
- * Resets the Uploader to initial state
6672
- */
6673
- reset() {
6674
- this.inputTarget.value = "";
6675
- this.previewsTarget.innerHTML = "";
6676
- this.handleVisible(false);
6677
- }
6678
- /**
6679
- * Set hide/show and disabled state on elements depending on preview state
6680
- * @param {boolean} shouldPreview - Uploader is entering a preview state
6681
- */
6682
- handleVisible(shouldPreview) {
6683
- const { scope } = this.targets;
6684
- const hideElements = scope.findAllElements("[data-s-uploader-hide-on-input]");
6685
- const showElements = scope.findAllElements("[data-s-uploader-show-on-input]");
6686
- const enableElements = scope.findAllElements("[data-s-uploader-enable-on-input]");
6687
- if (shouldPreview) {
6688
- hideElements.forEach((el) => {
6689
- el.classList.add("d-none");
6690
- });
6691
- showElements.forEach((el) => {
6692
- el.classList.remove("d-none");
6693
- });
6694
- enableElements.forEach((el) => {
6695
- el.removeAttribute("disabled");
6696
- });
6697
- }
6698
- else {
6699
- hideElements.forEach((el) => {
6700
- el.classList.remove("d-none");
6701
- });
6702
- showElements.forEach((el) => {
6703
- el.classList.add("d-none");
6704
- });
6705
- enableElements.forEach((el) => {
6706
- el.setAttribute("disabled", "true");
6707
- });
6708
- this.handleUploaderActive(false);
6709
- }
6710
- }
6711
- /**
6712
- * Adds a DOM element to preview a selected file
6713
- * @param {FilePreview} file
6714
- */
6715
- addFilePreview(file) {
6716
- if (!file) {
6717
- return;
6718
- }
6719
- const previewElement = document.createElement("div");
6720
- let thumbElement;
6721
- if (file.type.match("image/*") && file.data) {
6722
- thumbElement = document.createElement("img");
6723
- thumbElement.src = file.data.toString();
6724
- thumbElement.alt = file.name;
6725
- }
6726
- else {
6727
- thumbElement = document.createElement("div");
6728
- thumbElement.innerText = file.name;
6729
- }
6730
- thumbElement.classList.add("s-uploader--preview-thumbnail");
6731
- previewElement.appendChild(thumbElement);
6732
- previewElement.classList.add("s-uploader--preview");
6733
- previewElement.setAttribute("data-filename", file.name);
6734
- this.previewsTarget.appendChild(previewElement);
6735
- }
6736
- /**
6737
- * Toggles display and disabled state for select elements on valid input
6738
- * @param {boolean} active - Uploader is in active state (typically on 'dragenter')
6739
- */
6740
- handleUploaderActive(active) {
6741
- this.uploaderTarget.classList.toggle("is-active", active);
6742
- }
6743
- /**
6744
- * Converts the file data into a data URL
6745
- * @param {File} file
6746
- * @returns an object containing a FilePreview object
6747
- */
6748
- fileToDataURL(file) {
6749
- const reader = new FileReader();
6750
- const { name, size, type } = file;
6751
- if (size < UploaderController.MAX_FILE_SIZE &&
6752
- type.indexOf("image") > -1) {
6753
- return new Promise((resolve, reject) => {
6754
- reader.onload = (evt) => {
6755
- var _a;
6756
- const res = (_a = evt === null || evt === void 0 ? void 0 : evt.target) === null || _a === void 0 ? void 0 : _a.result;
6757
- if (res) {
6758
- resolve({ data: res, name, type });
6759
- }
6760
- else {
6761
- reject();
6762
- }
6763
- };
6764
- reader.readAsDataURL(file);
6765
- });
6766
- }
6767
- else {
6768
- return Promise.resolve({ name, type });
6769
- }
6770
- }
6771
- /**
6772
- * Gets an array of FilePreviews from a FileList
6773
- * @param {FileList|[]} files
6774
- * @returns an array of FilePreview objects from a FileList
6775
- */
6776
- getDataURLs(files, limit) {
6777
- const promises = Array.from(files)
6778
- .slice(0, Math.min(limit, files.length))
6779
- .map((f) => this.fileToDataURL(f));
6780
- return Promise.all(promises);
6781
- }
6782
- }
6783
- UploaderController.targets = ["input", "previews", "uploader"];
6784
- UploaderController.FILE_DISPLAY_LIMIT = 10;
6785
- UploaderController.MAX_FILE_SIZE = 1024 * 1024 * 10; // 10 MB
6786
-
6787
6427
  ;// ./lib/controllers.ts
6788
6428
  // export all controllers *with helpers* so they can be bulk re-exported by the package entry point
6789
6429
 
@@ -6794,22 +6434,18 @@ UploaderController.MAX_FILE_SIZE = 1024 * 1024 * 10; // 10 MB
6794
6434
 
6795
6435
 
6796
6436
 
6797
-
6798
-
6799
6437
  ;// ./lib/index.ts
6800
6438
 
6801
6439
 
6802
6440
 
6803
6441
  // register all built-in controllers
6804
6442
  application.register("s-banner", BannerController);
6805
- application.register("s-expandable-control", ExpandableController);
6806
6443
  application.register("s-modal", ModalController);
6807
6444
  application.register("s-toast", ToastController);
6808
6445
  application.register("s-navigation-tablist", TabListController);
6809
6446
  application.register("s-popover", PopoverController);
6810
6447
  application.register("s-table", TableController);
6811
6448
  application.register("s-tooltip", TooltipController);
6812
- application.register("s-uploader", UploaderController);
6813
6449
  // finalize the application to guard our controller namespace
6814
6450
  StacksApplication.finalize();
6815
6451
  // export all controllers w/ helpers