产品原型
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.

899 lines
37 KiB

4 years ago
  1. $axure.internal(function($ax) {
  2. var _pageData;
  3. var _initializePageFragment = function(pageFragment, objIdToObject) {
  4. var objectArrayHelper = function(objects, parent) {
  5. for(var i = 0; i < objects.length; i++) {
  6. diagramObjectHelper(objects[i], parent);
  7. }
  8. };
  9. var diagramObjectHelper = function(diagramObject, parent) {
  10. $ax.initializeObject('diagramObject', diagramObject);
  11. objIdToObject[pageFragment.packageId + '~' + diagramObject.id] = diagramObject;
  12. diagramObject.parent = parent;
  13. diagramObject.owner = pageFragment;
  14. diagramObject.scriptIds = [];
  15. if(diagramObject.diagrams) { //dynamic panel
  16. for(var i = 0; i < diagramObject.diagrams.length; i++) {
  17. var diagram = diagramObject.diagrams[i];
  18. objectArrayHelper(diagram.objects, diagram);
  19. }
  20. } else if($ax.public.fn.IsLayer(diagramObject.type)) {
  21. var layerObjs = diagramObject.objs;
  22. objectArrayHelper(layerObjs, parent);
  23. }
  24. if(diagramObject.objects) objectArrayHelper(diagramObject.objects, diagramObject);
  25. };
  26. objectArrayHelper(pageFragment.diagram.objects, pageFragment.diagram);
  27. };
  28. var _initalizeStylesheet = function(stylesheet) {
  29. var stylesById = {};
  30. var customStyles = stylesheet.customStyles;
  31. for(var key in customStyles) {
  32. var style = customStyles[key];
  33. stylesById[style.id] = style;
  34. }
  35. var duplicateStyles = stylesheet.duplicateStyles;
  36. for(var duplicateKey in duplicateStyles) {
  37. stylesById[duplicateKey] = stylesById[duplicateStyles[duplicateKey]];
  38. }
  39. stylesheet.stylesById = stylesById;
  40. };
  41. var _initializeDocumentData = function() {
  42. _initalizeStylesheet($ax.document.stylesheet);
  43. };
  44. var _initializePageData;
  45. // ******* Dictionaries ******** //
  46. (function() {
  47. var scriptIdToParentLayer = {};
  48. var elementIdToObject = {};
  49. var scriptIdToObject = {};
  50. var scriptIdToRepeaterId = {};
  51. var repeaterIdToScriptIds = {};
  52. var repeaterIdToItemIds = {};
  53. var scriptIdToPath = {};
  54. var _scriptIds = [];
  55. var elementIdToText = {};
  56. var radioGroupToSelectedElementId = {};
  57. _initializePageData = function() {
  58. if(!_pageData || !_pageData.page || !_pageData.page.diagram) return;
  59. var objIdToObject = {};
  60. _initializePageFragment(_pageData.page, objIdToObject);
  61. for(var masterId in _pageData.masters) {
  62. var master = _pageData.masters[masterId];
  63. _initializePageFragment(master, objIdToObject);
  64. }
  65. var _pathsToScriptIds = [];
  66. _pathToScriptIdHelper(_pageData.objectPaths, [], _pathsToScriptIds, scriptIdToPath);
  67. for(var i = 0; i < _pathsToScriptIds.length; i++) {
  68. var path = _pathsToScriptIds[i].idPath;
  69. var scriptId = _pathsToScriptIds[i].scriptId;
  70. var packageId = _pageData.page.packageId;
  71. if(path.length > 1) {
  72. for(var j = 0; j < path.length - 1; j++) {
  73. var rdoId = path[j];
  74. var rdo = objIdToObject[packageId + '~' + rdoId];
  75. packageId = rdo.masterId;
  76. }
  77. }
  78. var diagramObject = objIdToObject[packageId + '~' + path[path.length - 1]];
  79. diagramObject.scriptIds[diagramObject.scriptIds.length] = scriptId;
  80. scriptIdToObject[scriptId] = diagramObject;
  81. _scriptIds[_scriptIds.length] = scriptId;
  82. }
  83. // Now map scriptIds to repeaters and layers
  84. var mapScriptIdToRepeaterId = function(scriptId, repeaterId) {
  85. scriptIdToRepeaterId[scriptId] = repeaterId;
  86. var scriptIds = repeaterIdToScriptIds[repeaterId];
  87. if(scriptIds) scriptIds[scriptIds.length] = scriptId;
  88. else repeaterIdToScriptIds[repeaterId] = [scriptId];
  89. };
  90. var mapScriptIdToLayerId = function(obj, layerId, path) {
  91. var pathCopy = $ax.deepCopy(path);
  92. pathCopy[path.length] = obj.id;
  93. var scriptId = $ax.getScriptIdFromPath(pathCopy);
  94. scriptIdToParentLayer[scriptId] = layerId;
  95. }
  96. var mapIdsToRepeaterAndLayer = function(path, objs, repeaterId) {
  97. var pathCopy = $ax.deepCopy(path);
  98. for(var i = 0; i < objs.length; i++) {
  99. var obj = objs[i];
  100. pathCopy[path.length] = obj.id;
  101. var scriptId = $ax.getScriptIdFromPath(pathCopy);
  102. // Rdo have no element on page and are not mapped to the repeater
  103. if(repeaterId) mapScriptIdToRepeaterId(scriptId, repeaterId);
  104. if($ax.public.fn.IsDynamicPanel(obj.type)) {
  105. for(var j = 0; j < obj.diagrams.length; j++) mapIdsToRepeaterAndLayer(path, obj.diagrams[j].objects, repeaterId);
  106. } else if($ax.public.fn.IsReferenceDiagramObject(obj.type)) {
  107. mapIdsToRepeaterAndLayer(pathCopy, $ax.pageData.masters[obj.masterId].diagram.objects, repeaterId);
  108. } else if($ax.public.fn.IsRepeater(obj.type)) {
  109. mapScriptIdToRepeaterId(scriptId, scriptId);
  110. mapIdsToRepeaterAndLayer(path, obj.objects, scriptId);
  111. } else if($ax.public.fn.IsLayer(obj.type)) {
  112. var layerObjs = obj.objs;
  113. for(var j = 0; j < layerObjs.length; j++) {
  114. mapScriptIdToLayerId(layerObjs[j], scriptId, path);
  115. }
  116. mapIdsToRepeaterAndLayer(path, layerObjs, repeaterId);
  117. } else if(obj.objects && obj.objects.length) {
  118. if(repeaterId) {
  119. for(var j = 0; j < obj.objects.length; j++) {
  120. mapIdsToRepeaterAndLayer(path, obj.objects, repeaterId);
  121. }
  122. }
  123. }
  124. }
  125. };
  126. mapIdsToRepeaterAndLayer([], $ax.pageData.page.diagram.objects);
  127. };
  128. $ax.getPathFromScriptId = function(scriptId) {
  129. var reversedPath = [];
  130. var path = scriptIdToPath[scriptId];
  131. while(path && path.uniqueId) {
  132. reversedPath[reversedPath.length] = path.uniqueId;
  133. path = path.parent;
  134. }
  135. return reversedPath.reverse();
  136. };
  137. var _getScriptIdFromFullPath = function(path) {
  138. var current = $ax.pageData.objectPaths;
  139. for(var i = 0; i < path.length; i++) {
  140. current = current[path[i]];
  141. if(!current) return current;
  142. }
  143. return current && current.scriptId;
  144. };
  145. var _getScriptIdFromPath = function(path, relativeTo, includeLimbo) {
  146. var relativePath = [];
  147. var includeMasterInPath = false;
  148. if(relativeTo) {
  149. var relativeToScriptId;
  150. if(relativeTo.srcElement) { //this is eventInfo
  151. relativeToScriptId = $ax.repeater.getScriptIdFromElementId(relativeTo.srcElement);
  152. includeMasterInPath = relativeTo.isMasterEvent;
  153. } else if(typeof relativeTo === 'string') { //this is an element id
  154. relativeToScriptId = relativeTo;
  155. }
  156. if(relativeToScriptId) {
  157. relativePath = $ax.getPathFromScriptId(relativeToScriptId);
  158. if(!includeMasterInPath) relativePath = relativePath.slice(0, relativePath.length - 1);
  159. } else if(relativeTo instanceof Array) { //this is a path
  160. relativePath = relativeTo;
  161. }
  162. }
  163. var fullPath = relativePath.concat(path);
  164. var scriptId = _getScriptIdFromFullPath(fullPath);
  165. return (includeLimbo || !$ax.visibility.isScriptIdLimbo(scriptId)) && scriptId;
  166. };
  167. $ax.getScriptIdFromPath = _getScriptIdFromPath;
  168. var _getElementIdsFromPath = function(path, eventInfo) {
  169. var scriptId = _getScriptIdFromPath(path, eventInfo);
  170. if(!scriptId) return [];
  171. // Don't need placed check hear. If unplaced, scriptId will be undefined and exit out before here.
  172. return $ax.getElementIdsFromEventAndScriptId(eventInfo, scriptId);
  173. };
  174. $ax.getElementIdsFromPath = _getElementIdsFromPath;
  175. var _getElementIdFromPath = function(path, params, includeLimbo) {
  176. var scriptId = _getScriptIdFromPath(path, params.relativeTo, includeLimbo);
  177. if(!scriptId) return scriptId;
  178. var itemNum = params.itemNum;
  179. if(params.relativeTo && typeof params.relativeTo === 'string') {
  180. if($jobj(params.relativeTo)) itemNum = $ax.repeater.getItemIdFromElementId(params.relativeTo);
  181. }
  182. return $ax.repeater.createElementId(scriptId, itemNum);
  183. };
  184. $ax.getElementIdFromPath = _getElementIdFromPath;
  185. var _getElementsIdFromEventAndScriptId = function(eventInfo, scriptId) {
  186. var itemId = eventInfo && $ax.repeater.getItemIdFromElementId(eventInfo.srcElement);
  187. var target = false;
  188. // Try to get itemId from target if you can't get it from source.
  189. if(!itemId) {
  190. itemId = eventInfo && eventInfo.targetElement && $ax.repeater.getItemIdFromElementId(eventInfo.targetElement);
  191. if(itemId) target = true;
  192. }
  193. var parentRepeater = $ax.getParentRepeaterFromScriptId(scriptId);
  194. if(parentRepeater && scriptId != parentRepeater) {
  195. if(itemId && (!eventInfo || parentRepeater == $ax.getParentRepeaterFromScriptId($ax.repeater.getScriptIdFromElementId(target ? eventInfo.targetElement : eventInfo.srcElement)))) {
  196. return [$ax.repeater.createElementId(scriptId, itemId)];
  197. }
  198. var elementIds = [];
  199. var itemIds = $ax.getItemIdsForRepeater(parentRepeater);
  200. if(!itemIds) return [];
  201. for(var i = 0; i < itemIds.length; i++) elementIds[i] = $ax.repeater.createElementId(scriptId, itemIds[i]);
  202. return elementIds;
  203. }
  204. return [scriptId];
  205. };
  206. $ax.getElementIdsFromEventAndScriptId = _getElementsIdFromEventAndScriptId;
  207. var _getSrcElementIdFromEvent = function(event) {
  208. var currentQuery = $(event.srcElement || event.target);
  209. while(currentQuery && currentQuery.length && (!$obj(currentQuery.attr('id')) || $jobj(currentQuery.attr('id')).hasClass('text'))) {
  210. currentQuery = currentQuery.parent();
  211. };
  212. return currentQuery.attr('id');
  213. };
  214. $ax.getSrcElementIdFromEvent = _getSrcElementIdFromEvent;
  215. var _getEventInfoFromEvent = function(event, skipShowDescriptions, elementId) {
  216. var eventInfo = {};
  217. eventInfo.srcElement = elementId;
  218. eventInfo.now = new Date();
  219. if(event != null) {
  220. //elementId can be empty string, so can't simple use "or" assignment here.
  221. eventInfo.srcElement = elementId || elementId == '' ? elementId : _getSrcElementIdFromEvent(event);
  222. eventInfo.which = event.which;
  223. // When getting locations in mobile, need to extract the touch object to get the mouse location attributes
  224. var mouseEvent = (event.originalEvent && event.originalEvent.changedTouches && event.originalEvent.changedTouches[0]) || event.originalEvent;
  225. if(mouseEvent && !mouseEvent.type) mouseEvent.type = event.type;
  226. if(skipShowDescriptions) eventInfo.skipShowDescriptions = true;
  227. // Always update mouse location if possible
  228. $ax.event.updateMouseLocation(mouseEvent);
  229. }
  230. // Always set event info about cursor
  231. var _cursor = eventInfo.cursor = {};
  232. _cursor.x = $ax.mouseLocation.x;
  233. _cursor.y = $ax.mouseLocation.y;
  234. var body = $('body');
  235. if(body.css('position') == 'relative') {
  236. _cursor.x -= ($ax.getNumFromPx(body.css('left')) + Math.max(0, ($(window).width() - body.width()) / 2));
  237. }
  238. eventInfo.pageX = _cursor.x + 'px';
  239. eventInfo.pageY = _cursor.y + 'px';
  240. // Do Keyboard Info
  241. eventInfo.keyInfo = $ax.event.keyState();
  242. eventInfo.window = $ax.getWindowInfo();
  243. eventInfo.thiswidget = _getWidgetInfo(eventInfo.srcElement);
  244. eventInfo.item = _getItemInfo(eventInfo.srcElement);
  245. eventInfo.dragInfo = $ax.drag.GetWidgetDragInfo();
  246. return eventInfo;
  247. };
  248. $ax.getEventInfoFromEvent = _getEventInfoFromEvent;
  249. $ax.getBasicEventInfo = function() {
  250. var eventInfo = {};
  251. eventInfo.now = new Date();
  252. eventInfo.window = $ax.getWindowInfo();
  253. eventInfo.cursor = { x: 0, y: 0};
  254. return eventInfo;
  255. };
  256. //var _getWindowInfo = function() {
  257. // var win = {};
  258. // win.width = $(window).width();
  259. // win.height = $(window).height();
  260. // win.scrollx = $(window).scrollLeft();
  261. // win.scrolly = $(window).scrollTop();
  262. // return win;
  263. //};
  264. //$ax.getWindowInfo = _getWindowInfo;
  265. var repeaterInfoCache = [];
  266. $ax.cacheRepeaterInfo = function(repeaterId, repeaterInfo) {
  267. repeaterInfoCache[repeaterId] = repeaterInfo;
  268. }
  269. $ax.removeCachedRepeaterInfo = function(repeaterId) {
  270. repeaterInfoCache[repeaterId] = undefined;
  271. }
  272. var _getItemInfo = function(elementId) {
  273. if(!elementId) return { valid: false };
  274. elementId = _getParentElement(elementId);
  275. var index = $ax.repeater.getItemIdFromElementId(elementId);
  276. if(!index) return { valid: false };
  277. var item = { valid: true };
  278. var scriptId = $ax.repeater.getScriptIdFromElementId(elementId);
  279. var repeaterId = $ax.getParentRepeaterFromScriptId(scriptId);
  280. item.repeater = repeaterInfoCache[repeaterId] ? repeaterInfoCache[repeaterId] : _getWidgetInfo(repeaterId);
  281. $ax.repeater.setDisplayProps(item, repeaterId, index);
  282. item.ismarked = $ax.repeater.isEditItem(repeaterId, index);
  283. item.isvisible = Boolean($jobj(elementId).length);
  284. return item;
  285. };
  286. $ax.getItemInfo = _getItemInfo;
  287. var _getWidgetInfo = function(elementId) {
  288. if(!elementId) return { valid: false };
  289. elementId = _getParentElement(elementId);
  290. //var elementAxQuery = $ax('#' + elementId);
  291. var elementQuery = $jobj(elementId);
  292. var obj = $obj(elementId);
  293. var widget = { valid: true, isWidget: true, obj: obj, elementQuery: elementQuery, isLayer: $ax.public.fn.IsLayer(obj.type) };
  294. widget.elementId = elementId;
  295. widget.name = widget.label = (elementQuery.data('label') ? elementQuery.data('label') : '');
  296. //widget.text = $ax('#' + elementId).text();
  297. widget.opacity = Number(elementQuery.css('opacity')) * 100;
  298. //widget.rotation = $ax.move.getRotationDegree(widget.elementId);
  299. var scriptId = $ax.repeater.getScriptIdFromElementId(elementId);
  300. var repeaterId = $ax.getParentRepeaterFromScriptId(scriptId);
  301. if(repeaterId) widget.repeater = $ax.public.fn.IsRepeater(obj.type) ? widget : _getWidgetInfo(repeaterId);
  302. // Right now only dynamic panel can scroll
  303. if($ax.public.fn.IsDynamicPanel(obj.type)) {
  304. var stateId = $ax.visibility.GetPanelState(elementId);
  305. //can be empty when refreshing repeater and applying filter
  306. if(stateId) {
  307. var stateQuery = $('#' + stateId);
  308. widget.scrollx = stateQuery.scrollLeft();
  309. widget.scrolly = stateQuery.scrollTop();
  310. //widget.stateQuery = stateQuery;
  311. }
  312. } else {
  313. widget.scrollx = 0;
  314. widget.scrolly = 0;
  315. }
  316. // repeater only props
  317. if($ax.public.fn.IsRepeater(obj.type)) {
  318. widget.visibleitemcount = repeaterIdToItemIds[scriptId] ? repeaterIdToItemIds[scriptId].length : $ax.repeater.getVisibleDataCount(scriptId);
  319. widget.itemcount = $ax.repeater.getFilteredDataCount(scriptId);
  320. widget.datacount = $ax.repeater.getDataCount(scriptId);
  321. widget.pagecount = $ax.repeater.getPageCount(scriptId);
  322. widget.pageindex = $ax.repeater.getPageIndex(scriptId);
  323. }
  324. // Get widget info funcs
  325. //widget.elementAxQuery = function () {
  326. // return this.elementAxQueryProp || (this.elementAxQueryProp = $ax('#' + this.elementId));
  327. //}
  328. //widget.isFitToContent = function () {
  329. // if (this.isFitToContentProp === undefined) {
  330. // if (!this.stateQuery) this.isFitToContentProp = false;
  331. // else this.isFitToContentProp = $ax.dynamicPanelManager.isIdFitToContent(this.elementId);
  332. // }
  333. // return this.isFitToContentProp;
  334. //}
  335. widget.x = function () { return this.getProp('x'); }
  336. widget.y = function () { return this.getProp('y'); }
  337. widget.pagex = function () { return this.getProp('pagex'); }
  338. widget.pagey = function () { return this.getProp('pagey'); }
  339. widget.width = function () { return this.getProp('width'); }
  340. widget.height = function () { return this.getProp('height'); }
  341. widget.left = function () { return this.x(); }
  342. widget.top = function () { return this.y(); }
  343. widget.right = function () { return this.x() + this.width(); }
  344. widget.bottom = function () { return this.y() + this.height(); }
  345. widget.rotation = function () { return this.getProp('rotation'); }
  346. widget.text = function () { return this.getProp('text'); }
  347. widget.getProp = function (prop) {
  348. var propName = prop + 'Prop';
  349. if (typeof (this[propName]) != 'undefined') return this[propName];
  350. return this[propName] = this.cacheProp(prop);
  351. };
  352. widget.cacheProp = function (prop) {
  353. if(prop == 'x' || prop == 'y' || prop == 'width' || prop == 'height') {
  354. var boundingRect = $ax('#' + this.elementId).offsetBoundingRect(true);
  355. this.xProp = boundingRect.left;
  356. this.yProp = boundingRect.top;
  357. this.widthProp = boundingRect.width;
  358. this.heightProp = boundingRect.height;
  359. }
  360. if(prop == 'pagex' || prop == 'pagey') {
  361. var viewportLocation = $ax('#' + this.elementId).viewportLocation();
  362. this.pagexProp = viewportLocation.left;
  363. this.pageyProp = viewportLocation.top;
  364. }
  365. if(prop == 'rotation') {
  366. this.rotationProp = $ax.move.getRotationDegree(this.elementId);
  367. }
  368. if (prop == 'text') {
  369. this.textProp = $ax('#' + this.elementId).text();
  370. }
  371. return this[prop + 'Prop'];
  372. //// I'm keeping the returned undefineds the same as before, but really I could probably return undefined right away if elementQuery is empty
  373. //if (this.isLayer) {
  374. // if (prop == 'pagex' || prop == 'pagey') {
  375. // if (this.elementQuery.length > 0) {
  376. // if (prop == 'pagex') return this.elementAxQuery().left();
  377. // else return this.elementAxQuery().top();
  378. // }
  379. // return undefined; // Otherwise, it is undefined as there is no element
  380. // }
  381. // var boundingRect = $ax.public.fn.getWidgetBoundingRect(this.elementId);
  382. // this.xProp = boundingRect.left;
  383. // this.yProp = boundingRect.top;
  384. // this.widthProp = boundingRect.width;
  385. // this.heightProp = boundingRect.height;
  386. // return this[prop + 'Prop'];
  387. //}
  388. //if (this.elementQuery.length <= 0) return prop == 'x' || prop == 'y' ? 0 : undefined;
  389. //switch (prop) {
  390. // case 'x': return this.elementAxQuery().locRelativeIgnoreLayer(false);
  391. // case 'y': return this.elementAxQuery().locRelativeIgnoreLayer(true);
  392. // case 'pagex': return this.elementAxQuery().left();
  393. // case 'pagey': return this.elementAxQuery().top();
  394. //}
  395. //var val = this.elementAxQuery()[prop]();
  396. //if (this.isFitToContent()) val = this.stateQuery[prop]();
  397. //return val;
  398. };
  399. //widget.leftfixed = function() { this.getFixed('left'); }
  400. //widget.topfixed = function() { this.getFixed('top'); }
  401. //widget.rightfixed = function() { this.getFixed('right'); }
  402. //widget.bottomfixed = function() { this.getFixed('bottom'); }
  403. //widget.isFixed = function() {
  404. // if(this.isFixedProp === undefined) this.isFixedProp = this.elementQuery.css('position') == 'fixed)';
  405. // return this.isFixedProp;
  406. //}
  407. //widget.getFixed = function (prop) {
  408. // var fixed = prop + 'fixedProp';
  409. // if(!this.isFixed()) widget[fixed] = widget[prop]();
  410. // if(widget[fixed] === undefined) {
  411. // if(prop == 'left' || prop == 'right') {
  412. // if(this.windowScrollX === undefined) this.windowScrollX = $(window).scrollLeft();
  413. // var windowScroll = this.windowScrollX;
  414. // } else {
  415. // if(this.windowScrollY === undefined) this.windowScrollY = $(window).scrollTop();
  416. // windowScroll = this.windowScrollY;
  417. // }
  418. // widget[fixed] = widget[prop]() - windowScroll;
  419. // }
  420. // return widget[fixed];
  421. //}
  422. return widget;
  423. };
  424. $ax.getWidgetInfo = _getWidgetInfo;
  425. $ax.GetTextPanelId = function (id, create) {
  426. if(!$ax('#' + id).SupportsRichText()) return '';
  427. var buttonShape = $ax.GetButtonShape(id);
  428. var panelDiv = buttonShape.find('.text')[0];
  429. if(!panelDiv) {
  430. if(!create) return "";
  431. var adaptiveId = $ax.adaptive.currentViewId;
  432. var newId = id + "_text";
  433. //var newDiv = $('<div id="' + newId + '" class="text" style="visibility: inherit; position: absolute"></div>');
  434. var newDiv = $('<div id="' + newId + '" class="text' + (adaptiveId ? (' ' + adaptiveId) : '') + '" style="visibility: inherit; position: absolute"><p><span></span></p></div>');
  435. buttonShape.append(newDiv);
  436. $ax.style.setAdaptiveStyle(id, $ax.style.computeAllOverrides(id, undefined, $ax.style.generateState(id), adaptiveId));
  437. panelDiv = newDiv[0];
  438. }
  439. return panelDiv.id;
  440. }
  441. $ax.GetParentIdFromLink = function(id) {
  442. return $ax.GetShapeIdFromText($jobj(id).parentsUntil('.text').parent().attr('id'));
  443. };
  444. $ax.GetButtonShapeId = function(id) {
  445. var obj = $obj(id);
  446. switch(obj.type) {
  447. case $ax.constants.TREE_NODE_OBJECT_TYPE:
  448. return obj.buttonShapeId ? $ax.getElementIdFromPath([obj.buttonShapeId], { relativeTo: id }) : "";
  449. case $ax.constants.LINK_TYPE:
  450. return "";
  451. default:
  452. return id;
  453. }
  454. };
  455. $ax.GetButtonShape = function(id) {
  456. return $jobj($ax.GetButtonShapeId(id));
  457. };
  458. $ax.GetShapeIdFromText = function(id) {
  459. if(!id) return undefined; // this is to prevent an infinite loop.
  460. var current = document.getElementById(id);
  461. if(!current) return undefined;
  462. current = current.parentElement;
  463. while(current && current.tagName != 'BODY') {
  464. var currentId = current.id;
  465. if(currentId && currentId != 'base') return $ax.visibility.getWidgetFromContainer(currentId);
  466. current = current.parentElement;
  467. }
  468. return undefined;
  469. };
  470. $ax.GetImageIdFromShape = function(id) {
  471. var image = $ax.GetButtonShape(id).find('img[id$=img]');
  472. if(!image.length) image = $jobj(id).find('img[id$=image_sketch]');
  473. return image.attr('id');
  474. };
  475. var _getParentElement = $ax.getParentElement = function(elementId) {
  476. var obj = $obj(elementId);
  477. while(obj.isContained) {
  478. var path = $ax.getPathFromScriptId($ax.repeater.getScriptIdFromElementId(elementId));
  479. var itemId = $ax.repeater.getItemIdFromElementId(elementId);
  480. path[path.length - 1] = obj.parent.id;
  481. elementId = $ax.getElementIdFromPath(path, { itemNum: itemId });
  482. obj = $obj(elementId);
  483. }
  484. return elementId;
  485. };
  486. $ax.addItemIdToRepeater = function(itemId, repeaterId) {
  487. var itemIds = repeaterIdToItemIds[repeaterId];
  488. if(itemIds) itemIds[itemIds.length] = itemId;
  489. else repeaterIdToItemIds[repeaterId] = [itemId];
  490. var scriptIds = repeaterIdToScriptIds[repeaterId];
  491. for(var i = 0; i < scriptIds.length; i++) elementIdToObject[$ax.repeater.createElementId(scriptIds[i], itemId)] = $ax.getObjectFromScriptId(scriptIds[i]);
  492. };
  493. $ax.getAllElementIds = function() {
  494. var elementIds = [];
  495. for(var i = 0; i < _scriptIds.length; i++) {
  496. var scriptId = _scriptIds[i];
  497. var repeaterId = scriptIdToRepeaterId[scriptId];
  498. if(repeaterId && repeaterId != scriptId) {
  499. var itemIds = repeaterIdToItemIds[repeaterId] || [];
  500. for(var j = 0; j < itemIds.length; j++) elementIds[elementIds.length] = $ax.repeater.createElementId(scriptId, itemIds[j]);
  501. } else elementIds[elementIds.length] = scriptId;
  502. }
  503. return elementIds;
  504. };
  505. $ax.getAllScriptIds = function() {
  506. return _scriptIds;
  507. };
  508. $ax.getObjectFromElementId = function(elementId) {
  509. return $ax.getObjectFromScriptId($ax.repeater.getScriptIdFromElementId(elementId));
  510. };
  511. $ax.getObjectFromScriptId = function(scriptId) {
  512. return scriptIdToObject[scriptId];
  513. };
  514. $ax.getParentRepeaterFromElementId = function(elementId) {
  515. return $ax.getParentRepeaterFromScriptId($ax.repeater.getScriptIdFromElementId(elementId));
  516. };
  517. $ax.getParentRepeaterFromElementIdExcludeSelf = function (elementId) {
  518. var repeaterId = $ax.getParentRepeaterFromElementId(elementId);
  519. return repeaterId != elementId ? repeaterId : undefined;
  520. };
  521. $ax.getParentRepeaterFromScriptId = function(scriptId) {
  522. return scriptIdToRepeaterId[scriptId];
  523. };
  524. var _getChildScriptIdsForRepeater = function(repeaterId) {
  525. return repeaterIdToScriptIds[repeaterId];
  526. };
  527. var _getItemIdsForRepeater = function(repeaterId) {
  528. return repeaterIdToItemIds[repeaterId] || [];
  529. };
  530. $ax.getItemIdsForRepeater = _getItemIdsForRepeater;
  531. var _clearItemIdsForRepeater = function(repeaterId) {
  532. repeaterIdToItemIds[repeaterId] = [];
  533. };
  534. $ax.clearItemsForRepeater = _clearItemIdsForRepeater;
  535. $ax.getChildElementIdsForRepeater = function(repeaterId) {
  536. var scriptIds = _getChildScriptIdsForRepeater(repeaterId);
  537. var itemIds = _getItemIdsForRepeater(repeaterId);
  538. var retVal = [];
  539. if(!itemIds || !scriptIds) return retVal;
  540. for(var i = 0; i < scriptIds.length; i++) {
  541. for(var j = 0; j < itemIds.length; j++) {
  542. retVal[retVal.length] = $ax.repeater.createElementId(scriptIds[i], itemIds[j]);
  543. }
  544. }
  545. return retVal;
  546. };
  547. $ax.getRdoParentFromElementId = function(elementId) {
  548. var scriptId = $ax.repeater.getScriptIdFromElementId(elementId);
  549. var rdoId = scriptIdToPath[scriptId].parent.scriptId;
  550. if($ax.getParentRepeaterFromScriptId(rdoId)) rdoId = $ax.repeater.createElementId(rdoId, $ax.repeater.getItemIdFromElementId(elementId));
  551. return rdoId;
  552. };
  553. $ax.getLayerParentFromElementId = function (elementId) {
  554. var itemId = $ax.repeater.getItemIdFromElementId(elementId);
  555. var scriptId = scriptIdToParentLayer[$ax.repeater.getScriptIdFromElementId(elementId)];
  556. return $ax.getParentRepeaterFromElementId(scriptId) ? $ax.repeater.createElementId(scriptId, itemId) : scriptId;
  557. }
  558. $ax.updateElementText = function(elementId, text) {
  559. elementIdToText[elementId] = text;
  560. };
  561. $ax.hasElementTextChanged = function(elementId, text) {
  562. return elementIdToText[elementId] != text;
  563. };
  564. $ax.updateRadioButtonSelected = function(group, elementId) {
  565. var old = radioGroupToSelectedElementId[group];
  566. radioGroupToSelectedElementId[group] = elementId;
  567. return old;
  568. };
  569. $ax.hasRadioButtonSelectedChanged = function(group, elementId) {
  570. return radioGroupToSelectedElementId[group] != elementId;
  571. };
  572. })();
  573. //Recursively populates fullPathArray with:
  574. // [ { idPath, scriptId }, ... ]
  575. //for every scriptId in the object
  576. //also populates an object of scriptId -> path
  577. var _pathToScriptIdHelper = function(currentPath, currentChain, fullPathArray, scriptIdToPath) {
  578. for(var key in currentPath) {
  579. if(key != "scriptId") {
  580. var nextPath = currentPath[key];
  581. _pathToScriptIdHelper(nextPath, currentChain.concat(key), fullPathArray, scriptIdToPath);
  582. nextPath.parent = currentPath;
  583. nextPath.uniqueId = key;
  584. } else {
  585. fullPathArray[fullPathArray.length] = { idPath: currentChain, scriptId: currentPath.scriptId };
  586. scriptIdToPath[currentPath.scriptId] = currentPath;
  587. }
  588. }
  589. };
  590. $ax.public.loadCurrentPage = $ax.loadCurrentPage = function(pageData) {
  591. $ax.pageData = _pageData = pageData;
  592. _initializePageData();
  593. };
  594. $ax.public.loadDocument = $ax.loadDocument = function(document) {
  595. $ax.document = document;
  596. _initializeDocumentData();
  597. };
  598. /**
  599. Navigates to a page
  600. */
  601. $ax.public.navigate = $ax.navigate = function(to) { //url, includeVariables, type) {
  602. var targetUrl;
  603. if(typeof (to) === 'object') {
  604. targetUrl = !to.includeVariables ? to.url : $ax.globalVariableProvider.getLinkUrl(to.url, to.useGlobalVarNameInUrl);
  605. if(to.target == "new") {
  606. window.open(targetUrl, "");
  607. } else if(to.target == "popup") {
  608. var features = _getPopupFeatures(to.popupOptions);
  609. window.open(targetUrl, "", features);
  610. } else {
  611. var targetLocation = window.location;
  612. if(to.target == "current") {
  613. } else if(to.target == "parent") {
  614. if(!top.opener) return;
  615. targetLocation = top.opener.window.location;
  616. } else if(to.target == "parentFrame") {
  617. targetLocation = parent.location;
  618. } else if(to.target == "frame") {
  619. // targetLocation = to.frame.contentWindow.location;
  620. $(to.frame).attr('src', targetUrl || 'about:blank');
  621. return;
  622. }
  623. if (!_needsReload(targetLocation, to.url)) {
  624. targetLocation.href = targetUrl || 'about:blank';
  625. } else {
  626. targetLocation.href = $axure.utils.getReloadPath() + "#" + encodeURI(targetUrl);
  627. }
  628. }
  629. } else {
  630. $ax.navigate({
  631. url: to,
  632. target: "current",
  633. includeVariables: arguments[1]
  634. });
  635. }
  636. };
  637. var _needsReload = function(oldLocation, newBaseUrl) {
  638. var reload = false;
  639. try {
  640. var oldUrl = oldLocation.href;
  641. var oldBaseUrl = oldUrl.split("#")[0];
  642. var lastslash = oldBaseUrl.lastIndexOf("/");
  643. if(lastslash > 0) {
  644. oldBaseUrl = oldBaseUrl.substring(lastslash + 1, oldBaseUrl.length);
  645. if(oldBaseUrl == encodeURI(newBaseUrl)) {
  646. reload = true;
  647. }
  648. }
  649. } catch(e) {
  650. }
  651. return reload;
  652. };
  653. var _getPopupFeatures = function(options) {
  654. var defaultOptions = {
  655. toolbar: true,
  656. scrollbars: true,
  657. location: true,
  658. status: true,
  659. menubar: true,
  660. directories: true,
  661. resizable: true,
  662. centerwindow: true,
  663. left: -1,
  664. top: -1,
  665. height: -1,
  666. width: -1
  667. };
  668. var selectedOptions = $.extend({}, defaultOptions, options);
  669. var optionsList = [];
  670. optionsList.push('toolbar=' + (selectedOptions.toolbar ? 'yes' : 'no'));
  671. optionsList.push('scrollbars=' + (selectedOptions.scrollbars ? 'yes' : 'no'));
  672. optionsList.push('location=' + (selectedOptions.location ? 'yes' : 'no'));
  673. optionsList.push('status=' + (selectedOptions.status ? 'yes' : 'no'));
  674. optionsList.push('menubar=' + (selectedOptions.menubar ? 'yes' : 'no'));
  675. optionsList.push('directories=' + (selectedOptions.directories ? 'yes' : 'no'));
  676. optionsList.push('resizable=' + (selectedOptions.resizable ? 'yes' : 'no'));
  677. if(selectedOptions.centerwindow == false) {
  678. if(selectedOptions.left > -1) {
  679. optionsList.push('left=' + selectedOptions.left);
  680. }
  681. if(selectedOptions.top > -1) {
  682. optionsList.push('top=' + selectedOptions.top);
  683. }
  684. }
  685. var height = 0;
  686. var width = 0;
  687. if(selectedOptions.height > 0) {
  688. optionsList.push('height=' + selectedOptions.height);
  689. height = selectedOptions.height;
  690. }
  691. if(selectedOptions.width > 0) {
  692. optionsList.push('width=' + selectedOptions.width);
  693. width = selectedOptions.width;
  694. }
  695. var features = optionsList.join(',');
  696. if(selectedOptions.centerwindow) {
  697. var winl = (window.screen.width - width) / 2;
  698. var wint = (window.screen.height - height) / 2;
  699. features = features + ',left=' + winl + ',top=' + wint;
  700. }
  701. return features;
  702. };
  703. /**
  704. Closes a window
  705. */
  706. $ax.public.closeWindow = $ax.closeWindow = function() {
  707. parent.window.close();
  708. };
  709. /**
  710. Goes back
  711. */
  712. $ax.public.back = $ax.back = function() {
  713. window.history.go(-1);
  714. };
  715. /**
  716. Reloads the current page.
  717. # includeVariables: true if it should re-include the variables when the page is reloaded
  718. */
  719. $ax.public.reload = $ax.reload = function(includeVariables) {
  720. var targetUrl = (includeVariables === false)
  721. ? $axure.utils.getReloadPath() + "#" + encodeURI($ax.pageData.url)
  722. : $axure.utils.getReloadPath() + "#" + encodeURI($ax.globalVariableProvider.getLinkUrl($ax.pageData.url));
  723. window.location.href = targetUrl;
  724. };
  725. /**
  726. Sets a variable.
  727. # name: The name of the global variable to set
  728. # value: The value that should be set
  729. */
  730. $ax.public.setGlobalVariable = $ax.setGlobalVariable = function(name, value) {
  731. if(!name || !value) {
  732. return;
  733. }
  734. $ax.globalVariableProvider.setVariableValue(name, value);
  735. };
  736. /**
  737. Gets the value of a global variable
  738. # name: The name of the global variable value to get
  739. */
  740. $ax.public.getGlobalVariable = $ax.getGlobalVariable = function(name) {
  741. $ax.globalVariableProvider.getVariableValue(name);
  742. };
  743. $ax.getObjectFromElementIdDisregardHex = function (elementId) {
  744. var elementIdInput = elementId.charAt(0) == '#' ? elementId.substring(1) : elementId;
  745. return this.getObjectFromElementId(elementIdInput);
  746. }
  747. $ax.getTypeFromElementId = function(elementId) {
  748. var obj = this.getObjectFromElementIdDisregardHex(elementId);
  749. return obj && obj.type;
  750. };
  751. $ax.getNumFromPx = function(pxNum) {
  752. return Number(pxNum.replace('px', ''));
  753. }
  754. });