@hybridly/vue 0.4.1 → 0.4.3

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/index.cjs CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  const vue = require('vue');
6
4
  const core = require('@hybridly/core');
7
5
  const utils = require('@hybridly/utils');
@@ -12,10 +10,10 @@ const dotDiver = require('@clickbar/dot-diver');
12
10
  const isEqual = require('lodash.isequal');
13
11
  const hybridly = require('hybridly');
14
12
 
15
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
13
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
16
14
 
17
- const qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
18
- const isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
15
+ const qs__default = /*#__PURE__*/_interopDefaultCompat(qs);
16
+ const isEqual__default = /*#__PURE__*/_interopDefaultCompat(isEqual);
19
17
 
20
18
  const DEBUG_KEY = "vue:state:dialog";
21
19
  const dialogStore = {
@@ -226,7 +224,9 @@ function setupDevtools(app) {
226
224
  });
227
225
  const listen = [
228
226
  "start",
227
+ "ready",
229
228
  "data",
229
+ "navigating",
230
230
  "navigated",
231
231
  "progress",
232
232
  "error",
@@ -235,7 +235,9 @@ function setupDevtools(app) {
235
235
  "invalid",
236
236
  "exception",
237
237
  "fail",
238
- "after"
238
+ "after",
239
+ "backForward",
240
+ "success"
239
241
  ];
240
242
  core.registerHook("before", (options) => {
241
243
  const groupId = (Math.random() + 1).toString(36).substring(7);
@@ -412,28 +414,36 @@ Please specify a more appropriate element using the "as" attribute. For example:
412
414
 
413
415
  <RouterLink href="${url}" method="${method}" as="button">...</RouterLink>`);
414
416
  }
417
+ function performPreload(type) {
418
+ if (!preloads) {
419
+ return;
420
+ }
421
+ if (props.external) {
422
+ return;
423
+ }
424
+ if (method !== "GET") {
425
+ return;
426
+ }
427
+ if (type !== "mount" && props.disabled) {
428
+ return;
429
+ }
430
+ if (type === "hover" && preloads === "mount") {
431
+ return;
432
+ }
433
+ if (type === "mount" && preloads !== "mount") {
434
+ return;
435
+ }
436
+ core.router.preload(url, {
437
+ data,
438
+ ...props.options
439
+ });
440
+ }
441
+ performPreload("mount");
415
442
  return vue.h(props.as, {
416
443
  ...attrs,
417
444
  ...as === "a" ? { href: url } : {},
418
445
  ...props.disabled ? { disabled: props.disabled } : {},
419
- onMouseenter: () => {
420
- if (!preloads) {
421
- return;
422
- }
423
- if (props.external) {
424
- return;
425
- }
426
- if (props.disabled) {
427
- return;
428
- }
429
- if (method !== "GET") {
430
- return;
431
- }
432
- core.router.preload(url, {
433
- data,
434
- ...props.options
435
- });
436
- },
446
+ onMouseenter: () => performPreload("hover"),
437
447
  onClick: (event) => {
438
448
  if (props.external) {
439
449
  return;
@@ -492,7 +502,7 @@ Please specify a more appropriate element using the "as" attribute. For example:
492
502
  default: void 0
493
503
  },
494
504
  preload: {
495
- type: Boolean,
505
+ type: [Boolean, String],
496
506
  default: false
497
507
  }
498
508
  }
@@ -543,6 +553,12 @@ function useProperties() {
543
553
  function useProperty(path) {
544
554
  return vue.computed(() => dotDiver.getByPath(state.context.value?.view.properties, path));
545
555
  }
556
+ function setProperty(path, value) {
557
+ if (!state.context.value?.view.properties) {
558
+ return;
559
+ }
560
+ dotDiver.setByPath(state.context.value.view.properties, path, vue.toValue(value));
561
+ }
546
562
 
547
563
  function useContext() {
548
564
  return vue.computed(() => state.context.value);
@@ -692,6 +708,8 @@ function useForm(options) {
692
708
  clearError,
693
709
  setInitial,
694
710
  hasDirty,
711
+ submitWith: submit,
712
+ /** @deprecated Use `submitWith` instead */
695
713
  submitWithOptions: submit,
696
714
  submit: () => submit(),
697
715
  hasErrors: vue.computed(() => Object.values(errors.value ?? {}).length > 0),
@@ -783,10 +801,15 @@ const registerHook = (hook, fn, options) => {
783
801
 
784
802
  function useDialog() {
785
803
  return {
804
+ /** Closes the dialog. */
786
805
  close: () => hybridly.router.dialog.close(),
806
+ /** Closes the dialog without a server round-trip. */
787
807
  closeLocally: () => hybridly.router.dialog.close({ local: true }),
808
+ /** Unmounts the dialog. Should be called after its closing animations. */
788
809
  unmount: () => dialogStore.removeComponent(),
810
+ /** Whether the dialog is shown. */
789
811
  show: vue.computed(() => dialogStore.state.show.value),
812
+ /** Properties of the dialog. */
790
813
  properties: vue.computed(() => state.context.value?.dialog?.properties)
791
814
  };
792
815
  }
@@ -795,6 +818,16 @@ function useRefinements(properties, refinementsKeys, defaultOptions = {}) {
795
818
  const refinements = vue.computed(() => properties[refinementsKeys]);
796
819
  const sortsKey = vue.computed(() => refinements.value.keys.sorts);
797
820
  const filtersKey = vue.computed(() => refinements.value.keys.filters);
821
+ defaultOptions = {
822
+ replace: false,
823
+ ...defaultOptions
824
+ };
825
+ function getSort(name) {
826
+ return refinements.value.sorts.find((sort) => sort.name === name);
827
+ }
828
+ function getFilter(name) {
829
+ return refinements.value.filters.find((sort) => sort.name === name);
830
+ }
798
831
  async function reset(options = {}) {
799
832
  return await core.router.reload({
800
833
  ...defaultOptions,
@@ -825,16 +858,21 @@ function useRefinements(properties, refinementsKeys, defaultOptions = {}) {
825
858
  }
826
859
  });
827
860
  }
828
- async function applyFilter(filter, value, options = {}) {
829
- if (!refinements.value.filters.find(({ name }) => name === filter)) {
861
+ async function applyFilter(name, value, options = {}) {
862
+ const filter = getFilter(name);
863
+ if (!filter) {
864
+ console.warn(`[Refinement] Filter "${name} does not exist."`);
830
865
  return;
831
866
  }
867
+ if (["", null].includes(value) || value === filter.default) {
868
+ value = void 0;
869
+ }
832
870
  return await core.router.reload({
833
871
  ...defaultOptions,
834
872
  ...options,
835
873
  data: {
836
874
  [filtersKey.value]: {
837
- [filter]: value === "" ? void 0 : value
875
+ [name]: value
838
876
  }
839
877
  }
840
878
  });
@@ -854,16 +892,22 @@ function useRefinements(properties, refinementsKeys, defaultOptions = {}) {
854
892
  function currentFilters() {
855
893
  return refinements.value.filters.filter(({ is_active }) => is_active);
856
894
  }
857
- function isSorting() {
895
+ function isSorting(name) {
896
+ if (name) {
897
+ return currentSorts().some((sort) => sort.name === name);
898
+ }
858
899
  return currentSorts().length !== 0;
859
900
  }
860
- function isFiltering() {
901
+ function isFiltering(name) {
902
+ if (name) {
903
+ return currentFilters().some((filter) => filter.name === name);
904
+ }
861
905
  return currentFilters().length !== 0;
862
906
  }
863
- async function toggleSort(sortName, options) {
864
- const sort = refinements.value.sorts.find(({ name }) => name === sortName);
907
+ async function toggleSort(name, options) {
908
+ const sort = getSort(name);
865
909
  if (!sort) {
866
- console.warn(`[Refinement] Sort "${sortName} does not exist."`);
910
+ console.warn(`[Refinement] Sort "${name} does not exist."`);
867
911
  return;
868
912
  }
869
913
  const next = options?.direction ? sort[options?.direction] : sort.next;
@@ -875,22 +919,94 @@ function useRefinements(properties, refinementsKeys, defaultOptions = {}) {
875
919
  }
876
920
  });
877
921
  }
922
+ function bindFilter(name, options = {}) {
923
+ const transform = options?.transformValue ?? ((value) => value);
924
+ const watchFn = options?.watch ?? vue.watch;
925
+ const _ref = vue.ref(transform(refinements.value.filters.find((f) => f.name === name)?.value));
926
+ vue.watch(() => refinements.value.filters.find((f) => f.name === name), (filter) => {
927
+ _ref.value = transform(filter?.value);
928
+ }, { deep: true });
929
+ watchFn(_ref, (value) => applyFilter(name, transform(value), options));
930
+ return _ref;
931
+ }
878
932
  return {
933
+ /**
934
+ * Binds a named filter to a ref, applying filters when it changes and updating the ref accordingly.
935
+ */
936
+ bindFilter,
937
+ /**
938
+ * Available filters.
939
+ */
879
940
  filters: toReactive(refinements.value.filters),
941
+ /**
942
+ * Available sorts.
943
+ */
880
944
  sorts: toReactive(refinements.value.sorts),
945
+ /**
946
+ * Gets a filter by name.
947
+ */
948
+ getFilter,
949
+ /**
950
+ * Gets a sort by name.
951
+ */
952
+ getSort,
953
+ /**
954
+ * Resets all filters and sorts.
955
+ */
881
956
  reset,
957
+ /**
958
+ * Toggles the specified sort.
959
+ */
882
960
  toggleSort,
961
+ /**
962
+ * Whether a sort is active.
963
+ */
883
964
  isSorting,
965
+ /**
966
+ * Whether a filter is active.
967
+ */
884
968
  isFiltering,
969
+ /**
970
+ * The current sorts.
971
+ */
885
972
  currentSorts,
973
+ /**
974
+ * The current filters.
975
+ */
886
976
  currentFilters,
977
+ /**
978
+ * Clears the given filter.
979
+ */
887
980
  clearFilter,
981
+ /**
982
+ * Resets all sorts.
983
+ */
888
984
  clearSorts,
985
+ /**
986
+ * Resets all filters.
987
+ */
889
988
  clearFilters,
989
+ /**
990
+ * Applies the given filter.
991
+ */
890
992
  applyFilter
891
993
  };
892
994
  }
893
995
 
996
+ function useRoute() {
997
+ const current = vue.ref(hybridly.router.current());
998
+ function matches(name, parameters) {
999
+ return hybridly.router.matches(vue.toValue(name), parameters);
1000
+ }
1001
+ registerHook("navigated", () => {
1002
+ current.value = hybridly.router.current();
1003
+ });
1004
+ return {
1005
+ current: vue.readonly(current),
1006
+ matches
1007
+ };
1008
+ }
1009
+
894
1010
  exports.can = core.can;
895
1011
  exports.route = core.route;
896
1012
  exports.router = core.router;
@@ -899,6 +1015,7 @@ exports.defineLayout = defineLayout;
899
1015
  exports.defineLayoutProperties = defineLayoutProperties;
900
1016
  exports.initializeHybridly = initializeHybridly;
901
1017
  exports.registerHook = registerHook;
1018
+ exports.setProperty = setProperty;
902
1019
  exports.useBackForward = useBackForward;
903
1020
  exports.useContext = useContext;
904
1021
  exports.useDialog = useDialog;
@@ -908,3 +1025,4 @@ exports.usePaginator = usePaginator;
908
1025
  exports.useProperties = useProperties;
909
1026
  exports.useProperty = useProperty;
910
1027
  exports.useRefinements = useRefinements;
1028
+ exports.useRoute = useRoute;