gitea源码

transition.ts 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import $ from 'jquery';
  2. export function initFomanticTransition() {
  3. const transitionNopBehaviors = new Set([
  4. 'clear queue', 'stop', 'stop all', 'destroy',
  5. 'force repaint', 'repaint', 'reset',
  6. 'looping', 'remove looping', 'disable', 'enable',
  7. 'set duration', 'save conditions', 'restore conditions',
  8. ]);
  9. // stand-in for removed transition module
  10. $.fn.transition = function (arg0: any, arg1: any, arg2: any) {
  11. if (arg0 === 'is supported') return true;
  12. if (arg0 === 'is animating') return false;
  13. if (arg0 === 'is inward') return false;
  14. if (arg0 === 'is outward') return false;
  15. let argObj: Record<string, any>;
  16. if (typeof arg0 === 'string') {
  17. // many behaviors are no-op now. https://fomantic-ui.com/modules/transition.html#/usage
  18. if (transitionNopBehaviors.has(arg0)) return this;
  19. // now, the arg0 is an animation name, the syntax: (animation, duration, complete)
  20. argObj = {animation: arg0, ...(arg1 && {duration: arg1}), ...(arg2 && {onComplete: arg2})};
  21. } else if (typeof arg0 === 'object') {
  22. argObj = arg0;
  23. } else {
  24. throw new Error(`invalid argument: ${arg0}`);
  25. }
  26. const isAnimationIn = argObj.animation?.startsWith('show') || argObj.animation?.endsWith(' in');
  27. const isAnimationOut = argObj.animation?.startsWith('hide') || argObj.animation?.endsWith(' out');
  28. this.each((_, el) => {
  29. let toShow = isAnimationIn;
  30. if (!isAnimationIn && !isAnimationOut) {
  31. // If the animation is not in/out, then it must be a toggle animation.
  32. // Fomantic uses computed styles to check "visibility", but to avoid unnecessary arguments, here it only checks the class.
  33. toShow = this.hasClass('hidden'); // maybe it could also check "!this.hasClass('visible')", leave it to the future until there is a real problem.
  34. }
  35. argObj.onStart?.call(el);
  36. if (toShow) {
  37. el.classList.remove('hidden');
  38. el.classList.add('visible', 'transition');
  39. if (argObj.displayType) el.style.setProperty('display', argObj.displayType, 'important');
  40. argObj.onShow?.call(el);
  41. } else {
  42. el.classList.add('hidden');
  43. el.classList.remove('visible'); // don't remove the transition class because the Fomantic animation style is `.hidden.transition`.
  44. el.style.removeProperty('display');
  45. argObj.onHidden?.call(el);
  46. }
  47. argObj.onComplete?.call(el);
  48. });
  49. return this;
  50. };
  51. }