move.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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, { duration: 0, queue: false });
  111. if(rootLayer) $ax.visibility.popContainer(rootLayer, false);
  112. if(animationCompleteCallback) animationCompleteCallback();
  113. //if this widget is inside a layer, we should just remove the layer from the queue
  114. if(shouldFire) $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.move);
  115. } else if (options.trajectory === 'straight' || moveInfo.horzX === 0 || moveInfo.vertY === 0) {
  116. query.animate(cssStyles, {
  117. duration: options.duration, easing: options.easing, queue: false, complete: completeAnimation});
  118. } else {
  119. var initialHorzProp = $ax.getNumFromPx(query.css(moveInfo.horzProp));
  120. var initialVertProp = $ax.getNumFromPx(query.css(moveInfo.vertProp));
  121. var state = { parameter: 0 };
  122. var ellipseArcFunctionY = function(param) {
  123. return {
  124. x: initialHorzProp + (1.0 - Math.cos(param * Math.PI * 0.5)) * moveInfo.horzX,
  125. y: initialVertProp + Math.sin(param * Math.PI * 0.5) * moveInfo.vertY
  126. };
  127. };
  128. var ellipseArcFunctionX = function (param) {
  129. return {
  130. x: initialHorzProp + Math.sin(param * Math.PI * 0.5) * moveInfo.horzX,
  131. y: initialVertProp + (1.0 - Math.cos(param * Math.PI * 0.5)) * moveInfo.vertY
  132. };
  133. };
  134. var ellipseArcFunction = (moveInfo.horzX > 0) ^ (moveInfo.vertY > 0) ^ options.trajectory === 'arcClockwise'
  135. ? ellipseArcFunctionX : ellipseArcFunctionY;
  136. var inverseFunction = $ax.public.fn.inversePathLengthFunction(ellipseArcFunction);
  137. $(state).animate({ parameter: 1.0 }, {
  138. duration: options.duration, easing: options.easing, queue: false,
  139. step: function (now) {
  140. var newPos = ellipseArcFunction(inverseFunction(now));
  141. var changeFields = {};
  142. changeFields[moveInfo.horzProp] = newPos.x;
  143. changeFields[moveInfo.vertProp] = newPos.y;
  144. query.css(changeFields);
  145. },
  146. complete: completeAnimation});
  147. }
  148. // //moveinfo is used for moving 'with this'
  149. // var moveInfo = new Object();
  150. // moveInfo.x = horzX;
  151. // moveInfo.y = vertY;
  152. // moveInfo.options = options;
  153. // widgetMoveInfo[id] = moveInfo;
  154. };
  155. _move.nopMove = function(id, options) {
  156. var moveInfo = new Object();
  157. moveInfo.x = 0;
  158. moveInfo.y = 0;
  159. moveInfo.options = {};
  160. moveInfo.options.easing = 'none';
  161. moveInfo.options.duration = 0;
  162. widgetMoveInfo[id] = moveInfo;
  163. // Layer move using container now.
  164. var obj = $obj(id);
  165. if($ax.public.fn.IsLayer(obj.type)) if(options.onComplete) options.onComplete();
  166. $ax.event.raiseSyntheticEvent(id, "onMove");
  167. };
  168. //rotationDegree: total degree to rotate
  169. //centerPoint: the center of the circular path
  170. var _noRotateOnlyMove = function (id, moveDelta, rotatableMove, fireAnimationQueue, easing, duration, completionCallback) {
  171. moveDelta.x += rotatableMove.x;
  172. moveDelta.y += rotatableMove.y;
  173. if (moveDelta.x == 0 && moveDelta.y == 0) {
  174. if(fireAnimationQueue) {
  175. $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.rotate);
  176. $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.move);
  177. }
  178. if (completionCallback) completionCallback();
  179. } else {
  180. $jobj(id).animate({ top: '+=' + moveDelta.y, left: '+=' + moveDelta.x }, {
  181. duration: duration,
  182. easing: easing,
  183. queue: false,
  184. complete: function () {
  185. if(fireAnimationQueue) {
  186. $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.move);
  187. $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.rotate);
  188. }
  189. if (completionCallback) completionCallback();
  190. }
  191. });
  192. }
  193. }
  194. _move.circularMove = function (id, degreeDelta, centerPoint, moveDelta, rotatableMove, resizeOffset, options, fireAnimationQueue, completionCallback, willDoRotation) {
  195. var elem = $jobj(id);
  196. if(!willDoRotation) elem = $addAll(elem, id);
  197. var moveInfo = $ax.move.PrepareForMove(id, moveDelta.x, moveDelta.y, false, options);
  198. // If not rotating, still need to check moveDelta and may need to handle that.
  199. if (degreeDelta === 0) {
  200. _noRotateOnlyMove(id, moveDelta, rotatableMove, fireAnimationQueue, options.easing, options.duration, completionCallback);
  201. return;
  202. }
  203. var stepFunc = function(newDegree) {
  204. var deg = newDegree - rotation.degree;
  205. var widgetCenter = $ax('#' + id).offsetBoundingRect().centerPoint;
  206. //var widgetCenter = $ax.public.fn.getWidgetBoundingRect(id).centerPoint;
  207. //console.log("widget center of " + id + " x " + widgetCenter.x + " y " + widgetCenter.y);
  208. var widgetNewCenter = $axure.fn.getPointAfterRotate(deg, widgetCenter, centerPoint);
  209. // Start by getting the move not related to rotation, and make sure to update center point to move with it.
  210. var ratio = deg / degreeDelta;
  211. var xdelta = (moveDelta.x + rotatableMove.x) * ratio;
  212. var ydelta = (moveDelta.y + rotatableMove.y) * ratio;
  213. if(resizeOffset) {
  214. var resizeShift = {};
  215. resizeShift.x = resizeOffset.x * ratio;
  216. resizeShift.y = resizeOffset.y * ratio;
  217. $axure.fn.getPointAfterRotate(rotation.degree, resizeShift, { x: 0, y: 0 });
  218. xdelta += resizeShift.x;
  219. ydelta += resizeShift.y;
  220. }
  221. centerPoint.x += xdelta;
  222. centerPoint.y += ydelta;
  223. // Now for the move that is rotatable, it must be rotated
  224. rotatableMove = $axure.fn.getPointAfterRotate(deg, rotatableMove, { x: 0, y: 0 });
  225. // Now add in circular move to the mix.
  226. xdelta += widgetNewCenter.x - widgetCenter.x;
  227. ydelta += widgetNewCenter.y - widgetCenter.y;
  228. $ax.visibility.moveMovedLocation(id, xdelta, ydelta);
  229. if(xdelta < 0) elem.css('left', '-=' + -xdelta);
  230. else if(xdelta > 0) elem.css('left', '+=' + xdelta);
  231. if(ydelta < 0) elem.css('top', '-=' + -ydelta);
  232. else if(ydelta > 0) elem.css('top', '+=' + ydelta);
  233. };
  234. var onComplete = function() {
  235. if(fireAnimationQueue) $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.move);
  236. if(completionCallback) completionCallback();
  237. if(moveInfo.rootLayer) $ax.visibility.popContainer(moveInfo.rootLayer, false);
  238. var isPercentWidthPanel = $ax.dynamicPanelManager.isPercentWidthPanel($obj(id));
  239. if(isPercentWidthPanel) {
  240. $ax.dynamicPanelManager.updatePanelPercentWidth(id);
  241. $ax.dynamicPanelManager.updatePanelContentPercentWidth(id);
  242. }
  243. if(elem.css('position') == 'fixed') {
  244. if(!isPercentWidthPanel) elem.css('left', '');
  245. elem.css('top', '');
  246. }
  247. };
  248. var rotation = { degree: 0 };
  249. if(!options.easing || options.easing === 'none' || options.duration <= 0) {
  250. stepFunc(degreeDelta);
  251. onComplete();
  252. } else {
  253. $(rotation).animate({ degree: degreeDelta }, {
  254. duration: options.duration,
  255. easing: options.easing,
  256. queue: false,
  257. step: stepFunc,
  258. complete: onComplete
  259. });
  260. }
  261. };
  262. //rotate a widget by degree, center is 50% 50%
  263. _move.rotate = function (id, degree, easing, duration, to, shouldFire, completionCallback) {
  264. var currentDegree = _move.getRotationDegree(id);
  265. if(to) degree = degree - currentDegree;
  266. if(degree === 0) {
  267. if (shouldFire) $ax.action.fireAnimationFromQueue(id, $ax.action.queueTypes.rotate);
  268. return;
  269. }
  270. var query = $jobj(id);
  271. var stepFunc = function(now) {
  272. var degreeDelta = now - rotation.degree;
  273. var newDegree = currentDegree + degreeDelta;
  274. query.css($ax.public.fn.setTransformHowever("rotate(" + newDegree + "deg)"));
  275. currentDegree = newDegree;
  276. };
  277. var onComplete = function() {
  278. if(shouldFire) {
  279. $ax.action.fireAnimationFromQueue($ax.public.fn.compoundIdFromComponent(id), $ax.action.queueTypes.rotate);
  280. }
  281. if(completionCallback) completionCallback();
  282. $ax.annotation.adjustIconLocation(id);
  283. };
  284. var rotation = { degree: 0 };
  285. $ax.visibility.setRotatedAngle(id, currentDegree + degree);
  286. //if no animation, setting duration to 1, to prevent RangeError in rotation loops without animation
  287. if(!easing || easing === 'none' || duration <= 0) {
  288. stepFunc(degree);
  289. onComplete();
  290. } else {
  291. $(rotation).animate({ degree: degree }, {
  292. duration: duration,
  293. easing: easing,
  294. queue: false,
  295. step: stepFunc,
  296. complete: onComplete
  297. });
  298. }
  299. };
  300. _move.compoundRotateAround = function (id, degreeDelta, centerPoint, moveDelta, rotatableMove, resizeOffset, easing, duration, fireAnimationQueue, completionCallback) {
  301. if (degreeDelta === 0) {
  302. _noRotateOnlyMove($ax.public.fn.compoundIdFromComponent(id), moveDelta, rotatableMove, fireAnimationQueue, easing, duration, completionCallback, $ax.action.queueTypes.rotate);
  303. return;
  304. }
  305. var elem = $jobj(id);
  306. var rotation = { degree: 0 };
  307. if (!easing || easing === 'none' || duration <= 0) {
  308. duration = 1;
  309. easing = 'linear'; //it doesn't matter anymore here...
  310. }
  311. var originalWidth = $ax.getNumFromPx(elem.css('width'));
  312. var originalHeight = $ax.getNumFromPx(elem.css('height'));
  313. var originalLeft = $ax.getNumFromPx(elem.css('left'));
  314. var originalTop = $ax.getNumFromPx(elem.css('top'));
  315. $(rotation).animate({ degree: degreeDelta }, {
  316. duration: duration,
  317. easing: easing,
  318. queue: false,
  319. step: function (newDegree) {
  320. var transform = $ax.public.fn.transformFromElement(elem[0]);
  321. var originalCenter = { x: originalLeft + 0.5 * originalWidth, y: originalTop + 0.5 * originalHeight};
  322. var componentCenter = { x: originalCenter.x + transform[4], y: originalCenter.y + transform[5] };
  323. var deg = newDegree - rotation.degree;
  324. var ratio = deg / degreeDelta;
  325. var xdelta = (moveDelta.x + rotatableMove.x) * ratio;
  326. var ydelta = (moveDelta.y + rotatableMove.y) * ratio;
  327. if (resizeOffset) {
  328. var resizeShift = {};
  329. resizeShift.x = resizeOffset.x * ratio;
  330. resizeShift.y = resizeOffset.y * ratio;
  331. $axure.fn.getPointAfterRotate(rotation.degree, resizeShift, { x: 0, y: 0 });
  332. xdelta += resizeShift.x;
  333. ydelta += resizeShift.y;
  334. }
  335. var rotationMatrix = $ax.public.fn.rotationMatrix(deg);
  336. var compositionTransform = $ax.public.fn.matrixMultiplyMatrix(rotationMatrix,
  337. { m11: transform[0], m21: transform[1], m12: transform[2], m22: transform[3] });
  338. //console.log("widget center of " + id + " x " + widgetCenter.x + " y " + widgetCenter.y);
  339. var widgetNewCenter = $axure.fn.getPointAfterRotate(deg, componentCenter, centerPoint);
  340. var newMatrix = $ax.public.fn.matrixString(compositionTransform.m11, compositionTransform.m21, compositionTransform.m12, compositionTransform.m22,
  341. widgetNewCenter.x - originalCenter.x + xdelta, widgetNewCenter.y - originalCenter.y + ydelta);
  342. elem.css($ax.public.fn.setTransformHowever(newMatrix));
  343. },
  344. complete: function () {
  345. if (fireAnimationQueue) {
  346. $ax.action.fireAnimationFromQueue(elem.parent()[0].id, $ax.action.queueTypes.rotate);
  347. }
  348. if(completionCallback) completionCallback();
  349. }
  350. });
  351. };
  352. _move.getRotationDegreeFromElement = function(element) {
  353. if(element == null) return NaN;
  354. var transformString = element.style['transform'] ||
  355. element.style['-o-transform'] ||
  356. element.style['-ms-transform'] ||
  357. element.style['-moz-transform'] ||
  358. element.style['-webkit-transform'];
  359. if(transformString) {
  360. var rotateRegex = /rotate\(([-?0-9]+)deg\)/;
  361. var degreeMatch = rotateRegex.exec(transformString);
  362. if(degreeMatch && degreeMatch[1]) return parseFloat(degreeMatch[1]);
  363. }
  364. if(window.getComputedStyle) {
  365. var st = window.getComputedStyle(element, null);
  366. } else {
  367. console.log('rotation is not supported for ie 8 and below in this version of axure rp');
  368. return 0;
  369. }
  370. var tr = st.getPropertyValue("transform") ||
  371. st.getPropertyValue("-o-transform") ||
  372. st.getPropertyValue("-ms-transform") ||
  373. st.getPropertyValue("-moz-transform") ||
  374. st.getPropertyValue("-webkit-transform");
  375. if(!tr || tr === 'none') return 0;
  376. var values = tr.split('(')[1];
  377. values = values.split(')')[0],
  378. values = values.split(',');
  379. var a = values[0];
  380. var b = values[1];
  381. var radians = Math.atan2(b, a);
  382. if(radians < 0) {
  383. radians += (2 * Math.PI);
  384. }
  385. return radians * (180 / Math.PI);
  386. };
  387. _move.getRotationDegree = function(elementId) {
  388. if($ax.public.fn.IsLayer($obj(elementId).type)) {
  389. return $jobj(elementId).data('layerDegree');
  390. }
  391. return _move.getRotationDegreeFromElement(document.getElementById(elementId));
  392. }
  393. });