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.

466 lines
20 KiB

3 years ago
  1. $axure.internal(function($ax) {
  2. var _move = {};
  3. $ax.move = _move;
  4. var widgetMoveInfo = {};
  5. //register and return move info, also create container for rootlayer if needed
  6. $ax.move.PrepareForMove = function (id, x, y, to, options, jobj, rootLayer, skipContainerForRootLayer) {
  7. var fixedInfo = jobj ? {} : $ax.dynamicPanelManager.getFixedInfo(id);
  8. var widget = $jobj(id);
  9. var query = $ax('#' + id);
  10. var isLayer = $ax.getTypeFromElementId(id) == $ax.constants.LAYER_TYPE;
  11. if(!rootLayer) {
  12. rootLayer = _move.getRootLayer(id);
  13. if (rootLayer && !skipContainerForRootLayer) {
  14. $ax.visibility.pushContainer(rootLayer, false);
  15. if (isLayer) widget = $ax.visibility.applyWidgetContainer(id, true);
  16. }
  17. }
  18. if (!jobj) jobj = widget;
  19. var horzProp = 'left';
  20. var vertProp = 'top';
  21. var offsetLocation = to ? query.offsetLocation() : undefined;
  22. var horzX = to ? x - offsetLocation.x : x;
  23. var vertY = to ? y - offsetLocation.y : y;
  24. //var horzX = to ? x - query.locRelativeIgnoreLayer(false) : x;
  25. //var vertY = to ? y - query.locRelativeIgnoreLayer(true) : y;
  26. if (fixedInfo.horizontal == 'right') {
  27. horzProp = 'right';
  28. horzX = to ? $(window).width() - x - $ax.getNumFromPx(jobj.css('right')) - query.width() : -x;
  29. var leftChanges = -horzX;
  30. } else if(fixedInfo.horizontal == 'center') {
  31. horzProp = 'margin-left';
  32. if (to) horzX = x - $(window).width() / 2;
  33. }
  34. if (fixedInfo.vertical == 'bottom') {
  35. vertProp = 'bottom';
  36. vertY = to ? $(window).height() - y - $ax.getNumFromPx(jobj.css('bottom')) - query.height() : -y;
  37. var topChanges = -vertY;
  38. } else if (fixedInfo.vertical == 'middle') {
  39. vertProp = 'margin-top';
  40. if (to) vertY = y - $(window).height() / 2;
  41. }
  42. //todo currently this always save the info, which is not needed for compound vector children and maybe some other cases
  43. //let's optimize it later, only register if registerid is valid..
  44. widgetMoveInfo[id] = {
  45. x: leftChanges === undefined ? horzX : leftChanges,
  46. y: topChanges === undefined ? vertY : topChanges,
  47. options: options
  48. };
  49. return {
  50. horzX: horzX,
  51. vertY: vertY,
  52. horzProp: horzProp,
  53. vertProp: vertProp,
  54. rootLayer: rootLayer,
  55. jobj: jobj
  56. };
  57. };
  58. $ax.move.GetWidgetMoveInfo = function() {
  59. return $.extend({}, widgetMoveInfo);
  60. };
  61. _move.getRootLayer = function (id) {
  62. var isLayer = $ax.getTypeFromElementId(id) == $ax.constants.LAYER_TYPE;
  63. var rootLayer = isLayer ? id : '';
  64. var parentIds = $ax('#' + id).getParents(true, '*')[0];
  65. for(var i = 0; i < parentIds.length; i++) {
  66. var parentId = parentIds[i];
  67. // Keep climbing up layers until you hit a non-layer. At that point you have your root layer
  68. if($ax.public.fn.IsLayer($ax.getTypeFromElementId(parentId))) rootLayer = parentId;
  69. else break;
  70. }
  71. return rootLayer;
  72. };
  73. $ax.move.MoveWidget = function (id, x, y, options, to, animationCompleteCallback, shouldFire, jobj, skipOnMoveEvent) {
  74. var moveInfo = $ax.move.PrepareForMove(id, x, y, to, options, jobj);
  75. $ax.drag.LogMovedWidgetForDrag(id, options.dragInfo);
  76. var object = $obj(id);
  77. if(object && $ax.public.fn.IsLayer(object.type)) {
  78. var childrenIds = $ax.public.fn.getLayerChildrenDeep(id, true);
  79. //don't push container when register moveinfo for child
  80. if(!skipOnMoveEvent) {
  81. for(var i = 0; i < childrenIds.length; i++) $ax.move.PrepareForMove(childrenIds[i], x, y, to, options, null, moveInfo.rootLayer, true);
  82. }
  83. }
  84. //if(!moveInfo) moveInfo = _getMoveInfo(id, x, y, to, options, jobj);
  85. jobj = moveInfo.jobj;
  86. _moveElement(id, options, animationCompleteCallback, shouldFire, jobj, moveInfo);
  87. if(skipOnMoveEvent) return;
  88. $ax.event.raiseSyntheticEvent(id, "onMove");
  89. if(childrenIds) {
  90. for(var i = 0; i < childrenIds.length; i++) $ax.event.raiseSyntheticEvent(childrenIds[i], 'onMove');
  91. }
  92. };
  93. var _moveElement = function (id, options, animationCompleteCallback, shouldFire, jobj, moveInfo){
  94. var cssStyles = {};
  95. if(!$ax.dynamicPanelManager.isPercentWidthPanel($obj(id))) cssStyles[moveInfo.horzProp] = '+=' + moveInfo.horzX;
  96. cssStyles[moveInfo.vertProp] = '+=' + moveInfo.vertY;
  97. $ax.visibility.moveMovedLocation(id, moveInfo.horzX, moveInfo.vertY);
  98. // I don't think root layer is necessary anymore after changes to layer container structure.
  99. // Wait to try removing it until more stable.
  100. var rootLayer = moveInfo.rootLayer;
  101. var query = $addAll(jobj, id);
  102. var completeCount = query.length;
  103. var completeAnimation = function() {
  104. completeCount--;
  105. if(completeCount == 0 && rootLayer) $ax.visibility.popContainer(rootLayer, false);
  106. if(animationCompleteCallback) animationCompleteCallback();
  107. if(shouldFire) $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.move);
  108. };
  109. if(options.easing==='none') {
  110. query.animate(cssStyles, {
  111. duration: 0,
  112. queue: false,
  113. complete: function() { //this animation somehow is not fired without this complete function
  114. if(rootLayer) $ax.visibility.popContainer(rootLayer, false);
  115. if(animationCompleteCallback) animationCompleteCallback();
  116. }
  117. });
  118. //if this widget is inside a layer, we should just remove the layer from the queue
  119. if(shouldFire) $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.move);
  120. } else if (options.trajectory === 'straight' || moveInfo.horzX === 0 || moveInfo.vertY === 0) {
  121. query.animate(cssStyles, {
  122. duration: options.duration, easing: options.easing, queue: false, complete: completeAnimation});
  123. } else {
  124. var initialHorzProp = $ax.getNumFromPx(query.css(moveInfo.horzProp));
  125. var initialVertProp = $ax.getNumFromPx(query.css(moveInfo.vertProp));
  126. var state = { parameter: 0 };
  127. var ellipseArcFunctionY = function(param) {
  128. return {
  129. x: initialHorzProp + (1.0 - Math.cos(param * Math.PI * 0.5)) * moveInfo.horzX,
  130. y: initialVertProp + Math.sin(param * Math.PI * 0.5) * moveInfo.vertY
  131. };
  132. };
  133. var ellipseArcFunctionX = function (param) {
  134. return {
  135. x: initialHorzProp + Math.sin(param * Math.PI * 0.5) * moveInfo.horzX,
  136. y: initialVertProp + (1.0 - Math.cos(param * Math.PI * 0.5)) * moveInfo.vertY
  137. };
  138. };
  139. var ellipseArcFunction = (moveInfo.horzX > 0) ^ (moveInfo.vertY > 0) ^ options.trajectory === 'arcClockwise'
  140. ? ellipseArcFunctionX : ellipseArcFunctionY;
  141. var inverseFunction = $ax.public.fn.inversePathLengthFunction(ellipseArcFunction);
  142. $(state).animate({ parameter: 1.0 }, {
  143. duration: options.duration, easing: options.easing, queue: false,
  144. step: function (now) {
  145. var newPos = ellipseArcFunction(inverseFunction(now));
  146. var changeFields = {};
  147. changeFields[moveInfo.horzProp] = newPos.x;
  148. changeFields[moveInfo.vertProp] = newPos.y;
  149. query.css(changeFields);
  150. },
  151. complete: completeAnimation});
  152. }
  153. // //moveinfo is used for moving 'with this'
  154. // var moveInfo = new Object();
  155. // moveInfo.x = horzX;
  156. // moveInfo.y = vertY;
  157. // moveInfo.options = options;
  158. // widgetMoveInfo[id] = moveInfo;
  159. };
  160. _move.nopMove = function(id, options) {
  161. var moveInfo = new Object();
  162. moveInfo.x = 0;
  163. moveInfo.y = 0;
  164. moveInfo.options = {};
  165. moveInfo.options.easing = 'none';
  166. moveInfo.options.duration = 0;
  167. widgetMoveInfo[id] = moveInfo;
  168. // Layer move using container now.
  169. var obj = $obj(id);
  170. if($ax.public.fn.IsLayer(obj.type)) if(options.onComplete) options.onComplete();
  171. $ax.event.raiseSyntheticEvent(id, "onMove");
  172. };
  173. //rotationDegree: total degree to rotate
  174. //centerPoint: the center of the circular path
  175. var _noRotateOnlyMove = function (id, moveDelta, rotatableMove, fireAnimationQueue, easing, duration, completionCallback) {
  176. moveDelta.x += rotatableMove.x;
  177. moveDelta.y += rotatableMove.y;
  178. if (moveDelta.x == 0 && moveDelta.y == 0) {
  179. if(fireAnimationQueue) {
  180. $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.rotate);
  181. $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.move);
  182. }
  183. if (completionCallback) completionCallback();
  184. } else {
  185. $jobj(id).animate({ top: '+=' + moveDelta.y, left: '+=' + moveDelta.x }, {
  186. duration: duration,
  187. easing: easing,
  188. queue: false,
  189. complete: function () {
  190. if(fireAnimationQueue) {
  191. $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.move);
  192. $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.rotate);
  193. }
  194. if (completionCallback) completionCallback();
  195. }
  196. });
  197. }
  198. }
  199. _move.circularMove = function (id, degreeDelta, centerPoint, moveDelta, rotatableMove, resizeOffset, options, fireAnimationQueue, completionCallback, willDoRotation) {
  200. var elem = $jobj(id);
  201. if(!willDoRotation) elem = $addAll(elem, id);
  202. var moveInfo = $ax.move.PrepareForMove(id, moveDelta.x, moveDelta.y, false, options);
  203. // If not rotating, still need to check moveDelta and may need to handle that.
  204. if (degreeDelta === 0) {
  205. _noRotateOnlyMove(id, moveDelta, rotatableMove, fireAnimationQueue, options.easing, options.duration, completionCallback);
  206. return;
  207. }
  208. var stepFunc = function(newDegree) {
  209. var deg = newDegree - rotation.degree;
  210. var widgetCenter = $ax('#' + id).offsetBoundingRect().centerPoint;
  211. //var widgetCenter = $ax.public.fn.getWidgetBoundingRect(id).centerPoint;
  212. //console.log("widget center of " + id + " x " + widgetCenter.x + " y " + widgetCenter.y);
  213. var widgetNewCenter = $axure.fn.getPointAfterRotate(deg, widgetCenter, centerPoint);
  214. // Start by getting the move not related to rotation, and make sure to update center point to move with it.
  215. var ratio = deg / degreeDelta;
  216. var xdelta = (moveDelta.x + rotatableMove.x) * ratio;
  217. var ydelta = (moveDelta.y + rotatableMove.y) * ratio;
  218. if(resizeOffset) {
  219. var resizeShift = {};
  220. resizeShift.x = resizeOffset.x * ratio;
  221. resizeShift.y = resizeOffset.y * ratio;
  222. $axure.fn.getPointAfterRotate(rotation.degree, resizeShift, { x: 0, y: 0 });
  223. xdelta += resizeShift.x;
  224. ydelta += resizeShift.y;
  225. }
  226. centerPoint.x += xdelta;
  227. centerPoint.y += ydelta;
  228. // Now for the move that is rotatable, it must be rotated
  229. rotatableMove = $axure.fn.getPointAfterRotate(deg, rotatableMove, { x: 0, y: 0 });
  230. // Now add in circular move to the mix.
  231. xdelta += widgetNewCenter.x - widgetCenter.x;
  232. ydelta += widgetNewCenter.y - widgetCenter.y;
  233. $ax.visibility.moveMovedLocation(id, xdelta, ydelta);
  234. if(xdelta < 0) elem.css('left', '-=' + -xdelta);
  235. else if(xdelta > 0) elem.css('left', '+=' + xdelta);
  236. if(ydelta < 0) elem.css('top', '-=' + -ydelta);
  237. else if(ydelta > 0) elem.css('top', '+=' + ydelta);
  238. };
  239. var onComplete = function() {
  240. if(fireAnimationQueue) $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.move);
  241. if(completionCallback) completionCallback();
  242. if(moveInfo.rootLayer) $ax.visibility.popContainer(moveInfo.rootLayer, false);
  243. var isPercentWidthPanel = $ax.dynamicPanelManager.isPercentWidthPanel($obj(id));
  244. if(isPercentWidthPanel) {
  245. $ax.dynamicPanelManager.updatePanelPercentWidth(id);
  246. $ax.dynamicPanelManager.updatePanelContentPercentWidth(id);
  247. }
  248. if(elem.css('position') == 'fixed') {
  249. if(!isPercentWidthPanel) elem.css('left', '');
  250. elem.css('top', '');
  251. }
  252. };
  253. var rotation = { degree: 0 };
  254. if(!options.easing || options.easing === 'none' || options.duration <= 0) {
  255. stepFunc(degreeDelta);
  256. onComplete();
  257. } else {
  258. $(rotation).animate({ degree: degreeDelta }, {
  259. duration: options.duration,
  260. easing: options.easing,
  261. queue: false,
  262. step: stepFunc,
  263. complete: onComplete
  264. });
  265. }
  266. };
  267. //rotate a widget by degree, center is 50% 50%
  268. _move.rotate = function (id, degree, easing, duration, to, shouldFire, completionCallback) {
  269. var currentDegree = _move.getRotationDegree(id);
  270. if(to) degree = degree - currentDegree;
  271. if(degree === 0) {
  272. if (shouldFire) $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.rotate);
  273. return;
  274. }
  275. var query = $jobj(id);
  276. var stepFunc = function(now) {
  277. var degreeDelta = now - rotation.degree;
  278. var newDegree = currentDegree + degreeDelta;
  279. query.css($ax.public.fn.setTransformHowever("rotate(" + newDegree + "deg)"));
  280. currentDegree = newDegree;
  281. };
  282. var onComplete = function() {
  283. if(shouldFire) {
  284. $ax.action.fireAnimationFromQueue($ax.public.fn.compoundIdFromComponent(id), $ax.action.queueTypes.rotate);
  285. }
  286. if(completionCallback) completionCallback();
  287. $ax.annotation.adjustIconLocation(id);
  288. };
  289. var rotation = { degree: 0 };
  290. $ax.visibility.setRotatedAngle(id, currentDegree + degree);
  291. //if no animation, setting duration to 1, to prevent RangeError in rotation loops without animation
  292. if(!easing || easing === 'none' || duration <= 0) {
  293. stepFunc(degree);
  294. onComplete();
  295. } else {
  296. $(rotation).animate({ degree: degree }, {
  297. duration: duration,
  298. easing: easing,
  299. queue: false,
  300. step: stepFunc,
  301. complete: onComplete
  302. });
  303. }
  304. };
  305. _move.compoundRotateAround = function (id, degreeDelta, centerPoint, moveDelta, rotatableMove, resizeOffset, easing, duration, fireAnimationQueue, completionCallback) {
  306. if (degreeDelta === 0) {
  307. _noRotateOnlyMove($ax.public.fn.compoundIdFromComponent(id), moveDelta, rotatableMove, fireAnimationQueue, easing, duration, completionCallback, $ax.action.queueTypes.rotate);
  308. return;
  309. }
  310. var elem = $jobj(id);
  311. var rotation = { degree: 0 };
  312. if (!easing || easing === 'none' || duration <= 0) {
  313. duration = 1;
  314. easing = 'linear'; //it doesn't matter anymore here...
  315. }
  316. var originalWidth = $ax.getNumFromPx(elem.css('width'));
  317. var originalHeight = $ax.getNumFromPx(elem.css('height'));
  318. var originalLeft = $ax.getNumFromPx(elem.css('left'));
  319. var originalTop = $ax.getNumFromPx(elem.css('top'));
  320. $(rotation).animate({ degree: degreeDelta }, {
  321. duration: duration,
  322. easing: easing,
  323. queue: false,
  324. step: function (newDegree) {
  325. var transform = $ax.public.fn.transformFromElement(elem[0]);
  326. var originalCenter = { x: originalLeft + 0.5 * originalWidth, y: originalTop + 0.5 * originalHeight};
  327. var componentCenter = { x: originalCenter.x + transform[4], y: originalCenter.y + transform[5] };
  328. var deg = newDegree - rotation.degree;
  329. var ratio = deg / degreeDelta;
  330. var xdelta = (moveDelta.x + rotatableMove.x) * ratio;
  331. var ydelta = (moveDelta.y + rotatableMove.y) * ratio;
  332. if (resizeOffset) {
  333. var resizeShift = {};
  334. resizeShift.x = resizeOffset.x * ratio;
  335. resizeShift.y = resizeOffset.y * ratio;
  336. $axure.fn.getPointAfterRotate(rotation.degree, resizeShift, { x: 0, y: 0 });
  337. xdelta += resizeShift.x;
  338. ydelta += resizeShift.y;
  339. }
  340. var rotationMatrix = $ax.public.fn.rotationMatrix(deg);
  341. var compositionTransform = $ax.public.fn.matrixMultiplyMatrix(rotationMatrix,
  342. { m11: transform[0], m21: transform[1], m12: transform[2], m22: transform[3] });
  343. //console.log("widget center of " + id + " x " + widgetCenter.x + " y " + widgetCenter.y);
  344. var widgetNewCenter = $axure.fn.getPointAfterRotate(deg, componentCenter, centerPoint);
  345. var newMatrix = $ax.public.fn.matrixString(compositionTransform.m11, compositionTransform.m21, compositionTransform.m12, compositionTransform.m22,
  346. widgetNewCenter.x - originalCenter.x + xdelta, widgetNewCenter.y - originalCenter.y + ydelta);
  347. elem.css($ax.public.fn.setTransformHowever(newMatrix));
  348. },
  349. complete: function () {
  350. if (fireAnimationQueue) {
  351. $ax.action.fireAnimationFromQueue(elem.parent()[0].id, $ax.action.queueTypes.rotate);
  352. }
  353. if(completionCallback) completionCallback();
  354. }
  355. });
  356. };
  357. _move.getRotationDegreeFromElement = function(element) {
  358. if(element == null) return NaN;
  359. var transformString = element.style['transform'] ||
  360. element.style['-o-transform'] ||
  361. element.style['-ms-transform'] ||
  362. element.style['-moz-transform'] ||
  363. element.style['-webkit-transform'];
  364. if(transformString) {
  365. var rotateRegex = /rotate\(([-?0-9]+)deg\)/;
  366. var degreeMatch = rotateRegex.exec(transformString);
  367. if(degreeMatch && degreeMatch[1]) return parseFloat(degreeMatch[1]);
  368. }
  369. if(window.getComputedStyle) {
  370. var st = window.getComputedStyle(element, null);
  371. } else {
  372. console.log('rotation is not supported for ie 8 and below in this version of axure rp');
  373. return 0;
  374. }
  375. var tr = st.getPropertyValue("transform") ||
  376. st.getPropertyValue("-o-transform") ||
  377. st.getPropertyValue("-ms-transform") ||
  378. st.getPropertyValue("-moz-transform") ||
  379. st.getPropertyValue("-webkit-transform");
  380. if(!tr || tr === 'none') return 0;
  381. var values = tr.split('(')[1];
  382. values = values.split(')')[0],
  383. values = values.split(',');
  384. var a = values[0];
  385. var b = values[1];
  386. var radians = Math.atan2(b, a);
  387. if(radians < 0) {
  388. radians += (2 * Math.PI);
  389. }
  390. return radians * (180 / Math.PI);
  391. };
  392. _move.getRotationDegree = function(elementId) {
  393. if($ax.public.fn.IsLayer($obj(elementId).type)) {
  394. return $jobj(elementId).data('layerDegree');
  395. }
  396. return _move.getRotationDegreeFromElement(document.getElementById(elementId));
  397. }
  398. });