not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
11 KiB

  1. /**
  2. The enyo.Panels kind is designed to satisfy a variety of common use cases for
  3. application layout. Using enyo.Panels, controls may be arranged as (among other
  4. things) a carousel, a set of collapsing panels, a card stack that fades between
  5. panels, or a grid.
  6. Any Enyo control may be placed inside an enyo.Panels, but by convention we refer
  7. to each of these controls as a "panel." From the set of panels in an enyo.Panels,
  8. one is considered active. The active panel is set by index using the *setIndex*
  9. method. The actual layout of the panels typically changes each time the active
  10. panel is set, such that the new active panel has the most prominent position.
  11. For more information, see the [Panels documentation](https://github.com/enyojs/enyo/wiki/Panels)
  12. in the Enyo Developer Guide.
  13. */
  14. enyo.kind({
  15. name: "enyo.Panels",
  16. classes: "enyo-panels",
  17. published: {
  18. /**
  19. The index of the active panel. The layout of panels is controlled by
  20. the layoutKind, but as a rule, the active panel is displayed in the
  21. most prominent position. For example, in the (default) CardArranger
  22. layout, the active panel is shown and the other panels are hidden.
  23. */
  24. index: 0,
  25. //* Controls whether the user can drag between panels.
  26. draggable: true,
  27. //* Controls whether the panels animate when transitioning; for example,
  28. //* when _setIndex_ is called.
  29. animate: true,
  30. //* Controls whether panels "wrap around" when moving past the end. Actual effect depends upon the arranger in use.
  31. wrap: false,
  32. //* Sets the arranger kind to be used for dynamic layout.
  33. arrangerKind: "CardArranger",
  34. //* By default, each panel will be sized to fit the Panels' width when
  35. //* the screen size is narrow enough (less than ~800px). Set to false
  36. //* to avoid this behavior.
  37. narrowFit: true
  38. },
  39. events: {
  40. /**
  41. Fires at the start of a panel transition.
  42. This event fires when _setIndex_ is called and also during dragging.
  43. */
  44. onTransitionStart: "",
  45. /**
  46. Fires at the end of a panel transition.
  47. This event fires when _setIndex_ is called and also during dragging.
  48. */
  49. onTransitionFinish: ""
  50. },
  51. //* @protected
  52. handlers: {
  53. ondragstart: "dragstart",
  54. ondrag: "drag",
  55. ondragfinish: "dragfinish"
  56. },
  57. tools: [
  58. {kind: "Animator", onStep: "step", onEnd: "completed"}
  59. ],
  60. fraction: 0,
  61. create: function() {
  62. this.transitionPoints = [];
  63. this.inherited(arguments);
  64. this.arrangerKindChanged();
  65. this.avoidFitChanged();
  66. this.indexChanged();
  67. },
  68. initComponents: function() {
  69. this.createChrome(this.tools);
  70. this.inherited(arguments);
  71. },
  72. arrangerKindChanged: function() {
  73. this.setLayoutKind(this.arrangerKind);
  74. },
  75. avoidFitChanged: function() {
  76. this.addRemoveClass("enyo-panels-fit-narrow", this.narrowFit);
  77. },
  78. removeControl: function(inControl) {
  79. this.inherited(arguments);
  80. if (this.controls.length > 1 && this.isPanel(inControl)) {
  81. this.setIndex(Math.max(this.index - 1, 0));
  82. this.flow();
  83. this.reflow();
  84. }
  85. },
  86. isPanel: function() {
  87. // designed to be overridden in kinds derived from Panels that have
  88. // non-panel client controls
  89. return true;
  90. },
  91. flow: function() {
  92. this.arrangements = [];
  93. this.inherited(arguments);
  94. },
  95. reflow: function() {
  96. this.arrangements = [];
  97. this.inherited(arguments);
  98. this.refresh();
  99. },
  100. //* @public
  101. /**
  102. Returns an array of contained panels.
  103. Subclasses can override this if they don't want the arranger to layout all of their children
  104. */
  105. getPanels: function() {
  106. var p = this.controlParent || this;
  107. return p.children;
  108. },
  109. //* Returns a reference to the active panel--i.e., the panel at the specified index.
  110. getActive: function() {
  111. var p$ = this.getPanels();
  112. return p$[this.index];
  113. },
  114. /**
  115. Returns a reference to the <a href="#enyo.Animator">enyo.Animator</a>
  116. instance used to animate panel transitions. The Panels' animator can be used
  117. to set the duration of panel transitions, e.g.:
  118. this.getAnimator().setDuration(1000);
  119. */
  120. getAnimator: function() {
  121. return this.$.animator;
  122. },
  123. /**
  124. Sets the active panel to the panel specified by the given index.
  125. Note that if the _animate_ property is set to true, the active panel
  126. will animate into view.
  127. */
  128. setIndex: function(inIndex) {
  129. // override setIndex so that indexChanged is called
  130. // whether this.index has actually changed or not
  131. this.setPropertyValue("index", inIndex, "indexChanged");
  132. },
  133. /**
  134. Sets the active panel to the panel specified by the given index.
  135. Regardless of the value of the _animate_ property, the transition to the
  136. next panel will not animate and will be immediate.
  137. */
  138. setIndexDirect: function(inIndex) {
  139. this.setIndex(inIndex);
  140. this.completed();
  141. },
  142. //* Transitions to the previous panel--i.e., the panel whose index value is
  143. //* one less than that of the current active panel.
  144. previous: function() {
  145. this.setIndex(this.index-1);
  146. },
  147. //* Transitions to the next panel--i.e., the panel whose index value is one
  148. //* greater than that of the current active panel.
  149. next: function() {
  150. this.setIndex(this.index+1);
  151. },
  152. //* @protected
  153. clamp: function(inValue) {
  154. var l = this.getPanels().length-1;
  155. if (this.wrap) {
  156. // FIXME: dragging makes assumptions about direction and from->start indexes.
  157. //return inValue < 0 ? l : (inValue > l ? 0 : inValue);
  158. return inValue;
  159. } else {
  160. return Math.max(0, Math.min(inValue, l));
  161. }
  162. },
  163. indexChanged: function(inOld) {
  164. this.lastIndex = inOld;
  165. this.index = this.clamp(this.index);
  166. if (!this.dragging) {
  167. if (this.$.animator.isAnimating()) {
  168. this.completed();
  169. }
  170. this.$.animator.stop();
  171. if (this.hasNode()) {
  172. if (this.animate) {
  173. this.startTransition();
  174. this.$.animator.play({
  175. startValue: this.fraction
  176. });
  177. } else {
  178. this.refresh();
  179. }
  180. }
  181. }
  182. },
  183. step: function(inSender) {
  184. this.fraction = inSender.value;
  185. this.stepTransition();
  186. },
  187. completed: function() {
  188. if (this.$.animator.isAnimating()) {
  189. this.$.animator.stop();
  190. }
  191. this.fraction = 1;
  192. this.stepTransition();
  193. this.finishTransition();
  194. },
  195. dragstart: function(inSender, inEvent) {
  196. if (this.draggable && this.layout && this.layout.canDragEvent(inEvent)) {
  197. inEvent.preventDefault();
  198. this.dragstartTransition(inEvent);
  199. this.dragging = true;
  200. this.$.animator.stop();
  201. return true;
  202. }
  203. },
  204. drag: function(inSender, inEvent) {
  205. if (this.dragging) {
  206. inEvent.preventDefault();
  207. this.dragTransition(inEvent);
  208. }
  209. },
  210. dragfinish: function(inSender, inEvent) {
  211. if (this.dragging) {
  212. this.dragging = false;
  213. inEvent.preventTap();
  214. this.dragfinishTransition(inEvent);
  215. }
  216. },
  217. dragstartTransition: function(inEvent) {
  218. if (!this.$.animator.isAnimating()) {
  219. var f = this.fromIndex = this.index;
  220. this.toIndex = f - (this.layout ? this.layout.calcDragDirection(inEvent) : 0);
  221. } else {
  222. this.verifyDragTransition(inEvent);
  223. }
  224. this.fromIndex = this.clamp(this.fromIndex);
  225. this.toIndex = this.clamp(this.toIndex);
  226. //this.log(this.fromIndex, this.toIndex);
  227. this.fireTransitionStart();
  228. if (this.layout) {
  229. this.layout.start();
  230. }
  231. },
  232. dragTransition: function(inEvent) {
  233. // note: for simplicity we choose to calculate the distance directly between
  234. // the first and last transition point.
  235. var d = this.layout ? this.layout.calcDrag(inEvent) : 0;
  236. var t$ = this.transitionPoints, s = t$[0], f = t$[t$.length-1];
  237. var as = this.fetchArrangement(s);
  238. var af = this.fetchArrangement(f);
  239. var dx = this.layout ? this.layout.drag(d, s, as, f, af) : 0;
  240. var dragFail = d && !dx;
  241. if (dragFail) {
  242. //this.log(dx, s, as, f, af);
  243. }
  244. this.fraction += dx;
  245. var fr = this.fraction;
  246. if (fr > 1 || fr < 0 || dragFail) {
  247. if (fr > 0 || dragFail) {
  248. this.dragfinishTransition(inEvent);
  249. }
  250. this.dragstartTransition(inEvent);
  251. this.fraction = 0;
  252. // FIXME: account for lost fraction
  253. //this.dragTransition(inEvent);
  254. }
  255. this.stepTransition();
  256. },
  257. dragfinishTransition: function(inEvent) {
  258. this.verifyDragTransition(inEvent);
  259. this.setIndex(this.toIndex);
  260. // note: if we're still dragging, then we're at a transition boundary
  261. // and should fire the finish event
  262. if (this.dragging) {
  263. this.fireTransitionFinish();
  264. }
  265. },
  266. verifyDragTransition: function(inEvent) {
  267. var d = this.layout ? this.layout.calcDragDirection(inEvent) : 0;
  268. var f = Math.min(this.fromIndex, this.toIndex);
  269. var t = Math.max(this.fromIndex, this.toIndex);
  270. if (d > 0) {
  271. var s = f;
  272. f = t;
  273. t = s;
  274. }
  275. if (f != this.fromIndex) {
  276. this.fraction = 1 - this.fraction;
  277. }
  278. //this.log("old", this.fromIndex, this.toIndex, "new", f, t);
  279. this.fromIndex = f;
  280. this.toIndex = t;
  281. },
  282. refresh: function() {
  283. if (this.$.animator.isAnimating()) {
  284. this.$.animator.stop();
  285. }
  286. this.startTransition();
  287. this.fraction = 1;
  288. this.stepTransition();
  289. this.finishTransition();
  290. },
  291. startTransition: function() {
  292. this.fromIndex = this.fromIndex != null ? this.fromIndex : this.lastIndex || 0;
  293. this.toIndex = this.toIndex != null ? this.toIndex : this.index;
  294. //this.log(this.id, this.fromIndex, this.toIndex);
  295. if (this.layout) {
  296. this.layout.start();
  297. }
  298. this.fireTransitionStart();
  299. },
  300. finishTransition: function() {
  301. if (this.layout) {
  302. this.layout.finish();
  303. }
  304. this.transitionPoints = [];
  305. this.fraction = 0;
  306. this.fromIndex = this.toIndex = null;
  307. this.fireTransitionFinish();
  308. },
  309. fireTransitionStart: function() {
  310. var t = this.startTransitionInfo;
  311. if (this.hasNode() && (!t || (t.fromIndex != this.fromIndex || t.toIndex != this.toIndex))) {
  312. this.startTransitionInfo = {fromIndex: this.fromIndex, toIndex: this.toIndex};
  313. this.doTransitionStart(enyo.clone(this.startTransitionInfo));
  314. }
  315. },
  316. fireTransitionFinish: function() {
  317. var t = this.finishTransitionInfo;
  318. if (this.hasNode() && (!t || (t.fromIndex != this.lastIndex || t.toIndex != this.index))) {
  319. this.finishTransitionInfo = {fromIndex: this.lastIndex, toIndex: this.index};
  320. this.doTransitionFinish(enyo.clone(this.finishTransitionInfo));
  321. }
  322. this.lastIndex=this.index;
  323. },
  324. // gambit: we interpolate between arrangements as needed.
  325. stepTransition: function() {
  326. if (this.hasNode()) {
  327. // select correct transition points and normalize fraction.
  328. var t$ = this.transitionPoints;
  329. var r = (this.fraction || 0) * (t$.length-1);
  330. var i = Math.floor(r);
  331. r = r - i;
  332. var s = t$[i], f = t$[i+1];
  333. // get arrangements and lerp between them
  334. var s0 = this.fetchArrangement(s);
  335. var s1 = this.fetchArrangement(f);
  336. this.arrangement = s0 && s1 ? enyo.Panels.lerp(s0, s1, r) : (s0 || s1);
  337. if (this.arrangement && this.layout) {
  338. this.layout.flowArrangement();
  339. }
  340. }
  341. },
  342. fetchArrangement: function(inName) {
  343. if ((inName != null) && !this.arrangements[inName] && this.layout) {
  344. this.layout._arrange(inName);
  345. this.arrangements[inName] = this.readArrangement(this.getPanels());
  346. }
  347. return this.arrangements[inName];
  348. },
  349. readArrangement: function(inC) {
  350. var r = [];
  351. for (var i=0, c$=inC, c; (c=c$[i]); i++) {
  352. r.push(enyo.clone(c._arranger));
  353. }
  354. return r;
  355. },
  356. statics: {
  357. isScreenNarrow: function() {
  358. return enyo.dom.getWindowWidth() <= 800;
  359. },
  360. lerp: function(inA0, inA1, inFrac) {
  361. var r = [];
  362. for (var i=0, k$=enyo.keys(inA0), k; (k=k$[i]); i++) {
  363. r.push(this.lerpObject(inA0[k], inA1[k], inFrac));
  364. }
  365. return r;
  366. },
  367. lerpObject: function(inNew, inOld, inFrac) {
  368. var b = enyo.clone(inNew), n, o;
  369. // inOld might be undefined when deleting panels
  370. if (inOld) {
  371. for (var i in inNew) {
  372. n = inNew[i];
  373. o = inOld[i];
  374. if (n != o) {
  375. b[i] = n - (n - o) * inFrac;
  376. }
  377. }
  378. }
  379. return b;
  380. }
  381. }
  382. });