repeater.js.orig 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. 
  2. // ******* Repeater MANAGER ******** //
  3. $axure.internal(function($ax) {
  4. var _repeaterManager = {};
  5. $ax.repeater = _repeaterManager;
  6. //This is a mapping of current editItems
  7. var repeaterToEditItems = {};
  8. //This is a mapping of current filters
  9. var repeaterToFilters = {};
  10. // This is a mapping of current sorts
  11. var repeaterToSorts = {};
  12. // This is a mapping of repeater page info
  13. var repeaterToPageInfo = {};
  14. //Hopefully this can be simplified, but for now I think 3 are needed.
  15. //This is the data set that is owned by this repeater. The repeater may or may not reference this data set, and others can reference it.
  16. var repeaterToLocalDataSet = {};
  17. //This is the data set referenced by the repeater. It is not a copy of the local data set, but a reference to a local data set (or eventually a global data set could be referenced).
  18. var repeaterToCurrentDataSet = {};
  19. //This is a copy of the current data set, that is replaced whenever a set or refresh is done.
  20. var repeaterToActiveDataSet = {};
  21. var _loadRepeaters = function() {
  22. $ax(function(obj) {
  23. return obj.type == 'repeater';
  24. }).each(function(obj, repeaterId) {
  25. repeaterToLocalDataSet[repeaterId] = $ax.deepCopy(obj.data);
  26. repeaterToLocalDataSet[repeaterId].props = obj.dataProps;
  27. repeaterToEditItems[repeaterId] = [];
  28. _initPageInfo(obj, repeaterId);
  29. _setRepeaterDataSet(repeaterId, repeaterId);
  30. });
  31. };
  32. _repeaterManager.load = _loadRepeaters;
  33. var _loaded = {};
  34. var _initRepeaters = function() {
  35. $ax(function(obj, repeaterId) {
  36. return obj.type == 'repeater' && !_loaded[repeaterId];
  37. }).each(function(obj, repeaterId) {
  38. _refreshRepeater(repeaterId);
  39. });
  40. };
  41. _repeaterManager.init = _initRepeaters;
  42. var repeatersHaveNewDataSet = [];
  43. var _setRepeaterDataSet = function(repeaterId, dataSetId) {
  44. //TODO: No idea about how global data sets will be handled...
  45. repeaterToCurrentDataSet[repeaterId] = repeaterToLocalDataSet[dataSetId];
  46. repeaterToActiveDataSet[repeaterId] = repeaterToCurrentDataSet[repeaterId];
  47. repeaterToFilters[repeaterId] = [];
  48. repeaterToSorts[repeaterId] = [];
  49. // Not using this currently
  50. // if(repeatersHaveNewDataSet.indexOf(repeaterId) == -1) repeatersHaveNewDataSet[repeatersHaveNewDataSet.length] = repeaterId;
  51. };
  52. _repeaterManager.setDataSet = _setRepeaterDataSet;
  53. var _refreshRepeater = function(repeaterId, eventInfo) {
  54. _loaded[repeaterId] = true;
  55. $ax.action.refreshStart(repeaterId);
  56. $ax.style.ClearCacheForRepeater(repeaterId);
  57. if($ax.visibility.limboIds[repeaterId]) {
  58. removeItems(repeaterId);
  59. $ax.dynamicPanelManager.fitParentPanel(repeaterId);
  60. return;
  61. }
  62. var path = $ax.getPathFromScriptId(repeaterId);
  63. path.pop();
  64. if(eventInfo) {
  65. eventInfo = $ax.eventCopy(eventInfo);
  66. }
  67. var obj = $ax.getObjectFromScriptId(repeaterId);
  68. var html = $('#' + repeaterId + '_script').html();
  69. // var container = $('<div></div>');
  70. // container.html(html);
  71. // container.attr('id', '' + repeaterId + '_container');
  72. // container.css({ position: 'absolute' });
  73. // container.offset({ left: -obj.x, top: -obj.y });
  74. var div = $('<div></div>');
  75. div.html(html);
  76. var top = 0;
  77. var left = 0;
  78. //If there is no wrap, then set it to be above the number of rows
  79. var propMap = obj.repeaterPropMap;
  80. var viewId = $ax.adaptive.currentViewId || '';
  81. var wrap = _getAdaptiveProp(propMap, 'wrap', viewId);
  82. var vertical = _getAdaptiveProp(propMap, 'vertical', viewId);
  83. var offset = propMap[viewId];
  84. var xOffset = offset.width + _getAdaptiveProp(propMap, 'horizontalSpacing', viewId);
  85. var yOffset = offset.height + _getAdaptiveProp(propMap, 'verticalSpacing', viewId);
  86. div.css({
  87. width: xOffset,
  88. height: yOffset
  89. });
  90. var background = _getAdaptiveProp(propMap, 'backColor', viewId);
  91. _applyColorCss(background, div);
  92. var hasAltColor = _getAdaptiveProp(propMap, 'hasAltColor', viewId);
  93. var altDiv = div;
  94. if(hasAltColor) altDiv = _applyColorCss(hasAltColor ? _getAdaptiveProp(propMap, 'altColor', viewId) : background, div.clone());
  95. var orderedIds = getOrderedIds(repeaterId, eventInfo);
  96. // Hide repeater, if shown, while updating.
  97. var shown = $ax.visibility.IsIdVisible(repeaterId);
  98. if(shown) document.getElementById(repeaterId).style.visibility = 'hidden';
  99. var start = 0;
  100. var end = orderedIds.length;
  101. var pageInfo = repeaterToPageInfo[repeaterId];
  102. if(!pageInfo.noLimit) {
  103. end = pageInfo.itemsPerPage * pageInfo.currPage;
  104. start = end - pageInfo.itemsPerPage;
  105. // If past the end, move to last page
  106. if(start >= orderedIds.length) {
  107. pageInfo.currPage = Math.floor((orderedIds.length - 1) / pageInfo.itemsPerPage) + 1;
  108. if(pageInfo.currPage <= 0) pageInfo.currPage = 1;
  109. end = pageInfo.itemsPerPage * pageInfo.currPage;
  110. start = end - pageInfo.itemsPerPage;
  111. }
  112. end = Math.min(end, orderedIds.length);
  113. }
  114. var useAlt = false;
  115. var resized = $ax.getItemIdsForRepeater(repeaterId).length != (end - start);
  116. //clean up old items as late as possible
  117. removeItems(repeaterId);
  118. var i = 0;
  119. var ids = [];
  120. for(var pos = start; pos < end; pos++) {
  121. var itemId = orderedIds[pos];
  122. var itemElementId = _createElementId(repeaterId, itemId);
  123. $ax.addItemIdToRepeater(itemId, repeaterId);
  124. ids.push(itemElementId);
  125. var processId = function(full, prop, id, suffix) {
  126. var elementId = _createElementId('u' + id, itemId);
  127. //If there is a suffix (ex. _img), then don't push the id.
  128. if(!suffix) ids.push(elementId);
  129. return prop + '="' + elementId + '"';
  130. };
  131. var copy = (useAlt ? altDiv : div).clone();
  132. useAlt = !useAlt;
  133. copy.attr('id', itemElementId);
  134. copy.html(div.html().replace(/(id|for)="?u([0-9]+(_[_a-z0-9]*)?)"?/g, processId));
  135. copy.css({
  136. 'position': 'absolute',
  137. 'top': top + 'px',
  138. 'left': left + 'px',
  139. 'width': obj.width + 'px',
  140. 'height': obj.height + 'px'
  141. });
  142. $('#' + repeaterId).append(copy);
  143. i++;
  144. if(wrap != -1 && i % wrap == 0) {
  145. if(vertical) {
  146. top = 0;
  147. left += xOffset;
  148. } else {
  149. left = 0;
  150. top += yOffset;
  151. }
  152. } else if(vertical) top += yOffset;
  153. else left += xOffset;
  154. }
  155. var query = $ax(function(diagramObject, elementId) {
  156. // All objects with the repeater as their parent, except the repeater itself.
  157. var scriptId = _getScriptIdFromElementId(elementId);
  158. return $ax.getParentRepeaterFromScriptId(scriptId) == repeaterId && scriptId != repeaterId;
  159. });
  160. if(viewId) $ax.adaptive.applyView(viewId, query);
  161. else {
  162. var limbo = {};
  163. var hidden = {};
  164. query.each(function(diagramObject, elementId) {
  165. // sigh, javascript. we need the === here because undefined means not overriden
  166. if(diagramObject.style.visible === false) hidden[elementId] = true;
  167. //todo: **mas** check if the limboed widgets are hidden by default by the generator
  168. if(diagramObject.style.limbo) limbo[elementId] = true;
  169. });
  170. $ax.visibility.addLimboAndHiddenIds(limbo, hidden, query);
  171. $ax.dynamicPanelManager.updatePercentPanelCache(query);
  172. }
  173. $ax.annotation.InitializeAnnotations(query);
  174. for(var index = 0; index < ids.length; index++) {
  175. var id = ids[index];
  176. var childObj = $obj(id);
  177. var childJobj = $jobj(id);
  178. var childItemId = _getItemIdFromElementId(id);
  179. if(obj.repeaterPropMap.isolateRadio && childObj.type == 'radioButton') {
  180. var input = $jobj(_applySuffixToElementId(id, '_input'));
  181. input.attr('name', _createElementId(input.attr('name'), childItemId));
  182. if($ax.ieColorManager) $ax.ieColorManager.applyBackground($ax('#' + id));
  183. }
  184. if(obj.repeaterPropMap.isolateSelection && childJobj.attr('selectiongroup')) {
  185. childJobj.attr('selectiongroup', _createElementId(childJobj.attr('selectiongroup'), childItemId));
  186. }
  187. $ax.initializeObjectEvents($ax('#' + id));
  188. $ax.dynamicPanelManager.initFitPanels($ax('#' + id));
  189. $ax.style.initializeObjectTextAlignment($ax('#' + id));
  190. }
  191. //$ax.event.raiseSyntheticEvent(itemElementId, 'onLoad', true);
  192. //$ax.loadDynamicPanelsAndMasters(obj.objects, path, itemId);
  193. // Now load
  194. for(pos = start; pos < end; pos++) {
  195. itemId = orderedIds[pos];
  196. itemElementId = _createElementId(repeaterId, itemId);
  197. $ax.event.raiseSyntheticEvent(itemElementId, 'onItemLoad', true);
  198. $ax.loadDynamicPanelsAndMasters(obj.objects, path, itemId);
  199. }
  200. // Reshow repeater if it was originally shown (load is complete by now)
  201. if(shown) document.getElementById(repeaterId).style.visibility = 'visible';
  202. $ax.dynamicPanelManager.fitParentPanel(repeaterId);
  203. // If number of items changed, you need to resize the repeater
  204. if(resized) {
  205. var count = end - start;
  206. // When getting assume not vertical first. Get how many items wide and high it is.
  207. var yCount = wrap == -1 ? 1 : Math.ceil(count / wrap);
  208. var xCount = wrap == -1 ? count : count % wrap;
  209. if(xCount == 0) xCount = wrap;
  210. // Now swap if vertical
  211. if(vertical) {
  212. var temp = xCount;
  213. xCount = yCount;
  214. yCount = temp;
  215. }
  216. // xOffset includes, the spacing, but first time that should be ignored. So just add the width/height once, and all the other times add the offset.
  217. $jobj(repeaterId).css('width', offset.width + (xCount - 1) * xOffset);
  218. $jobj(repeaterId).css('height', offset.height + (yCount - 1) * yOffset);
  219. }
  220. // Right now we assume only one refresh at a time. If we can manually trigger refreshes, that may possibly change.
  221. $ax.action.refreshEnd();
  222. };
  223. _repeaterManager.refreshRepeater = _refreshRepeater;
  224. _repeaterManager.refreshAllRepeaters = function() {
  225. $ax('*').each(function(diagramObject, elementId) {
  226. if(diagramObject.type != 'repeater') return;
  227. _initPageInfo(diagramObject, elementId);
  228. _refreshRepeater(elementId, $ax.getEventInfoFromEvent($ax.getjBrowserEvent()));
  229. });
  230. };
  231. _repeaterManager.refreshRepeaters = function(ids, eventInfo) {
  232. for(var i = 0; i < ids.length; i++) _refreshRepeater(ids[i], eventInfo);
  233. };
  234. var _initPageInfo = function(obj, elementId) {
  235. var pageInfo = {};
  236. var map = obj.repeaterPropMap;
  237. var currentViewId = $ax.adaptive.currentViewId || '';
  238. var itemsPerPage = _getAdaptiveProp(map, 'itemsPerPage', currentViewId);
  239. if(itemsPerPage == -1) pageInfo.noLimit = true;
  240. else {
  241. pageInfo.itemsPerPage = itemsPerPage;
  242. pageInfo.currPage = _getAdaptiveProp(map, 'currPage', currentViewId);
  243. }
  244. repeaterToPageInfo[elementId] = pageInfo;
  245. };
  246. var _applyColorCss = function(json, div) {
  247. var args = json.r + ', ' + json.g + ', ' + json.b;
  248. var background = json.a == 0 ? '' : json.a == 1 ? 'rgb(' + args + ')' : 'rgba(' + args + ', ' + json.a + ')';
  249. if($ax.ieColorManager && json.a != 0 && json.a != 1) {
  250. var ieColor = $ax.ieColorManager.getColorFromArgb(json.a * 255, json.r, json.g, json.b, true);
  251. if(ieColor) background = ieColor;
  252. }
  253. div.css('background-color', background);
  254. return div;
  255. };
  256. var _getAdaptiveProp = _repeaterManager.getAdaptiveProp = function(map, prop, viewId) {
  257. var viewChain = $ax.adaptive.getAdaptiveIdChain(viewId);
  258. for(var i = viewChain.length - 1; i >= 0; i--) {
  259. viewId = viewChain[i];
  260. var viewProps = map[viewId];
  261. if(viewProps.hasOwnProperty(prop)) return viewProps[prop];
  262. }
  263. var base = map[''];
  264. if(base.hasOwnProperty(prop)) return base[prop];
  265. return map['default'][prop];
  266. };
  267. _repeaterManager.getItemCount = function(repeaterId) {
  268. var data = repeaterToActiveDataSet[repeaterId].length;
  269. var info = repeaterToPageInfo[repeaterId];
  270. if(!info.noLimit) {
  271. var start = Math.min(data, info.itemsPerPage * info.currPage);
  272. var end = Math.min(data, start + info.itemsPerPage);
  273. data = end - start;
  274. }
  275. return data;
  276. };
  277. _repeaterManager.setDisplayProps = function(obj, repeaterId, itemIndex) {
  278. var data = repeaterToActiveDataSet[repeaterId];
  279. var info = repeaterToPageInfo[repeaterId];
  280. var start = 0;
  281. var end = data.length;
  282. if(!info.noLimit) {
  283. start = Math.min(end, info.itemsPerPage * (info.currPage - 1));
  284. end = Math.min(end, start + info.itemsPerPage);
  285. }
  286. var count = end - start;
  287. var index = -1;
  288. for(var i = 0; i < count; i++) {
  289. if(data[start + i].index == itemIndex) index = i + 1;
  290. }
  291. if(index == -1) return;
  292. obj.index = index;
  293. obj.isfirst = index == 1;
  294. obj.islast = index == end - start;
  295. obj.iseven = index % 2 == 0;
  296. obj.isodd = index % 2 == 1;
  297. };
  298. _repeaterManager.getDataCount = function(repeaterId) {
  299. return repeaterToCurrentDataSet[repeaterId].length;
  300. };
  301. var _getFilteredDataCount = _repeaterManager.getFilteredDataCount = function(repeaterId) {
  302. return repeaterToActiveDataSet[repeaterId].length;
  303. };
  304. _repeaterManager.getPageCount = function(repeaterId) {
  305. var info = repeaterToPageInfo[repeaterId];
  306. return info.noLimit ? 1 : Math.ceil(_getFilteredDataCount(repeaterId) / info.itemsPerPage);
  307. };
  308. _repeaterManager.getPageIndex = function(repeaterId) {
  309. var info = repeaterToPageInfo[repeaterId];
  310. return info.noLimit ? 1 : info.currPage;
  311. };
  312. var getActiveDataSet = function(repeaterId) {
  313. var active = $ax.deepCopy(repeaterToCurrentDataSet[repeaterId]);
  314. // Set up 1 indexing each item.
  315. for(var i = 0; i < active.length; i++) active[i].index = i + 1;
  316. return active;
  317. };
  318. var getOrderedIds = function(repeaterId, eventInfo) {
  319. var data = repeaterToActiveDataSet[repeaterId] = getActiveDataSet(repeaterId);
  320. // Filter first so less to sort
  321. applyFilter(repeaterId, data, eventInfo);
  322. // Sort next
  323. var sorts = repeaterToSorts[repeaterId] || [];
  324. if(sorts.length != 0 && data.length > 1) {
  325. // TODO: Make this generic and factor out if we want to use it elsewhere...
  326. // Compare is a function that takes 2 arguments, and returns a number. A high number means the second should go first
  327. // Otherwise the first stays first.
  328. var mergesort = function(list, start, end, compare) {
  329. var middle = Math.floor((start + end) / 2);
  330. if(middle - start > 1) mergesort(list, start, middle, compare);
  331. if(end - middle > 1) mergesort(list, middle, end, compare);
  332. var index1 = start;
  333. var index2 = middle;
  334. var tempList = [];
  335. while(index1 < middle && index2 < end) {
  336. tempList[tempList.length] = list[compare(list[index1], list[index2]) > 0 ? index2++ : index1++];
  337. }
  338. while(index1 < middle) tempList[tempList.length] = list[index1++];
  339. while(index2 < end) tempList[tempList.length] = list[index2++];
  340. // transfer from temp list to the real list.
  341. for(var i = 0; i < tempList.length; i++) list[start + i] = tempList[i];
  342. };
  343. // Compare is the tie breaking function to us if necessary.
  344. var getComparator = function(columnName, ascending, type, compare) {
  345. // If this needs to be sped up, break up into several smaller functions conditioned off of type
  346. return function(row1, row2) {
  347. // If column undefined, no way to measure this, so call it a tie.
  348. if(row1[columnName] === undefined || row2[columnName] === undefined) return 0;
  349. var text1 = row1[columnName].text;
  350. var text2 = row2[columnName].text;
  351. // This means we are case insensitive, so lowercase everything to kill casing
  352. if(type == 'Text') {
  353. text1 = text1.toLowerCase();
  354. text2 = text2.toLowerCase();
  355. }
  356. //If tied, go to tie breaker
  357. if(text1 == text2) {
  358. if(compare) return compare(row1.index, row2.index);
  359. // Actually a tie.
  360. return 0;
  361. }
  362. if(type == 'Text' || type == 'Text (Case Sensitive)') {
  363. if(text1 < text2 ^ ascending) return 1;
  364. else return -1;
  365. } else if(type == 'Number') {
  366. var num1 = Number(text1);
  367. var num2 = Number(text2);
  368. if(isNaN(num1) && isNaN(num2)) return 0;
  369. if(isNaN(num1) || isNaN(num2)) return isNaN(num1) ? 1 : -1;
  370. if(num1 < num2 ^ ascending) return 1;
  371. else return -1;
  372. } else if(type == 'Date - YYYY-MM-DD' || type == 'Date - MM/DD/YYYY') {
  373. var func = type == 'Date - YYYY-MM-DD' ? getDate1 : getDate2;
  374. var date1 = func(text1);
  375. var date2 = func(text2);
  376. if(!date1.valid && !date2.valid) return 0;
  377. if(!date1.valid || !date2.valid) return date1.valid ? -1 : 1;
  378. var diff = date2.year - date1.year;
  379. if(diff == 0) diff = date2.month - date1.month;
  380. if(diff == 0) diff = date2.day - date1.day;
  381. if(diff == 0) return 0;
  382. return diff > 0 ^ ascending ? 1 : -1;
  383. }
  384. console.log('unhandled sort type');
  385. return 0;
  386. };
  387. };
  388. var compareFunc = null;
  389. for(var i = 0; i < sorts.length; i++) compareFunc = getComparator(sorts[i].columnName, sorts[i].ascending, sorts[i].sortType, compareFunc);
  390. mergesort(data, 0, data.length, compareFunc);
  391. }
  392. var ids = [];
  393. for(i = 0; i < data.length; i++) ids[i] = data[i].index;
  394. return ids;
  395. };
  396. var getDate1 = function(text) {
  397. var date = { valid: false };
  398. var sections = text.split('-');
  399. if(sections.length == 1) sections = text.split('/');
  400. if(sections.length != 3) return date;
  401. date.year = Number(sections[0]);
  402. date.month = Number(sections[1]);
  403. date.day = Number(sections[2]);
  404. date.valid = !isNaN(date.year);
  405. date.valid &= !isNaN(date.month) && date.month > 0 && date.month <= 12;
  406. date.valid &= !isNaN(date.day) && date.day > 0 && date.day <= daysPerMonth(date.month, date.year);
  407. return date;
  408. };
  409. var getDate2 = function(text) {
  410. var date = { valid: false };
  411. var sections = text.split('-');
  412. if(sections.length == 1) sections = text.split('/');
  413. if(sections.length != 3) return date;
  414. date.month = Number(sections[0]);
  415. date.day = Number(sections[1]);
  416. date.year = Number(sections[2]);
  417. date.valid = !isNaN(date.year);
  418. date.valid &= !isNaN(date.month) && date.month > 0 && date.month <= 12;
  419. date.valid &= !isNaN(date.day) && date.day > 0 && date.day <= daysPerMonth(date.month, date.year);
  420. return date;
  421. };
  422. var daysPerMonth = function(month, year) {
  423. if(month == 9 || month == 4 || month == 6 || month == 11) return 30;
  424. if(month != 2) return 31;
  425. if(year % 4 != 0) return 28;
  426. if(year % 100 != 0) return 29;
  427. return year % 400 == 0 ? 29 : 28;
  428. };
  429. var applyFilter = function(repeaterId, data, eventInfo) {
  430. var dataFiltered = [];
  431. var filters = repeaterToFilters[repeaterId] || [];
  432. if(filters.length != 0) {
  433. var oldSrc = eventInfo.srcElement;
  434. var oldThis = eventInfo.thiswidget;
  435. outer:
  436. for(var i = 1; i <= data.length; i++) {
  437. for(var j = 0; j < filters.length; j++) {
  438. eventInfo.targetElement = _createElementId(repeaterId, i);
  439. eventInfo.srcElement = filters[j].thisId;
  440. eventInfo.thiswidget = $ax.getWidgetInfo(eventInfo.srcElement);
  441. if($ax.expr.evaluateExpr(filters[j].filter, eventInfo) != 'true') continue outer;
  442. }
  443. dataFiltered[dataFiltered.length] = data[i - 1];
  444. }
  445. for(i = 0; i < dataFiltered.length; i++) data[i] = dataFiltered[i];
  446. while(data.length > dataFiltered.length) data.pop();
  447. eventInfo.srcElement = oldSrc;
  448. eventInfo.thiswidget = oldThis;
  449. }
  450. };
  451. var _addFilter = function(repeaterId, label, filter, thisId) {
  452. var filterList = repeaterToFilters[repeaterId];
  453. if(!filterList) repeaterToFilters[repeaterId] = filterList = [];
  454. var filterObj = { filter: filter, thisId: thisId };
  455. if(label) filterObj.label = label;
  456. filterList[filterList.length] = filterObj;
  457. };
  458. _repeaterManager.addFilter = _addFilter;
  459. var _removeFilter = function(repeaterId, label) {
  460. var filterList = repeaterToFilters[repeaterId];
  461. // If no list, nothing to remove
  462. if(!filterList) return;
  463. // If no label, remove everything
  464. if(!label) {
  465. repeaterToFilters[repeaterId] = [];
  466. return;
  467. }
  468. for(var i = filterList.length - 1; i >= 0; i--) {
  469. var filterObj = filterList[i];
  470. if(filterObj.label && filterObj.label == label) $ax.splice(filterList, i, 1);
  471. }
  472. };
  473. _repeaterManager.removeFilter = _removeFilter;
  474. var _addSort = function(repeaterId, label, columnName, ascending, toggle, sortType) {
  475. var sortList = repeaterToSorts[repeaterId];
  476. if(!sortList) repeaterToSorts[repeaterId] = sortList = [];
  477. for(var i = 0; i < sortList.length; i++) {
  478. if(columnName == sortList[i].columnName) {
  479. var lastSortObj = $ax.splice(sortList, i, 1)[0];
  480. if(toggle) ascending = !lastSortObj.ascending;
  481. break;
  482. }
  483. }
  484. var sortObj = { columnName: columnName, ascending: ascending, sortType: sortType };
  485. if(label) sortObj.label = label;
  486. sortList[sortList.length] = sortObj;
  487. };
  488. _repeaterManager.addSort = _addSort;
  489. var _removeSort = function(repeaterId, label) {
  490. var sortList = repeaterToSorts[repeaterId];
  491. // If no list, nothing to remove
  492. if(!sortList) return;
  493. // If no label, remove everything
  494. if(!label) {
  495. repeaterToSorts[repeaterId] = [];
  496. return;
  497. }
  498. for(var i = sortList.length - 1; i >= 0; i--) {
  499. var sortObj = sortList[i];
  500. if(sortObj.label && sortObj.label == label) $ax.splice(sortList, i, 1);
  501. }
  502. };
  503. _repeaterManager.removeSort = _removeSort;
  504. var _setRepeaterToPage = function(repeaterId, type, value, eventInfo) {
  505. var pageInfo = repeaterToPageInfo[repeaterId];
  506. // page doesn't matter if there is no limit.
  507. if(pageInfo.noLimit) return;
  508. var dataSet = repeaterToActiveDataSet[repeaterId];
  509. if(!dataSet) dataSet = repeaterToCurrentDataSet[repeaterId];
  510. var lastPage = Math.ceil(dataSet.length / pageInfo.itemsPerPage);
  511. if(type == 'Value') {
  512. var val = Number($ax.expr.evaluateExpr(value, eventInfo));
  513. // if invalid, default to 1, otherwise, clamp the value
  514. if(isNaN(val)) val = 1;
  515. else if(val < 1) val = 1;
  516. else if(val > lastPage) val = lastPage;
  517. pageInfo.currPage = val;
  518. } else if(type == 'Previous') {
  519. if(pageInfo.currPage > 1) pageInfo.currPage--;
  520. } else if(type == 'Next') {
  521. if(pageInfo.currPage < lastPage) pageInfo.currPage++;
  522. } else if(type == 'Last') {
  523. pageInfo.currPage = lastPage;
  524. } else {
  525. console.log('Unknown type');
  526. }
  527. };
  528. _repeaterManager.setRepeaterToPage = _setRepeaterToPage;
  529. var _setNoItemLimit = function(repeaterId) {
  530. var pageInfo = repeaterToPageInfo[repeaterId];
  531. delete pageInfo.currPage;
  532. delete pageInfo.itemsPerPage;
  533. pageInfo.noLimit = true;
  534. };
  535. _repeaterManager.setNoItemLimit = _setNoItemLimit;
  536. var _setItemLimit = function(repeaterId, value, eventInfo) {
  537. var pageInfo = repeaterToPageInfo[repeaterId];
  538. if(pageInfo.noLimit) {
  539. pageInfo.noLimit = false;
  540. pageInfo.currPage = 1;
  541. }
  542. var oldTarget = eventInfo.targetElement;
  543. eventInfo.targetElement = repeaterId;
  544. var itemLimit = Number($ax.expr.evaluateExpr(value, eventInfo));
  545. eventInfo.targetElement = oldTarget;
  546. if(isNaN(itemLimit)) itemLimit = 20;
  547. else if(itemLimit < 1) itemLimit = 1;
  548. pageInfo.itemsPerPage = itemLimit;
  549. };
  550. _repeaterManager.setItemLimit = _setItemLimit;
  551. var removeItems = function(repeaterId) {
  552. var elementIds = $ax.getChildElementIdsForRepeater(repeaterId);
  553. for(var i = 0; i < elementIds.length; i++) $('#' + elementIds[i]).remove();
  554. $ax.visibility.clearLimboAndHiddenIds(elementIds);
  555. $ax.clearItemsForRepeater(repeaterId);
  556. };
  557. var _getDataFromDataSet = function(repeaterId, itemId, propName, type) {
  558. var itemNum = Number(itemId) - 1;
  559. // Default to obj with text as empty string, as we don't generate the data for empty props
  560. var data = repeaterToCurrentDataSet[repeaterId][itemNum][propName] || { text: '' };
  561. //For now text is always the default. May change this to depend on context.
  562. return type == 'data' && data.type != 'text' ? data : (type && data[type]) || data['text'];
  563. };
  564. _repeaterManager.getData = _getDataFromDataSet;
  565. _repeaterManager.hasData = function(id, propName) {
  566. var repeaterId = $ax.getParentRepeaterFromScriptId(_getScriptIdFromElementId(id));
  567. return Boolean(repeaterToCurrentDataSet[repeaterId] && repeaterToCurrentDataSet[repeaterId].props.indexOf(propName) != -1);
  568. };
  569. var _addItemToDataSet = function(repeaterId, row, itemEventInfo) {
  570. itemEventInfo.data = true;
  571. var oldTarget = itemEventInfo.targetElement;
  572. itemEventInfo.targetElement = repeaterId;
  573. var dataSet = repeaterToLocalDataSet[repeaterId];
  574. for(var propName in row) {
  575. if(!row.hasOwnProperty(propName)) continue;
  576. var prop = row[propName];
  577. if(prop.type == 'literal') {
  578. var retval = $ax.expr.evaluateExpr(prop.literal, itemEventInfo);
  579. if(typeof (retval) == 'string') retval = { type: 'text', text: retval };
  580. row[propName] = retval;
  581. }
  582. }
  583. itemEventInfo.targetElement = oldTarget;
  584. dataSet[dataSet.length] = row;
  585. itemEventInfo.data = false;
  586. };
  587. _repeaterManager.addItem = _addItemToDataSet;
  588. var _deleteItemsFromDataSet = function(repeaterId, eventInfo, type, rule) {
  589. var dataSet = repeaterToCurrentDataSet[repeaterId];
  590. var items;
  591. // Should always be this, marked, or rule.
  592. if(type == 'this') items = [_getItemIdFromElementId(eventInfo.srcElement)];
  593. else if(type == 'marked') items = repeaterToEditItems[repeaterId];
  594. else {
  595. // This should be rule
  596. var visibleData = repeaterToActiveDataSet[repeaterId];
  597. var items = [];
  598. var oldTarget = eventInfo.targetElement;
  599. for(var i = 0; i < visibleData.length; i++) {
  600. eventInfo.targetElement = _createElementId(repeaterId, visibleData[i].index);
  601. if($ax.expr.evaluateExpr(rule, eventInfo).toLowerCase() != 'true') continue;
  602. items.push(visibleData[i].index);
  603. }
  604. eventInfo.targetElement = oldTarget;
  605. }
  606. // Want them decending
  607. items.sort(function(a, b) { return b - a; });
  608. for(i = 0; i < items.length; i++) {
  609. var itemId = items[i];
  610. $ax.splice(dataSet, itemId - 1, 1);
  611. }
  612. repeaterToEditItems[repeaterId] = [];
  613. };
  614. _repeaterManager.deleteItems = _deleteItemsFromDataSet;
  615. var _updateEditItemsInDataSet = function(repeaterId, propMap, eventInfo, type, rule) {
  616. var oldTarget = eventInfo.targetElement;
  617. var dataSet = repeaterToCurrentDataSet[repeaterId];
  618. var items;
  619. // Should always be this, marked, or rule.
  620. if(type == 'this') items = [_getItemIdFromElementId(eventInfo.srcElement)];
  621. else if(type == 'marked') items = repeaterToEditItems[repeaterId];
  622. else {
  623. // This should be rule
  624. var visibleData = repeaterToActiveDataSet[repeaterId];
  625. var items = [];
  626. var oldTarget = eventInfo.targetElement;
  627. for(var i = 0; i < visibleData.length; i++) {
  628. eventInfo.targetElement = _createElementId(repeaterId, visibleData[i].index);
  629. if($ax.expr.evaluateExpr(rule, eventInfo).toLowerCase() != 'true') continue;
  630. items.push(visibleData[i].index);
  631. }
  632. eventInfo.targetElement = oldTarget;
  633. }
  634. eventInfo.data = true;
  635. for(var prop in propMap) {
  636. if(!propMap.hasOwnProperty(prop)) continue;
  637. for(var i = 0; i < items.length; i++) {
  638. var data = propMap[prop];
  639. var item = items[i];
  640. if(data.type == 'literal') {
  641. eventInfo.targetElement = _createElementId(repeaterId, item);
  642. data = $ax.expr.evaluateExpr(data.literal, eventInfo);
  643. if(typeof (data) == 'string') data = { type: 'text', text: data };
  644. }
  645. dataSet[item - 1][prop] = data;
  646. }
  647. }
  648. eventInfo.targetElement = oldTarget;
  649. eventInfo.data = false;
  650. };
  651. _repeaterManager.updateEditItems = _updateEditItemsInDataSet;
  652. var _getAllItemIds = function(repeaterId) {
  653. var retval = [];
  654. var currDataSet = repeaterToCurrentDataSet[repeaterId];
  655. for(var i = 0; i < currDataSet.length; i++) retval.push(i + 1);
  656. return retval;
  657. };
  658. _repeaterManager.getAllItemIds = _getAllItemIds;
  659. var _addEditItemToRepeater = function(repeaterId, itemIds) {
  660. for(var i = 0; i < itemIds.length; i++) {
  661. var itemId = Number(itemIds[i]);
  662. var items = repeaterToEditItems[repeaterId];
  663. if(items.indexOf(itemId) == -1) items[items.length] = itemId;
  664. }
  665. };
  666. _repeaterManager.addEditItems = _addEditItemToRepeater;
  667. var _removeEditItemFromRepeater = function(repeaterId, itemIds) {
  668. for(var i = 0; i < itemIds.length; i++) {
  669. var itemId = itemIds[i];
  670. var items = repeaterToEditItems[repeaterId];
  671. var index = items.indexOf(Number(itemId));
  672. if(index != -1) $ax.splice(items, index, 1);
  673. }
  674. };
  675. _repeaterManager.removeEditItems = _removeEditItemFromRepeater;
  676. _repeaterManager.isEditItem = function(repeaterId, itemId) {
  677. var items = repeaterToEditItems[repeaterId];
  678. return items.indexOf(Number(itemId)) != -1;
  679. };
  680. var _createElementId = function(scriptId, itemId) {
  681. if(!itemId) return scriptId;
  682. var i = scriptId.indexOf('_');
  683. var sections = i > -1 ? [scriptId.substring(0, i), scriptId.substring(i + 1)] : [scriptId];
  684. var retval = sections[0] + '-' + itemId;
  685. return sections.length > 1 ? retval + '_' + sections[1] : retval;
  686. };
  687. _repeaterManager.createElementId = _createElementId;
  688. var _getElementId = function(scriptId, childId) {
  689. var elementId = scriptId;
  690. if($ax.getParentRepeaterFromScriptId(scriptId)) {
  691. // Must be in the same item as the child
  692. var itemId = $ax.repeater.getItemIdFromElementId(childId);
  693. elementId = $ax.repeater.createElementId(scriptId, itemId);
  694. }
  695. return elementId;
  696. };
  697. _repeaterManager.getElementId = _getElementId;
  698. var _getScriptIdFromElementId = function(elementId) {
  699. if(!elementId) return elementId;
  700. var sections = elementId.split('-');
  701. var retval = sections[0];
  702. if(sections.length <= 1) return retval;
  703. sections = sections[1].split('_');
  704. return sections.length > 1 ? retval + '_' + sections[1] : retval;
  705. };
  706. _repeaterManager.getScriptIdFromElementId = _getScriptIdFromElementId;
  707. var _getItemIdFromElementId = function(elementId) {
  708. var sections = elementId.split('-');
  709. if(sections.length < 2) return '';
  710. sections = sections[1].split('_');
  711. return sections[0];
  712. };
  713. _repeaterManager.getItemIdFromElementId = _getItemIdFromElementId;
  714. // TODO: Just inline this if we keep it this way.
  715. var _applySuffixToElementId = function(id, suffix) {
  716. return id + suffix;
  717. // return _createElementId(_getScriptIdFromElementId(id) + suffix, _getItemIdFromElementId(id));
  718. };
  719. _repeaterManager.applySuffixToElementId = _applySuffixToElementId;
  720. // var _getRepeaterSize = function(repeaterId) {
  721. // var itemCount = ($ax.getItemIdsForRepeater(repeaterId) || []).length;
  722. // if(itemCount == 0) return { width: 0, height: 0 };
  723. // var repeater = $obj(repeaterId);
  724. // // Width and height per item;
  725. // var width = repeater.width;
  726. // var height = repeater.height;
  727. // var viewId = $ax.adaptive.currentViewId || '';
  728. // var widthIncrement = width + _getAdaptiveProp(repeater.repeaterPropMap, 'horizontalSpacing', viewId);
  729. // var heightIncrement = height + _getAdaptiveProp(repeater.repeaterPropMap, 'verticalSpacing', viewId);
  730. // var wrap = _getAdaptiveProp(repeater.repeaterPropMap, 'wrap', viewId);
  731. // var vertical = _getAdaptiveProp(repeater.repeaterPropMap, 'vertical', viewId);
  732. // if(wrap == -1 || itemCount <= wrap) {
  733. // if(vertical) height += heightIncrement * (itemCount - 1);
  734. // else width += widthIncrement * (itemCount - 1);
  735. // } else {
  736. // var primaryDim = wrap;
  737. // var secondaryDim = Math.ceil(itemCount / primaryDim);
  738. // if(vertical) {
  739. // height += heightIncrement * (primaryDim - 1);
  740. // width += widthIncrement * (secondaryDim - 1);
  741. // } else {
  742. // width += widthIncrement * (primaryDim - 1);
  743. // height += heightIncrement * (secondaryDim - 1);
  744. // }
  745. // }
  746. // return { width: width, height: height };
  747. // };
  748. // _repeaterManager.getRepeaterSize = _getRepeaterSize;
  749. });
  750. // ******* Dynamic Panel Manager ******** //
  751. $axure.internal(function($ax) {
  752. // TODO: Probably a lot of the dynamic panel functions from pagescript should be moved here at some point...
  753. var _dynamicPanelManager = $ax.dynamicPanelManager = {};
  754. var _isIdFitToContent = _dynamicPanelManager.isIdFitToContent = function(id) {
  755. var obj = $obj(id);
  756. if(!obj || obj.type != 'dynamicPanel' || !obj.fitToContent) return false;
  757. var jobj = $jobj($ax.visibility.GetPanelState(id));
  758. return jobj.css('position') == 'relative';
  759. };
  760. var _fitParentPanel = function(widgetId) {
  761. // Find parent panel if there is one.
  762. var parentPanelInfo = getParentPanel(widgetId);
  763. if(!parentPanelInfo) return;
  764. var parentId = parentPanelInfo.parent;
  765. if(_updateFitPanel(parentId, parentPanelInfo.state)) _fitParentPanel(parentId);
  766. };
  767. _dynamicPanelManager.fitParentPanel = _fitParentPanel;
  768. _dynamicPanelManager.initialize = function() {
  769. _dynamicPanelManager.initFitPanels($ax('*'));
  770. $axure.resize(_handleResize);
  771. _handleResize();
  772. };
  773. _dynamicPanelManager.initFitPanels = function(query) {
  774. var fitToContent = [];
  775. query.each(function(obj, elementId) {
  776. if(obj.type == 'dynamicPanel' && obj.fitToContent) fitToContent[fitToContent.length] = elementId;
  777. });
  778. for(var i = fitToContent.length - 1; i >= 0; i--) {
  779. var panelId = fitToContent[i];
  780. var stateCount = $obj(panelId).diagrams.length;
  781. for(var j = 0; j < stateCount; j++) {
  782. // Traverse through children to find what size it should be.
  783. var stateId = $ax.repeater.applySuffixToElementId(panelId, '_state' + j);
  784. var stateContentId = stateId + '_content';
  785. var stateQuery = $jobj(stateId);
  786. var size = getContainerSize(stateContentId);
  787. if(!$obj(panelId).percentWidth) stateQuery.width(size.width);
  788. stateQuery.height(size.height);
  789. }
  790. }
  791. };
  792. var percentPanelToLeftCache = [];
  793. var percentPanelsInitialized = false;
  794. var _handleResize = function() {
  795. if(percentPanelsInitialized) {
  796. for(var key in percentPanelToLeftCache) {
  797. //could optimize to only update non-contained panels
  798. _updatePanelPercentWidth(key);
  799. }
  800. } else {
  801. $ax('*').each(function(obj, elementId) {
  802. if(_isPercentWidthPanel(obj)) _updatePanelPercentWidth(elementId);
  803. });
  804. percentPanelsInitialized = true;
  805. }
  806. };
  807. var _isPercentWidthPanel = _dynamicPanelManager.isPercentWidthPanel = function(obj) {
  808. return obj && obj.type == 'dynamicPanel' && obj.percentWidth;
  809. };
  810. _dynamicPanelManager.updatePanelContentPercentWidth = function(elementId) {
  811. // if(_isPercentWidthPanel($obj(elementId))) return;
  812. var stateChildrenQuery = $jobj(elementId).children('.panel_state');
  813. stateChildrenQuery.children('.panel_state_content').each(
  814. function() {
  815. $(this).children('.ax_dynamic_panel').each(
  816. function() { _updatePanelPercentWidth(this.id); }
  817. );
  818. }
  819. );
  820. };
  821. _dynamicPanelManager.updatePercentPanelCache = function(query) {
  822. query.each(function(obj, elementId) {
  823. if(_isPercentWidthPanel(obj)) {
  824. if(_updatePercentPanelToLeftCache(obj, elementId, true)) {
  825. _updatePanelPercentWidth(elementId);
  826. }
  827. }
  828. });
  829. };
  830. _dynamicPanelManager.resetFixedPanel = function(obj, domElement) {
  831. if(obj.fixedHorizontal == 'center') domElement.style.marginLeft = "";
  832. if(obj.fixedVertical == 'middle') domElement.style.marginTop = "";
  833. };
  834. _dynamicPanelManager.resetAdaptivePercentPanel = function(obj, domElement) {
  835. if(!_isPercentWidthPanel(obj)) return;
  836. if(obj.fixedHorizontal == 'center') domElement.style.marginLeft = "";
  837. else if(obj.fixedHorizontal == 'right') domElement.style.width = "";
  838. };
  839. var _updatePercentPanelToLeftCache = function(obj, elementId, overwrite) {
  840. var wasUpdated = false;
  841. var jObj = $jobj(elementId);
  842. if(percentPanelToLeftCache[elementId] == undefined || overwrite) {
  843. if(obj.fixedHorizontal == 'center') percentPanelToLeftCache[elementId] = Number(jObj.css('margin-left').replace("px", ""));
  844. else if(obj.fixedHorizontal == 'right') percentPanelToLeftCache[elementId] = jObj.width() + Number(jObj.css('right').replace("px", ""));
  845. else percentPanelToLeftCache[elementId] = Number(jObj.css('left').replace("px", ""));
  846. wasUpdated = true;
  847. }
  848. if(obj.fixedHorizontal == 'right' && _isIdFitToContent(elementId)) {
  849. var fitWidth = getContainerSize($ax.visibility.GetPanelState(elementId) + '_content').width;
  850. percentPanelToLeftCache[elementId] = fitWidth + Number(jObj.css('right').replace("px", ""));
  851. wasUpdated = true;
  852. }
  853. return wasUpdated;
  854. };
  855. var _updatePanelPercentWidth = _dynamicPanelManager.updatePanelPercentWidth = function(elementId) {
  856. var obj = $obj(elementId);
  857. if(!_isPercentWidthPanel(obj)) return;
  858. _updatePercentPanelToLeftCache(obj, elementId, false);
  859. var width;
  860. var x;
  861. if(obj.fixedHorizontal) {
  862. x = 0;
  863. width = $(window).width();
  864. } else {
  865. var parentPanelInfo = getParentPanel(elementId);
  866. if(parentPanelInfo) {
  867. var parentId = parentPanelInfo.parent;
  868. width = $jobj(parentId).width();
  869. var parentObj = $obj(parentId);
  870. if(parentObj.percentWidth) {
  871. var stateId = $ax.repeater.applySuffixToElementId(parentId, '_state' + parentPanelInfo.state);
  872. var stateContentId = stateId + '_content';
  873. x = -Number($jobj(stateContentId).css('margin-left').replace("px", ""));
  874. } else x = 0;
  875. } else {
  876. var parentRepeater = $ax.getParentRepeaterFromScriptId($ax.repeater.getScriptIdFromElementId(elementId));
  877. if(parentRepeater) {
  878. var itemId = $ax.repeater.getItemIdFromElementId(elementId);
  879. var itemContainerId = $ax.repeater.createElementId(parentRepeater, itemId);
  880. x = 0;
  881. width = $jobj(itemContainerId).width();
  882. } else {
  883. var $window = $(window);
  884. width = $window.width();
  885. var bodyLeft = Number($('body').css('left').replace("px", ""));
  886. var bodyWidth = Number($('body').css('width').replace("px", ""));
  887. var isCenter = $ax.adaptive.getPageStyle().pageAlignment == 'center';
  888. width = Math.max(width, bodyWidth);
  889. x = isCenter ? -(width - bodyWidth) / 2 - bodyLeft : 0;
  890. }
  891. }
  892. }
  893. var jObj = $jobj(elementId);
  894. if(obj.fixedHorizontal == 'left') jObj.css('left', x + 'px');
  895. else if(obj.fixedHorizontal == 'center') {
  896. jObj.css('left', x + 'px');
  897. jObj.css('margin-left', 0 + 'px');
  898. } else jObj.css('left', x + 'px');
  899. jObj.css('width', width + 'px');
  900. var panelLeft = percentPanelToLeftCache[elementId];
  901. var stateChildrenQuery = jObj.children('.panel_state');
  902. stateChildrenQuery.css('width', width + 'px');
  903. if(obj.fixedHorizontal == 'center')
  904. stateChildrenQuery.children('.panel_state_content').css('left', '50%').css('margin-left', panelLeft + 'px');
  905. else if(obj.fixedHorizontal == 'right')
  906. stateChildrenQuery.children('.panel_state_content').css('left', width - panelLeft + 'px');
  907. else stateChildrenQuery.children('.panel_state_content').css('margin-left', panelLeft - x + 'px');
  908. };
  909. _dynamicPanelManager.updateAllFitPanels = function() {
  910. var fitToContent = [];
  911. $ax('*').each(function(obj, elementId) {
  912. if(obj.type == 'dynamicPanel' && obj.fitToContent) fitToContent[fitToContent.length] = elementId;
  913. });
  914. for(var i = fitToContent.length - 1; i >= 0; i--) {
  915. var panelId = fitToContent[i];
  916. var stateCount = $obj(panelId).diagrams.length;
  917. for(var j = 0; j < stateCount; j++) {
  918. $ax.dynamicPanelManager.setFitToContentCss(panelId, true);
  919. _updateFitPanel(panelId, j, true);
  920. }
  921. }
  922. };
  923. _dynamicPanelManager.setFitToContentCss = function(elementId, fitToContent, oldWidth, oldHeight) {
  924. if($ax.dynamicPanelManager.isIdFitToContent(elementId) == fitToContent) return;
  925. var panel = $jobj(elementId);
  926. var stateCss;
  927. var scrollbars = $obj(elementId).scrollbars;
  928. if(fitToContent) {
  929. panel.attr('style', '');
  930. stateCss = {};
  931. stateCss.position = 'relative';
  932. if(scrollbars != 'none') {
  933. stateCss.overflow = 'visible';
  934. stateCss['-webkit-overflow-scrolling'] = 'visible';
  935. }
  936. if(scrollbars == 'verticalAsNeeded') {
  937. stateCss['overflow-x'] = 'visible';
  938. stateCss['-ms-overflow-x'] = 'visible';
  939. } else if(scrollbars == 'horizontalAsNeeded') {
  940. stateCss['overflow-y'] = 'visible';
  941. stateCss['-ms-overflow-y'] = 'visible';
  942. }
  943. panel.children().css(stateCss);
  944. } else {
  945. var panelCss = { width: oldWidth, height: oldHeight };
  946. stateCss = { width: oldWidth, height: oldHeight };
  947. panelCss.overflow = 'hidden';
  948. stateCss.position = 'absolute';
  949. if(scrollbars != 'none') {
  950. stateCss.overflow = 'auto';
  951. stateCss['-webkit-overflow-scrolling'] = 'touch';
  952. }
  953. if(scrollbars == 'verticalAsNeeded') {
  954. stateCss['overflow-x'] = 'hidden';
  955. stateCss['-ms-overflow-x'] = 'hidden';
  956. } else if(scrollbars == 'horizontalAsNeeded') {
  957. stateCss['overflow-y'] = 'hidden';
  958. stateCss['-ms-overflow-y'] = 'hidden';
  959. }
  960. panel.css(panelCss);
  961. panel.children().css(stateCss);
  962. }
  963. };
  964. // TODO: [ben] This should be used in a lot more places...
  965. var _getPanelJobj = _dynamicPanelManager.getPanelJobj = function(id) {
  966. // Try for container first
  967. var container = $jobj(id + '_container');
  968. return container.length ? container : $jobj(id);
  969. };
  970. var _getShownState = function(id) {
  971. var obj = $obj(id);
  972. if(!obj || obj.type != 'dynamicPanel') return $jobj(id);
  973. var children = $jobj(id).children();
  974. for(var i = 0; i < children.length; i++) {
  975. var child = children[i];
  976. if(child && child.style && child.style.display != 'none') return $jobj(child.id);
  977. }
  978. return $jobj(id);
  979. };
  980. var _getClamp = function(id) {
  981. var obj = $obj(id);
  982. if(!obj) return _getPanelJobj(id);
  983. if(obj.type == 'dynamicPanel') return _getShownState(id);
  984. return $jobj(id);
  985. };
  986. var _updateFitPanel = function(panelId, stateIndex, initializingView) {
  987. if(!panelId) return false;
  988. // Only fit if fitToContent is true
  989. if(!$ax.dynamicPanelManager.isIdFitToContent(panelId)) return false;
  990. // Traverse through children to find what size it should be.
  991. var stateId = $ax.repeater.applySuffixToElementId(panelId, '_state' + stateIndex);
  992. var stateContentId = stateId + '_content';
  993. var stateQuery = $jobj(stateId);
  994. var size = getContainerSize(stateContentId);
  995. // Skip if size hasn't changed
  996. var oldWidth = stateQuery.width();
  997. var oldHeight = stateQuery.height();
  998. if(oldWidth == size.width && oldHeight == size.height) return false;
  999. if(!$obj(panelId).percentWidth) stateQuery.width(size.width);
  1000. stateQuery.height(size.height);
  1001. //updatePercentWidth on all child panels
  1002. $jobj(stateContentId).children('.ax_dynamic_panel').each(
  1003. function() { _updatePanelPercentWidth(this.id); }
  1004. );
  1005. //do the following only if it is the current state
  1006. if(stateId != $ax.visibility.GetPanelState(panelId)) return false;
  1007. if(!initializingView) _adjustFixed(panelId, oldWidth, oldHeight, size.width, size.height);
  1008. else if(stateIndex != 0) {
  1009. var state0 = $jobj($ax.repeater.applySuffixToElementId(panelId, '_state0'));
  1010. _adjustFixed(panelId, state0.width(), state0.height(), size.width, size.height);
  1011. }
  1012. $ax.event.raiseSyntheticEvent(panelId, 'onResize');
  1013. $ax.flyoutManager.updateFlyout(panelId);
  1014. return true;
  1015. };
  1016. var getParentPanel = function(widgetId, path) {
  1017. path = path || $ax.getPathFromScriptId($ax.repeater.getScriptIdFromElementId(widgetId));
  1018. var obj = $obj(widgetId);
  1019. if(obj.parentDynamicPanel) {
  1020. path[path.length - 1] = obj.parentDynamicPanel;
  1021. var parentId = $ax.getScriptIdFromPath(path);
  1022. parentId = $ax.repeater.getElementId(parentId, widgetId);
  1023. var parentObj = $obj(parentId);
  1024. var retVal = { parent: parentId };
  1025. for(var i = 0; i < parentObj.diagrams.length; i++) {
  1026. var stateId = $ax.repeater.applySuffixToElementId(parentId, '_state' + i);
  1027. var stateQuery = $jobj(stateId);
  1028. if(stateQuery.find('#' + widgetId).length != 0) {
  1029. retVal.state = i;
  1030. break;
  1031. }
  1032. }
  1033. return retVal;
  1034. }
  1035. if(path.length == 1) return undefined;
  1036. path.pop();
  1037. var parentMaster = $ax.getScriptIdFromPath(path);
  1038. parentMaster = $ax.repeater.getElementId(parentMaster, widgetId);
  1039. return getParentPanel(parentMaster, path);
  1040. };
  1041. // TODO: May be a better location for this. Used currently for rdo and panel state containers
  1042. var getContainerSize = function(containerId) {
  1043. var containerQuery = containerId ? $jobj(containerId) : $('#base');
  1044. var children = containerQuery.children();
  1045. // Default size
  1046. var size = { width: 0, height: 0 };
  1047. for(var i = 0; i < children.length; i++) {
  1048. var child = $(children[i]);
  1049. var childId = child.attr('id');
  1050. if(!childId || $ax.visibility.limboIds[childId] || !$ax.visibility.IsIdVisible(childId)) continue;
  1051. var childObj = $obj(childId);
  1052. // On the body there are some children that should be ignored, as they are not objects.
  1053. if(!childObj) continue;
  1054. // Ignore fixed
  1055. if(childObj.type == 'dynamicPanel' && childObj.fixedHorizontal) continue;
  1056. var position = { left: Number(child.css('left').replace('px', '')), top: Number(child.css('top').replace('px', '')) };
  1057. // Default width and height (adjusted for widgets that are special)
  1058. var width = child.width();
  1059. var height = child.height();
  1060. if(childObj.type == 'master') {
  1061. var masterSize = getContainerSize(childId);
  1062. width = masterSize.width;
  1063. height = masterSize.height;
  1064. // } else if(childObj.type == 'repeater') {
  1065. // var repeaterSize = $ax.repeater.getRepeaterSize(childId);
  1066. // width = repeaterSize.width;
  1067. // height = repeaterSize.height;
  1068. // if(width == 0 && height == 0) continue;
  1069. // position.left += childObj.x;
  1070. // position.top += childObj.y;
  1071. } else if(childObj.type == 'dynamicPanel') {
  1072. if($ax.dynamicPanelManager.isIdFitToContent(childId)) {
  1073. var stateQuery = $jobj($ax.visibility.GetPanelState(childId));
  1074. width = stateQuery.width();
  1075. height = stateQuery.height();
  1076. }
  1077. }
  1078. size.width = Math.max(size.width, position.left + width);
  1079. size.height = Math.max(size.height, position.top + height);
  1080. }
  1081. return size;
  1082. };
  1083. var _adjustFixed = _dynamicPanelManager.adjustFixed = function(panelId, oldWidth, oldHeight, width, height) {
  1084. var loc = _getFixedPosition(panelId, oldWidth, oldHeight, width, height);
  1085. if(loc) {
  1086. $ax.action.addAnimation(panelId, function() {
  1087. $ax.move.MoveWidget(panelId, loc[0], loc[1], 'none', 0, false, null, true);
  1088. });
  1089. }
  1090. };
  1091. var _getFixedPosition = _dynamicPanelManager.getFixedPosition = function(panelId, oldWidth, oldHeight, width, height) {
  1092. var panelObj = $obj(panelId);
  1093. var x = 0;
  1094. var y = 0;
  1095. if(panelObj.fixedHorizontal == 'center') {
  1096. x = (oldWidth - width) / 2;
  1097. }
  1098. if(panelObj.fixedVertical == 'middle') {
  1099. y = (oldHeight - height) / 2;
  1100. }
  1101. return x == 0 && y == 0 ? undefined : [x, y];
  1102. };
  1103. _dynamicPanelManager.getFixedInfo = function(panelId) {
  1104. var panelObj = $obj(panelId);
  1105. if(!panelObj || panelObj.type != 'dynamicPanel') return {};
  1106. var jobj = $jobj(panelId);
  1107. if(jobj.css('position') == 'absolute') return {};
  1108. var info = {};
  1109. var horizontal = panelObj.fixedHorizontal;
  1110. if(!horizontal) return info;
  1111. info.fixed = true;
  1112. info.horizontal = horizontal;
  1113. info.vertical = panelObj.fixedVertical;
  1114. if(info.horizontal == 'left') info.x = Number(jobj.css('left').replace('px', ''));
  1115. else if(info.horizontal == 'center') info.x = Number(jobj.css('margin-left').replace('px', ''));
  1116. else if(info.horizontal == 'right') info.x = Number(jobj.css('right').replace('px', ''));
  1117. if(info.vertical == 'top') info.y = Number(jobj.css('top').replace('px', ''));
  1118. else if(info.vertical == 'middle') info.y = Number(jobj.css('margin-top').replace('px', ''));
  1119. else if(info.vertical == 'bottom') info.y = Number(jobj.css('bottom').replace('px', ''));
  1120. return info;
  1121. };
  1122. // Show isn't necessary if this is always done before toggling (which is currently true), but I don't want that
  1123. // change (if it happened) to break this.
  1124. var _compressToggle = function(id, vert, show, easing, duration) {
  1125. var panelJobj = _getPanelJobj(id);
  1126. var threshold = Number(panelJobj.css(vert ? 'top' : 'left').replace('px', ''));
  1127. var delta = _getShownState(id)[vert ? 'height' : 'width']();
  1128. if(!show) {
  1129. // Need to make threshold bottom/right
  1130. threshold += delta;
  1131. // Delta is in the opposite direction
  1132. delta *= -1;
  1133. }
  1134. _compress(id, vert, threshold, delta, easing, duration);
  1135. };
  1136. _dynamicPanelManager.compressToggle = _compressToggle;
  1137. // Used when setting state of dynamic panel
  1138. var _compressDelta = function(id, oldState, newState, vert, easing, duration) {
  1139. var panelQuery = $jobj(id);
  1140. var oldQuery = $jobj(oldState);
  1141. var newQuery = $jobj(newState);
  1142. var thresholdProp = vert ? 'top' : 'left';
  1143. var thresholdOffset = vert ? 'height' : 'width';
  1144. var threshold = Number(panelQuery.css(thresholdProp).replace('px', ''));
  1145. threshold += oldQuery[thresholdOffset]();
  1146. var delta = newQuery[thresholdOffset]() - oldQuery[thresholdOffset]();
  1147. var clampProp = vert ? 'left' : 'top';
  1148. var clampOffset = vert ? 'width' : 'height';
  1149. var clamp = [Number(panelQuery.css(clampProp).replace('px', ''))];
  1150. clamp[1] = clamp[0] + Math.max(oldQuery[clampOffset](), newQuery[clampOffset]());
  1151. _compress(id, vert, threshold, delta, easing, duration, clamp);
  1152. };
  1153. _dynamicPanelManager.compressDelta = _compressDelta;
  1154. var _compress = function(id, vert, threshold, delta, easing, duration, clamp) {
  1155. if(!easing) {
  1156. easing = 'none';
  1157. duration = 0;
  1158. }
  1159. var parent = $ax('#' + id).getParents()[0];
  1160. var obj = $ax.getObjectFromElementId(parent);
  1161. while(obj && obj.type == 'referenceDiagramObject') {
  1162. parent = $ax('#' + parent).getParents()[0];
  1163. obj = $ax.getObjectFromElementId(parent);
  1164. }
  1165. var clampLoc = _getPanelJobj(id);
  1166. var clampDim = _getClamp(id);
  1167. // If below, a horizantal clamp, otherwise a vertical clamp
  1168. var clampProp = vert ? 'left' : 'top';
  1169. var clampOffset = vert ? 'width' : 'height';
  1170. if(!clamp) {
  1171. clamp = [Number(clampLoc.css(clampProp).replace('px', ''))];
  1172. clamp[1] = clamp[0] + clampDim[clampOffset]();
  1173. }
  1174. // If clamps, threshold, or delta is not a number, can't compress.
  1175. if(isNaN(clamp[0]) || isNaN(clamp[1]) || isNaN(threshold) || isNaN(delta)) return;
  1176. // Note: If parent is body, some of these aren't widgets
  1177. if(parent && $jobj(parent + '_content').length > 0) parent = parent + '_content';
  1178. var children = $(parent ? '#' + parent : '#base').children();
  1179. for(var i = 0; i < children.length; i++) {
  1180. var child = $(children[i]);
  1181. var childId = child.attr('id');
  1182. // Don't move self, and check id to make sure it is a widget.
  1183. if(childId == id || !childId || childId[0] != 'u') continue;
  1184. var numbers = childId.substring(1).split('-');
  1185. if(numbers.length < 1 || isNaN(Number(numbers[0])) || (numbers.length == 2 && isNaN(Number(numbers[1]))) || numbers.length > 2) continue;
  1186. var markerProp = vert ? 'top' : 'left';
  1187. var marker = Number(child.css(markerProp).replace('px', ''));
  1188. var childClamp = [Number(child.css(clampProp).replace('px', ''))];
  1189. // Dynamic panels are not reporting correct size sometimes, so pull it from the state. Get shown state just returns the widget if it is not a dynamic panel.
  1190. var sizeChild = _getShownState(childId);
  1191. childClamp[1] = childClamp[0] + sizeChild[clampOffset]();
  1192. if(isNaN(marker) || isNaN(childClamp[0]) || isNaN(childClamp[1]) ||
  1193. marker < threshold || childClamp[1] <= clamp[0] || childClamp[0] >= clamp[1]) continue;
  1194. marker += delta;
  1195. var props = {};
  1196. props[markerProp] = marker;
  1197. if(easing == 'none') child.css(props);
  1198. else child.animate(props, duration, easing);
  1199. }
  1200. };
  1201. var _parentHandlesStyles = function(id) {
  1202. var parents = $ax('#' + id).getParents(true)[0];
  1203. if(!parents) return false;
  1204. var directParent = true;
  1205. for(var i = 0; i < parents.length; i++) {
  1206. var parentId = parents[i];
  1207. // Because state parent panel id is the active state, must strip off the end of it.
  1208. var itemId = $ax.repeater.getItemIdFromElementId(parentId);
  1209. parentId = parentId.split('_')[0];
  1210. if(itemId) parentId = $ax.repeater.createElementId(parentId, itemId);
  1211. var parentObj = $obj(parentId);
  1212. if(parentObj.type != 'dynamicPanel') continue;
  1213. if(!parentObj.propagate) {
  1214. directParent = false;
  1215. continue;
  1216. }
  1217. return { id: parentId, direct: directParent };
  1218. }
  1219. return false;
  1220. };
  1221. _dynamicPanelManager.parentHandlesStyles = _parentHandlesStyles;
  1222. var _propagateMouseOver = function(id, value) {
  1223. propagate(id, true, value);
  1224. };
  1225. _dynamicPanelManager.propagateMouseOver = _propagateMouseOver;
  1226. var _propagateMouseDown = function(id, value) {
  1227. propagate(id, false, value);
  1228. };
  1229. _dynamicPanelManager.propagateMouseDown = _propagateMouseDown;
  1230. var propagate = function(id, hover, value) {
  1231. var hoverChildren = function(children) {
  1232. if(!children) return;
  1233. for(var i = 0; i < children.length; i++) {
  1234. var elementId = children[i].id;
  1235. var obj = $obj(elementId);
  1236. if(obj.type == 'dynamicPanel' && !obj.propagate) continue;
  1237. if(hover) $ax.style.SetWidgetHover(elementId, value);
  1238. else $ax.style.SetWidgetMouseDown(elementId, value);
  1239. $ax.annotation.updateLinkLocations($ax.style.GetTextIdFromShape(elementId));
  1240. hoverChildren(children[i].children);
  1241. }
  1242. };
  1243. hoverChildren($ax('#' + id).getChildren(true)[0].children);
  1244. };
  1245. });