canvas.js 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. /* Contains all the Javascript logic of the canvas and its main features: save, export and share */
  2. $(document).ready(function() {
  3. /* ================================================
  4. Miscellaneous
  5. ================================================= */
  6. // Declare the currently loaded tags belonging to the user
  7. var tags;
  8. // Declare current user
  9. var username = $("input[name='username']").val();
  10. // Fixes bug when added item field zooms on tag click
  11. var tagWindowIsOpen = false;
  12. // This piece of code is for making the column-count and column-gap CSS to work in Firefox
  13. /*
  14. document.getElementById("7-5-col-layout").style.MozColumnCount = "7";
  15. document.getElementById("2-col-layout").style.MozColumnCount = "2";
  16. */
  17. // Prevent pressing ENTER on Project Title from submitting the form
  18. $('.proj_title').keydown(function(event){
  19. if(event.keyCode == 13) {
  20. event.preventDefault();
  21. return false;
  22. }
  23. });
  24. /* ================================================
  25. Rearrange fields numerically if 1 column is displayed
  26. ================================================= */
  27. // Declarations
  28. var groupOneLayout = $("#7-5-col-layout");
  29. var groupTwoLayout = $("#4-col-layout");
  30. var field01 = $("#panel_01");
  31. var field03 = $("#panel_03");
  32. var field04 = $("#panel_04");
  33. var field09 = $("#panel_09");
  34. var field05 = $("#panel_05");
  35. var field06 = $("#panel_06");
  36. var field02 = $("#panel_02");
  37. var field07 = $("#panel_07");
  38. var field08 = $("#panel_08");
  39. // var field10 = $("#panel_10");
  40. var isRearranged = false;
  41. // Rearrange the fields to suit mobile
  42. function rearrangeFields() {
  43. field01.detach();
  44. field03.detach();
  45. field04.detach();
  46. field09.detach();
  47. field05.detach();
  48. field06.detach();
  49. field02.detach();
  50. field07.detach();
  51. field08.detach();
  52. // field10.detach();
  53. groupOneLayout.append(field01);
  54. groupOneLayout.append(field02);
  55. groupOneLayout.append(field03);
  56. groupOneLayout.append(field04);
  57. groupOneLayout.append(field05);
  58. groupOneLayout.append(field06);
  59. groupOneLayout.append(field07);
  60. groupOneLayout.append(field08);
  61. groupOneLayout.append(field09);
  62. // groupOneLayout.append(field10);
  63. }
  64. // Rearrange the fields according to their original order
  65. function rearrangeFieldsOriginal() {
  66. field01.detach();
  67. field02.detach();
  68. field03.detach();
  69. field04.detach();
  70. field05.detach();
  71. field06.detach();
  72. field07.detach();
  73. field08.detach();
  74. field09.detach();
  75. // field10.detach();
  76. groupOneLayout.append(field01);
  77. groupOneLayout.append(field03);
  78. groupOneLayout.append(field04);
  79. groupOneLayout.append(field09);
  80. groupOneLayout.append(field05);
  81. groupOneLayout.append(field06);
  82. groupOneLayout.append(field02);
  83. groupTwoLayout.append(field07);
  84. groupTwoLayout.append(field08);
  85. // groupTwoLayout.append(field10);
  86. }
  87. // If the web page is opened on a mobile
  88. if ($(window).width() <= 499) {
  89. rearrangeFields();
  90. }
  91. // When resizing the window
  92. $(window).on("resize", function() {
  93. if (isRearranged === false && $(window).width() <= 499) {
  94. rearrangeFields();
  95. isRearranged = true;
  96. }
  97. else if(isRearranged === true && $(window).width() >= 500) {
  98. rearrangeFieldsOriginal();
  99. isRearranged = false;
  100. }
  101. });
  102. /* ================================================
  103. Collaborative features
  104. ================================================= */
  105. /* ================================================
  106. Collaborators window
  107. ================================================= */
  108. // When the user clicks on the "Collaborators" button, show the collaborators window
  109. $("div.collaborators button").on("click", function() {
  110. // Show the collaborators window
  111. $("div#shadow").css("display", "block");
  112. $("div#collaborators-window").css("display", "block");
  113. });
  114. // Close the collaborators window
  115. function closeCollaboratorsWindow() {
  116. // Close the collaborators window
  117. $("div#shadow").css("display", "none");
  118. $("div#collaborators-window").css("display", "none");
  119. }
  120. // When the user clicks on the "Close" button
  121. $("div#collaborators-window button.close").on("click", function() {
  122. // Close the collaborators window
  123. closeCollaboratorsWindow();
  124. });
  125. // When the user checks the "Show only active collaborators" checkbox
  126. $("input[name='show-only-active']").on("change", function() {
  127. // If the user only wants to see active collaborators
  128. if($(this).is(":checked")) {
  129. // For every collaborator
  130. $("div#collaborators-window tr").each(function() {
  131. // If this collaborator is offline
  132. if($(this).find("td:nth-child(1)").html() === "") {
  133. // Hide this collaborator
  134. $(this).css("display", "none")
  135. }
  136. });
  137. }
  138. // If the user only wants to all active collaborators
  139. else {
  140. // For every collaborator
  141. $("div#collaborators-window tr").each(function() {
  142. // Show this collaborator
  143. $(this).css("display", "table-row");
  144. });
  145. }
  146. });
  147. /* ================================================
  148. "Jump to" functions
  149. ================================================= */
  150. var hasScrolledDown = false;
  151. function showFixedJumpedTo() {
  152. // Add classes
  153. $("div.jump-to-click-area").addClass("jump-to-click-area-toggle");
  154. $("div.jump-to").addClass("jump-to-toggle");
  155. $("div.jump-to-list").addClass("jump-to-list-toggle");
  156. $("img.jump-to-img").addClass("jump-to-img-toggle");
  157. // If the user clicks on a link and scrolls up the page, hide list
  158. $("div.jump-to-list").hide();
  159. }
  160. function showInitialJumpedTo() {
  161. // Toggle classes
  162. $("div.jump-to-click-area").removeClass("jump-to-click-area-toggle");
  163. $("div.jump-to").removeClass("jump-to-toggle");
  164. $("div.jump-to-list").removeClass("jump-to-list-toggle");
  165. $("img.jump-to-img").removeClass("jump-to-img-toggle");
  166. // If the user clicks on a link and scrolls up the page, toggle list
  167. $("div.jump-to-list").show();
  168. hasScrolledDown = false;
  169. }
  170. // When the user scrolls down, change "position" to "fixed"
  171. $(window).scroll(function() {
  172. // If the web page is opened on a mobile
  173. if($(window).width() <= 499) {
  174. // Declarations
  175. var scrollPosition = $(window).scrollTop();
  176. var jumpToPosition = $("div.jump-to").offset().top;
  177. // If the user has scrolled down to "Jump To..."
  178. if(hasScrolledDown === false && scrollPosition >= jumpToPosition) {
  179. showFixedJumpedTo();
  180. // Update scroll position
  181. $(window).scrollTop($("div.saved-tags").offset().top - 300);
  182. hasScrolledDown = true;
  183. }
  184. // If the user has scrolled up to the top
  185. else if(hasScrolledDown === true && scrollPosition === 0) {
  186. showInitialJumpedTo();
  187. }
  188. }
  189. // If the web page is not open on a mobile
  190. else {
  191. showInitialJumpedTo();
  192. }
  193. });
  194. // If the user clicks on "Jump to", show menu
  195. $("div.jump-to-click-area").on("click", function() {
  196. // Toggle the menu
  197. $("div.jump-to div div").slideToggle(300);
  198. // Rotate arrow
  199. // $("div.jump-to span.jump-to-arrow").toggleClass("rotate-arrow-180");
  200. // $("div.jump-to span.jump-to-arrow").toggleClass("rotate-arrow-0");
  201. $("img.jump-to-img").toggleClass("jump-to-arrow-90");
  202. $("img.jump-to-img").toggleClass("jump-to-arrow-0");
  203. return false;
  204. });
  205. // If the user clicks on a menu item
  206. $("div.jump-to ul a").on("click", function() {
  207. // Declarations
  208. var chosenLiIndex = $(this).parent().index();
  209. var chosenFieldPosition;
  210. var scrollPositionNew;
  211. // Detect the scroll position of the chosen field
  212. // If the user has chosen the list item 0
  213. if(chosenLiIndex === 0) {
  214. chosenFieldPosition = $("div.saved-tags").offset().top;
  215. }
  216. // If the user has chosen the list item 1-9
  217. else if(chosenLiIndex >= 1 && chosenLiIndex <= 9) {
  218. chosenFieldPosition = $("div.field_0" + chosenLiIndex).offset().top;
  219. }
  220. // If the user has chosen the list item 10 or higher
  221. else {
  222. chosenFieldPosition = $("div.field_" + chosenLiIndex).offset().top;
  223. }
  224. // If the user hasn't scrolled down
  225. if(hasScrolledDown === false) {
  226. console.log(chosenLiIndex);
  227. // Set the new scroll position
  228. scrollPositionNew = chosenFieldPosition - $("div.jump-to").height() - 89;
  229. // Add classes
  230. showFixedJumpedTo();
  231. // Update scroll position
  232. hasScrolledDown = true;
  233. }
  234. // If the user has scrolled down to "Jump To..."
  235. else {
  236. // Set the new scroll position
  237. scrollPositionNew = chosenFieldPosition - 58;
  238. // Toggle the menu
  239. $("div.jump-to-list").slideToggle(300);
  240. // Rotate arrow
  241. // $("div.jump-to span.jump-to-arrow").toggleClass("rotate-arrow-90");
  242. // $("div.jump-to span.jump-to-arrow").toggleClass("rotate-arrow-0");
  243. $("img.jump-to-img").toggleClass("jump-to-arrow-90");
  244. $("img.jump-to-img").toggleClass("jump-to-arrow-0");
  245. }
  246. // Apply the new scroll position
  247. $(window).scrollTop(scrollPositionNew);
  248. return false;
  249. });
  250. /* ================================================
  251. Show tooltip for every category
  252. ================================================= */
  253. $("a[data-toggle='tooltip']").tooltip({container: "body"});
  254. $("a[data-toggle='tooltip']").on("click", function() {
  255. return false;
  256. });
  257. /* ================================================
  258. Remove all tags from all fields
  259. ================================================= */
  260. // Remove all tags from all fields
  261. function removeTags() {
  262. // AJAX
  263. $.ajax({
  264. type: "POST",
  265. url: "php/get-tags.php",
  266. dataType: "JSON",
  267. data: {
  268. "username": username
  269. },
  270. timeout: 5000,
  271. success: function(returnData) {
  272. // Declarations
  273. var loopCounter = 0;
  274. tags = returnData;
  275. // For every added item
  276. $("li.added_item .expandable").each(function() {
  277. var thisDiv = $(this); // Delete the declaration (because it's ugly!)
  278. // For every tag in the database that belongs to the active user
  279. for(t in tags) {
  280. // If the current tag exists in the textarea
  281. if(thisDiv.html().indexOf("</a>") != -1) { // Why not a while loop?
  282. // Delete the tag
  283. var text = thisDiv.html();
  284. text = thisDiv.text().replace("<a class='tag'>" + tags[loopCounter] + "</a>", tags[loopCounter]);
  285. thisDiv.html(text);
  286. }
  287. loopCounter++;
  288. }
  289. });
  290. if(tags.length === 0) {
  291. $("div.saved-tags p").html("You haven't added any tags yet.");
  292. }
  293. },
  294. error: function(xhr) {
  295. console.log(xhr.statusText);
  296. }
  297. });
  298. /*
  299. // The code below is much better than the code above, because no database connection is being called, but the "You haven't added any tags yet" text is not being reset
  300. // Declarations
  301. var loopCounter = 0;
  302. // For every added item
  303. $("li.added_item .expandable").each(function() {
  304. var thisDiv = $(this); // Delete the declaration (because it's ugly)
  305. // For every tag that belongs to the active user
  306. for(t in tags) {
  307. // If the current tag exists in the textarea
  308. if(thisDiv.html().indexOf("</a>") != -1) {
  309. // Delete the tag
  310. var text = thisDiv.html();
  311. text = thisDiv.text().replace("<a class='tag'>" + tags[loopCounter] + "</a>", tags[loopCounter]);
  312. thisDiv.html(text);
  313. }
  314. loopCounter++;
  315. }
  316. });
  317. console.log(tags);
  318. console.log(tags.length);
  319. if(tags.length === 0) {
  320. $("div.saved-tags p").html("You haven't added any tags yet.");
  321. }
  322. */
  323. }
  324. /* ================================================
  325. Get the user's tags from the database and apply them on the canvas
  326. ================================================= */
  327. // Apply tags (Incoming parameter is an array with every tag from the database belonging to the active user)
  328. function updateTags() {
  329. // Apply tags on the canvas
  330. /*
  331. IMPORTANT! A timer was needed in order to solve several bugs related to tags not being applied on the canvas.
  332. Should this still become an issue later on, try increasing the amount of milliseconds.
  333. */
  334. window.setTimeout(function() {
  335. // For every added item
  336. $("li.added_item .expandable").each(function() {
  337. // Declarations
  338. // var instanceCounter;
  339. var loopCounter = 0;
  340. var text = $(this).text();
  341. // instanceCounter = $(text + ":contains('test')").length;
  342. // $('#parentDiv').find(":contains('No Records Found')").length
  343. // console.log(instanceCounter);
  344. // For every tag in the database that belongs to the active user
  345. for(t in tags) {
  346. // instanceCounter = ($(this).text().match(/aaa/g) || []).length;
  347. // Apply the tag for every instance of the current term
  348. // instanceCounter = text.indexOf(tags[loopCounter]);
  349. // while(instanceCounter > -1) {
  350. // text = text.replace(tags[loopCounter], "<a class='tag' contenteditable='false'>" + tags[loopCounter] + "</a>");
  351. text = text.replace(tags[loopCounter], "<a class='tag'>" + tags[loopCounter] + "</a>");
  352. // instanceCounter = text.indexOf(tags[loopCounter], instanceCounter + 1);
  353. // }
  354. loopCounter++;
  355. }
  356. $(this).html(text);
  357. });
  358. // Show the tag window on tag click
  359. showTagWindowOnTagClick();
  360. }, 100);
  361. // Populate "Saved Tags"
  362. var loopCounter = 0;
  363. var savedTags = $("div.saved-tags p");
  364. savedTags.html("");
  365. for(t in tags) {
  366. if(savedTags.html() === "You haven't added any tags yet.") {
  367. savedTags.html("");
  368. }
  369. savedTags.append("<a href='#' class='tag'>" + tags[loopCounter] + "</a>").append("&nbsp;&nbsp; ");
  370. loopCounter++;
  371. }
  372. }
  373. // Get the tags from the database
  374. function getTags() {
  375. // Declarations
  376. // AJAX
  377. $.ajax({
  378. type: "POST",
  379. url: "php/get-tags.php",
  380. dataType: "JSON",
  381. data: {
  382. "username": username
  383. },
  384. timeout: 5000,
  385. success: function(returnData) {
  386. if(returnData.length > 0) {
  387. // Assign returnData to the tags variable
  388. tags = returnData;
  389. // Update tags on the canvas
  390. updateTags();
  391. }
  392. },
  393. error: function(xhr) {
  394. console.log(xhr.statusText);
  395. }
  396. });
  397. }
  398. /* ================================================
  399. Load tags if the user is logged in
  400. ================================================= */
  401. if($("input[name='name']").val() != "") {
  402. getTags();
  403. }
  404. /* ================================================
  405. Tag window functions
  406. ================================================= */
  407. // Declarations
  408. // var textarea = $("div#tag-window textarea#tag-description");
  409. var textareaHasBeenChanged = false;
  410. var tag;
  411. var tagIsNew = false;
  412. var username = $("input[name='username']").val();
  413. // Check if the tag is new
  414. function checkIfTagIsNew() {
  415. // Declarations
  416. var tagToAJAX = tag;
  417. // AJAX
  418. $.ajax({
  419. type: "POST",
  420. url: "php/check-if-tag-exists.php",
  421. dataType: "text",
  422. data: {
  423. "tag": tagToAJAX,
  424. "username": username
  425. },
  426. timeout: 5000,
  427. success: function(returnData) {
  428. // If the current tag does not exist in the database
  429. if(returnData === "") {
  430. // Hide "Delete tag" button
  431. $("div#tag-window button#delete-tag").css("display", "none");
  432. // Hide "Similar tags"
  433. $("div#tag-window h2.similar-tags").css("display", "none");
  434. // The tag is new
  435. tagIsNew = true;
  436. }
  437. // If the current tag exists in the database
  438. else {
  439. // Show "Delete tag" button
  440. $("div#tag-window button#delete-tag").css("display", "inline");
  441. // Show "Similar tags"
  442. $("div#tag-window h2.similar-tags").css("display", "block");
  443. }
  444. },
  445. error: function(xhr) {
  446. console.log(xhr.statusText);
  447. }
  448. });
  449. }
  450. // Updating the remaining characters information
  451. function updateRemainingCharacters() {
  452. // Declarations
  453. var length = $("div#tag-window textarea#tag-description").val().length; // Retrieving from "Please enter a description..."
  454. var maxLength = 200;
  455. /*
  456. var length;
  457. if(description != "") {
  458. length = description;
  459. }
  460. else {
  461. length = $("div#tag-window textarea#tag-description").val().length;
  462. }
  463. */
  464. // Update length
  465. length = maxLength - length;
  466. // Show the remaining characters
  467. $("div#tag-window span.chars").text(length);
  468. }
  469. // Get the description for the selected tag
  470. function getDescriptionForSelectedTag() {
  471. // Declarations
  472. var tagToAJAX = tag;
  473. // AJAX
  474. $.ajax({
  475. type: "POST",
  476. url: "php/get-description-for-selected-tag.php",
  477. dataType: "text",
  478. data: {
  479. "tag": tagToAJAX,
  480. "username": username
  481. },
  482. timeout: 5000,
  483. success: function(returnData) {
  484. if(returnData != "") {
  485. // Update description field with the description for the selected tag
  486. $("div#tag-window textarea#tag-description").val(returnData);
  487. // Update remaining characters
  488. updateRemainingCharacters();
  489. }
  490. textareaHasBeenChanged = true;
  491. },
  492. error: function(xhr) {
  493. console.log(xhr.statusText);
  494. }
  495. });
  496. }
  497. // Get similar tags by other users
  498. function getSimilarTags() {
  499. // Declarations
  500. var tagToAJAX = tag;
  501. // AJAX
  502. $.ajax({
  503. type: "POST",
  504. url: "php/get-similar-tags.php",
  505. dataType: "JSON",
  506. data: {
  507. "tag": tagToAJAX,
  508. "username": username
  509. },
  510. timeout: 5000,
  511. success: function(returnData) {
  512. // Reset similar tags if similar tags are to be showed again
  513. $("p.similar-tags-tag").remove();
  514. $("p.similar-tags-description").remove();
  515. $("p.similar-tags-username").remove();
  516. // Declarations
  517. var htmlToAppend = "";
  518. // If similar tags exist
  519. if(returnData.length > 0) {
  520. // Declarations
  521. var index = 0;
  522. // While there still are tags to append
  523. while(index < returnData.length) {
  524. htmlToAppend += "<p class='similar-tags-tag'><a class='tag'>" + returnData[index]["tag"] + "</a></p>"
  525. + "<p class='similar-tags-description'>" + returnData[index]["description"] + "</p>"
  526. + "<p class='similar-tags-username'>by <a href='#'>" + returnData[index]["username"] + "</a></p>";
  527. index++;
  528. }
  529. // Append tags
  530. $("div#tag-window h2.similar-tags").after(htmlToAppend);
  531. $("p.similar-tags-description-none").css("display", "none");
  532. // Update tags on the canvas
  533. // updateTags();
  534. }
  535. else {
  536. // If the tag isn't a new tag
  537. if(tagIsNew === false) {
  538. // Show the message saying that there are no similar tags to show
  539. $("p.similar-tags-description-none").css("display", "block");
  540. }
  541. }
  542. },
  543. error: function(xhr) {
  544. console.log(xhr.statusText);
  545. }
  546. });
  547. }
  548. // Close the tag window
  549. function closeTagWindow() {
  550. tagWindowIsOpen = false;
  551. $("div#shadow").css("display", "none");
  552. $("div#tag-window").css("display", "none");
  553. }
  554. // Show the tag window
  555. function showTagWindow() {
  556. // Show the tag window
  557. $("div#shadow").css("display", "block");
  558. $("div#tag-window").css("display", "block");
  559. // Move the window to the centre of the screen
  560. /*
  561. The code below should be working, but the height of the second selector seems completely wrong!
  562. var middle = (document.body.clientHeight / 2) - ($("div#tag-window").height() / 2);
  563. $("div#tag-window").css("top", middle);
  564. */
  565. // Check if the tag is new
  566. checkIfTagIsNew();
  567. // Get the description for the selected tag
  568. getDescriptionForSelectedTag();
  569. // Get tags by other users from the database
  570. // window.setTimeout(getSimilarTags(), 100);
  571. getSimilarTags();
  572. // Update remaining characters in case description is loaded
  573. updateRemainingCharacters();
  574. // If a description hasn't been entered
  575. if($("div#tag-window textarea#tag-description") !== "Please enter a description..." && textareaHasBeenChanged !== true) {
  576. // Reset the remaining characters information
  577. $("div#tag-window span.chars").text("200");
  578. }
  579. }
  580. // If the user presses a key in the description textarea
  581. $("div#tag-window textarea#tag-description").on("keyup", function() {
  582. // Update remaining characters
  583. updateRemainingCharacters();
  584. });
  585. // If the user clicks on the "Save tag" button
  586. $("div#tag-window #save-tag").on("click", function() {
  587. // Declarations
  588. var description = $("div#tag-window textarea#tag-description").val();
  589. var tagToAJAX = tag;
  590. // If a description has been entered
  591. if(description != "" && description != "Please enter a description...") {
  592. // AJAX
  593. $.ajax({
  594. timeout: 5000,
  595. dataType: "text",
  596. type: "post",
  597. data: {
  598. "tag": tagToAJAX,
  599. "description": description,
  600. "username": username
  601. },
  602. url: "php/save-tag.php",
  603. success: function() {
  604. textareaHasBeenChanged = true;
  605. // Get the user's tags from the database and apply them on the canvas
  606. getTags();
  607. },
  608. error: function(xhr) {
  609. console.log(xhr.statusText);
  610. }
  611. });
  612. // Close the tag window
  613. closeTagWindow();
  614. }
  615. tagIsNew = false;
  616. });
  617. // If the user clicks on the "Close" button
  618. $("div#tag-window button.close").on("click", function() {
  619. // Close the tag window
  620. closeTagWindow();
  621. });
  622. // If the user clicks on the "Delete tag" button
  623. $("div#tag-window #delete-tag").on("click", function() {
  624. // Declarations
  625. var tagToAJAX = tag;
  626. // AJAX
  627. $.ajax({
  628. timeout: 5000,
  629. dataType: "text",
  630. type: "post",
  631. data: {
  632. "tag": tagToAJAX,
  633. "username": username
  634. },
  635. url: "php/delete-tag.php",
  636. success: function() {
  637. // Remove all tags from all fields
  638. removeTags();
  639. // Get the user's tags from the database and apply them on the canvas
  640. getTags();
  641. },
  642. error: function(xhr) {
  643. console.log(xhr.statusText);
  644. }
  645. });
  646. // Close the tag window
  647. closeTagWindow();
  648. });
  649. // If the user focuses in the textarea
  650. $("div#tag-window textarea#tag-description").on("focusin", function() {
  651. // Declarations
  652. var description = $("div#tag-window textarea#tag-description").val();
  653. // If a description hasn't been entered
  654. // if(textareaHasBeenChanged === false) {
  655. if(description === "Please enter a description...") {
  656. // Empty the description textarea
  657. $("div#tag-window textarea#tag-description").val("");
  658. }
  659. });
  660. // If the user focuses out the textarea
  661. $("div#tag-window textarea#tag-description").on("focusout", function() {
  662. // Declarations
  663. var description = $("div#tag-window textarea#tag-description").val();
  664. // If a description hasn't been entered
  665. // if(textareaHasBeenChanged === false) {
  666. if(description === "") {
  667. // Reset the description textarea
  668. $("div#tag-window textarea#tag-description").val("Please enter a description...");
  669. }
  670. });
  671. /* ================================================
  672. If the user clicks on a tag, show the tag window
  673. ================================================= */
  674. // If the user clicks on a tag
  675. function showTagWindowOnTagClick() {
  676. $("a.tag").on("click", function() {
  677. // Declarations
  678. tag = $(this).text();
  679. // If the user is logged in
  680. if($("input[name='username']").val() != "") {
  681. tagWindowIsOpen = true;
  682. // Show the tag window
  683. showTagWindow();
  684. }
  685. else {
  686. // Show a dialog
  687. $("div#shadow").css("display", "block");
  688. $("div#dialog-log-in").css("display", "block");
  689. }
  690. });
  691. }
  692. /*
  693. // Mouse-enabled devices
  694. $("tag").mouseenter(function() {
  695. timer = setTimeout(function() {
  696. showTagWindow(); // showTagWindow needs a parameter!
  697. }, 400);
  698. }).mouseleave(function() {
  699. clearTimeout(timer);
  700. });
  701. // Touch devices
  702. try {
  703. document.createEvent("TouchEvent");
  704. $("a.tag").on("click", showTagWindow()); // showTagWindow needs a parameter!
  705. return true;
  706. }
  707. catch(e) {
  708. return false;
  709. }
  710. */
  711. /* ================================================
  712. Save added idea on Enter press
  713. ================================================= */
  714. function saveAddedIdeaOnEnterPress(li) {
  715. $("li.added_item textarea.expandable").on("keydown", function(event) {
  716. if(event.which === 13) {
  717. event.preventDefault();
  718. // Declarations
  719. var li = $(this).parent();
  720. var textElement;
  721. var textElementWidth;
  722. // Replace textarea with a div
  723. $(this).replaceWith(
  724. "<div class='expandable' name='" + $(this).attr("name") + "'>" + $(this).val() + "</div>"
  725. );
  726. // Remember the text element
  727. textElement = li.find(".expandable");
  728. // Remember the width of the text element
  729. textElementWidth = textElement.width();
  730. // Update tags on the canvas
  731. // window.setTimeout(updateTags(), 100);
  732. updateTags();
  733. // Zoom out animation
  734. if(textElementWidth >= 0 && textElementWidth <= 250) {
  735. textElement.addClass("zoom-out-regular");
  736. }
  737. else if(textElementWidth >= 251 && textElementWidth <= 500) {
  738. textElement.addClass("zoom-out-less");
  739. }
  740. else if(textElementWidth >= 501) {
  741. textElement.addClass("zoom-out-least");
  742. }
  743. textElement.css("background-color", "rgba(255, 255, 255, 0.75)");
  744. textElement.css("-ms-transform", "scale(1)");
  745. textElement.css("-webkit-transform", "scale(1)");
  746. textElement.css("transform", "scale(1)");
  747. toggleTextElementOnFocus();
  748. }
  749. });
  750. }
  751. /* ================================================
  752. Toggle textarea.expandable/div.expandable on edit
  753. ================================================= */
  754. function toggleTextElementOnFocus() {
  755. // If the user clicks on the added idea
  756. $("li.added_item .expandable").on("click", function() {
  757. if(tagWindowIsOpen === false) {
  758. var li = $(this).parent();
  759. var textElement;
  760. var textElementHTML;
  761. var textElementWidth;
  762. // Replace div with a textarea
  763. $(this).replaceWith(
  764. "<textarea maxlength='100' class='expandable' rows='3' data-limit-rows='true' data-autoresize name='" + $(this).attr("name") + "'>" + $(this).text() + "</textarea>"
  765. );
  766. // Remember the text element
  767. textElement = li.find(".expandable");
  768. // Fix the bug that tags still might appear
  769. textElementHTML = textElement.html();
  770. while(textElementHTML.indexOf("<a class='tag'>") != -1) {
  771. textElementHTML = textElementHTML.replace("<a class='tag'>", "");
  772. textElementHTML = textElementHTML.replace("</a>", "");
  773. }
  774. textElement.html(textElementHTML);
  775. // Remember the width of the text element
  776. textElementWidth = textElement.width();
  777. // Focus immediately on click instead of having to click on the element a second time
  778. textElement.focus();
  779. // Zoom in animation
  780. if(textElementWidth >= 0 && textElementWidth <= 250) {
  781. textElement.addClass("zoom-in-regular");
  782. textElement.css("background-color", "rgba(255, 255, 255, 1)");
  783. textElement.css("-ms-transform", "scale(1.02)");
  784. textElement.css("-webkit-transform", "scale(1.02)");
  785. textElement.css("transform", "scale(1.02)");
  786. }
  787. else if(textElementWidth >= 251 && textElementWidth <= 600) {
  788. textElement.addClass("zoom-in-less");
  789. textElement.css("background-color", "rgba(255, 255, 255, 1)");
  790. textElement.css("-ms-transform", "scale(1.01)");
  791. textElement.css("-webkit-transform", "scale(1.01)");
  792. textElement.css("transform", "scale(1.01)");
  793. }
  794. else if(textElementWidth >= 601) {
  795. textElement.addClass("zoom-in-least");
  796. textElement.css("background-color", "rgba(255, 255, 255, 1)");
  797. textElement.css("-ms-transform", "scale(1.005)");
  798. textElement.css("-webkit-transform", "scale(1.005)");
  799. textElement.css("transform", "scale(1.005)");
  800. }
  801. // Save added idea on Enter press
  802. saveAddedIdeaOnEnterPress(li);
  803. // Replace textarea with a div
  804. $("li.added_item textarea.expandable").on("focusout", function() {
  805. // Declarations
  806. var li = $(this).parent();
  807. var textElement;
  808. var textElementWidth;
  809. // Replace textarea with a div
  810. $(this).replaceWith(
  811. "<div class='expandable' name='" + $(this).attr("name") + "'>" + $(this).val() + "</div>"
  812. );
  813. // Remember the text element
  814. textElement = li.find(".expandable");
  815. // Remember the width of the text element
  816. textElementWidth = textElement.width();
  817. // Update tags on the canvas
  818. // window.setTimeout(updateTags(), 100);
  819. updateTags();
  820. // Zoom out animation
  821. if(textElementWidth >= 0 && textElementWidth <= 250) {
  822. textElement.addClass("zoom-out-regular");
  823. }
  824. else if(textElementWidth >= 251 && textElementWidth <= 500) {
  825. textElement.addClass("zoom-out-less");
  826. }
  827. else if(textElementWidth >= 501) {
  828. textElement.addClass("zoom-out-least");
  829. }
  830. textElement.css("background-color", "rgba(255, 255, 255, 0.75)");
  831. textElement.css("-ms-transform", "scale(1)");
  832. textElement.css("-webkit-transform", "scale(1)");
  833. textElement.css("transform", "scale(1)");
  834. toggleTextElementOnFocus();
  835. });
  836. }
  837. });
  838. }
  839. /*
  840. // ================================================
  841. If the user types a new character in the added idea field, add tag if a term could be found
  842. ================================================= //
  843. // 8an33j24
  844. function applyTagOnTypeMatch() {
  845. $("li.added_item .expandable").on("focusin", function() {
  846. // If the user presses a key in the description textarea
  847. $("li.added_item .expandable").on("keyup", function() {
  848. if(event.which !== 9 && // Tab
  849. event.which !== 16 && // Shift
  850. event.which !== 37 && // Left
  851. event.which !== 38 && // Up
  852. event.which !== 39 && // Right
  853. event.which !== 40) { // Down
  854. // Declarations
  855. var bugCounter = 0;
  856. var loopCounter = 0;
  857. var tagLinkToSearch;
  858. var tagLinkHits;
  859. var tagTextToSearch;
  860. var tagTextHits;
  861. var text = $(this).html();
  862. // Fix the bug that triggers the event twice
  863. if(!bugCounter > 0) {
  864. // console.log("tagTextHits: " + tagTextHits);
  865. // console.log("tagLinkHits: " + tagLinkHits);
  866. // For every tag that belongs to the active user
  867. for(t in tags) {
  868. if($(this).html().indexOf(tags[loopCounter]) != -1) {
  869. // Search for the tag inside the added idea as a link
  870. tagLinkToSearch = text.match(tags[loopCounter] + "</a>"); // match() can only count to one instance (67hi9nt3)
  871. // Search for the tag inside the added idea as plain text
  872. tagTextToSearch = text.match(tags[loopCounter]); // match() can only count to one instance (67hi9nt3)
  873. // If the tag exists inside the added idea as a link
  874. if(tagLinkToSearch !== null) {
  875. // tagLinkHits = 1
  876. tagLinkHits = tagLinkToSearch.length;
  877. }
  878. else {
  879. // tagLinkHits = 0
  880. tagLinkHits = 0;
  881. }
  882. // If the tag exists inside the added idea as plain text
  883. if(tagTextToSearch !== null) {
  884. // tagTextHits = 1
  885. tagTextHits = tagTextToSearch.length;
  886. }
  887. else {
  888. // tagTextHits = 0
  889. tagTextHits = 0;
  890. }
  891. // If there are more instances of the tag as plain text than the tag as links, and there is a maximum of 1 instances of the tag as plain text
  892. // Change "tagTextHits <= 1" to "tagLinkHits === 0"
  893. if(tagTextHits > tagLinkHits && tagLinkHits === 0) {
  894. // Apply the tag
  895. text = text.replace(tags[loopCounter], "<a class='tag' contenteditable='false'>" + tags[loopCounter] + "</a>");
  896. $(this).html(text);
  897. // Restore caret position // u78krf3t
  898. }
  899. }
  900. loopCounter++;
  901. }
  902. }
  903. bugCounter++;
  904. }
  905. });
  906. });
  907. };
  908. */
  909. /* ----------------------------------------------
  910. Limiting the number of characters the user is allowed to type
  911. ----------------------------------------------- */
  912. var maxLength = 100;
  913. $('.card').on('keyup', '.new_item', function() {
  914. var length = $(this).val().length;
  915. length = maxLength - length;
  916. // show the characters remaining only on this field
  917. $(this).closest('.user-input').find('.chars').text(length);
  918. });
  919. function limitLengthOnInput() {
  920. // Limit text on key press
  921. $("li.added_item .expandable").on("keypress", function(event) {
  922. // Declarations
  923. var numberOfTags = $(this).children().filter("a").length;
  924. var textLength = $(this).html().length;
  925. // Subtract 43 from textLength per tag (the tag HTML is 43 characters)
  926. $(this).each(function() {
  927. textLength -= 43 * numberOfTags;
  928. });
  929. if(textLength === 100) { // Windows menu/Right cmd
  930. event.preventDefault();
  931. }
  932. });
  933. // 28jek79t
  934. // Limit text on paste
  935. /*
  936. var pastedTextOriginal;
  937. $("li.added_item .expandable").on("paste", function(event) {
  938. pastedTextOriginal = event.originalEvent.clipboardData.getData("Text");
  939. if($(this).html().length + pastedTextOriginal > 100) {
  940. console.log("Too long!");
  941. event.preventDefault();
  942. // var caretPosition = the text position
  943. // Insert the text after caretPosition
  944. }
  945. // return false;
  946. });
  947. */
  948. }
  949. /* ================================================
  950. If the user closes a dialog
  951. ================================================= */
  952. $("div.dialog button").on("click", function() {
  953. $("div#shadow").css("display", "none");
  954. $("div.dialog").css("display", "none");
  955. });
  956. /* ================================================
  957. If the user clicks on the "Tag selected term" link
  958. ================================================= */
  959. // Declaration
  960. var selection = "";
  961. // var newRange = "";
  962. // Initiate tag
  963. tag = "";
  964. // Update the tag variable
  965. document.onselectionchange = function() {
  966. // if($("textarea").is(":focus")) {
  967. if($("li.added_item .expandable").is(":focus")) {
  968. selection = window.getSelection().toString();
  969. tag = selection.trim();
  970. }
  971. }
  972. // If the user clicks on the "Tag selected term" link
  973. $("p.tag-selected-term a").on("click", function() {
  974. // If the tag isn't empty
  975. if(tag != "") {
  976. // Show the tag window
  977. showTagWindow();
  978. }
  979. else {
  980. // Show a dialog
  981. $("div#shadow").css("display", "block");
  982. $("div#dialog-select-term").css("display", "block");
  983. }
  984. // Prevent the current view to jump to the top of the screen
  985. return false;
  986. });
  987. // If the user moves the focus from the added idea, reset variables
  988. $("li.added_item .expandable").on("focusout", function() {
  989. selection = "";
  990. tag = "";
  991. });
  992. /* ================================================
  993. Serialize Form to JSON
  994. ================================================= */
  995. $.fn.serializeObject = function() {
  996. var o = {};
  997. var a = this.serializeArray();
  998. $.each(a, function() {
  999. if (o[this.name]) {
  1000. if (!o[this.name].push) {
  1001. o[this.name] = [o[this.name]];
  1002. }
  1003. o[this.name].push(this.value || '');
  1004. } else {
  1005. o[this.name] = this.value || '';
  1006. }
  1007. });
  1008. return o;
  1009. };
  1010. /* ================================================
  1011. Getting the current date
  1012. ================================================= */
  1013. var fullDate = new Date();
  1014. var twoDigitMonth = fullDate.getMonth() + "";
  1015. if (twoDigitMonth.length == 1) twoDigitMonth = "0" + twoDigitMonth;
  1016. var twoDigitDate = fullDate.getDate() + "";
  1017. if (twoDigitDate.length == 1) twoDigitDate = "0" + twoDigitDate;
  1018. var currentDate = fullDate.getFullYear() + "-" + twoDigitMonth + "-" + twoDigitDate;
  1019. // set the currebt date in the date input field
  1020. $('.proj_date').val(currentDate);
  1021. /* ================================================
  1022. USER LOGS OUT (dropdown menu)
  1023. ================================================= */
  1024. $('.user-profile').on('click', '#logout', function() {
  1025. var url = 'php/logout.php';
  1026. $.post(url, function(data, status) {
  1027. if (data == 200) {
  1028. $('.user-profile').hide();
  1029. $('.invited-members').hide();
  1030. window.location.href="https://www.ethicscanvas.org";
  1031. }
  1032. });
  1033. });
  1034. /* ================================================
  1035. When the page loads, Import the chosen canvas if the user has picked one from the dashbord,
  1036. otherwise load an empty canvas
  1037. ================================================= */
  1038. // if a canvas is chosen by the user to be loaded
  1039. if (current_canvas_id !== '') {
  1040. var url = 'json/' + current_canvas_id + '.json';
  1041. // var url= 'json/test_canvas.json';
  1042. // get the saved ISON object in the sendJSON.text file
  1043. $.getJSON(url, function(returnedObj) {
  1044. // Display the json data in the html
  1045. var itemListHTML = '';
  1046. // iterate through the object
  1047. $.each(returnedObj, function(key, value) {
  1048. // project name and tem field
  1049. if (key === 'field_00[]') {
  1050. $('.form-header').find('input.proj_title').val(value[
  1051. 0]);
  1052. $('.form-header').find('input.proj_date').val(value[1]);
  1053. }
  1054. else if (key !== 'new_item') {
  1055. if ($.type(value) === "array") {
  1056. $.each(value, function(i, itm) {
  1057. /** FIX DUPLICATIONs in the canvas when importing
  1058. /* Importing will override the canvas content
  1059. clear the canvas by giving en emty content to the ul list (remove previous list items) */
  1060. $('.canvas-form').find('.card').filter('.' + key.substr(0, 8)).find('ul.item_list').html('');
  1061. /* Create a list item with each value item
  1062. and give it text area with the name attribute as the "key" (right field name) */
  1063. itemListHTML +=
  1064. // '<li class="added_item"><span class="handle glyphicon glyphicon-move"></span><textarea maxlength="100" class="expandable" rows="3" data-limit-rows="true" data-autoresize name="' + key + '">' + itm + '</textarea><span class="remove glyphicon glyphicon-remove-circle"></span></li>';
  1065. '<li class="added_item"><div class="expandable" name="' + key + '">' + itm + '</div><br /><span class="handle glyphicon glyphicon-list"></span><span class="move-up glyphicon glyphicon-chevron-up"></span><span class="move-down glyphicon glyphicon-chevron-down"></span><span class="remove glyphicon glyphicon-remove-circle"></span></li>';
  1066. });
  1067. }
  1068. else { // a single value string
  1069. itemListHTML +=
  1070. // '<li class="added_item"><span class="handle glyphicon glyphicon-move"></span><textarea maxlength="100" class="expandable" rows="3" data-limit-rows="true" data-autoresize name="' + key + '">' + value + '</textarea><span class="remove glyphicon glyphicon-remove-circle"></span></li>';
  1071. '<li class="added_item"><div class="expandable" name="' + key + '">' + value + '</div><br /><span class="handle glyphicon glyphicon-list"></span><span class="move-up glyphicon glyphicon-chevron-up"></span><span class="move-down glyphicon glyphicon-chevron-down"></span><span class="remove glyphicon glyphicon-remove-circle"></span></li>';
  1072. }
  1073. /* Append the created list items/textatreas to the right field based on the "key"*/
  1074. /* the str.substr(start,length)
  1075. is used to remove the [] from the end of the "key"name (for each field. also the name attributes accociated with each fiels) so that we can select the right class (right field) and append the created lists to the right field
  1076. so field names/key/name attribute will tuen into class names: ex: field_1[] becomes field_1
  1077. */
  1078. // find the field by its class names besed on the current key name
  1079. // append the created list of item values to that right field
  1080. $('.canvas-form').find('.card').filter('.' + key.substr(0, 8)).find('ul.item_list').append(itemListHTML);
  1081. /*$('form').find('.card').filter('.field_1').find('ul.item_list').append(itemListHTML); */
  1082. /* !! Empty the item list after each count of "key" so that the previous item lists from the other fields (associated with the previous key) don't get added up to the item list of other fields */
  1083. itemListHTML = '';
  1084. }
  1085. });
  1086. });
  1087. // Toggle textarea.expandable/div.expandable on edit
  1088. toggleTextElementOnFocus();
  1089. // Get the user's tags from the database and apply them on the canvas
  1090. getTags();
  1091. // Show "Tag selected term" link
  1092. if($(this).parent().parent().parent().prev().prev().children().length > 0) {
  1093. $(this).parent().parent().parent().prev().css("display", "block");
  1094. }
  1095. // Fix the heights after importing
  1096. // fixHeights();
  1097. }
  1098. /* ================================================
  1099. Toggle the introduction text in fields
  1100. ================================================= */
  1101. //$(selector).toggle(speed,easing,callback)
  1102. $('.card').on('click', '.intro-toggle', function() {
  1103. var $TogglingText = $($(this).closest('.card').find('.intro'));
  1104. var $Toggler = $($(this).closest('.card').find('.intro-toggle'));
  1105. $TogglingText.toggle('slow', function() {
  1106. // Do this when toggling:
  1107. // the boolean .is(':visible') of the current toggle state
  1108. if ($TogglingText.is(':visible')) {
  1109. // change the text of the toggle
  1110. $Toggler.find('.intro-toggle-text').text('Hide description');
  1111. // change the icon of the toggle
  1112. $Toggler.find('.intro-toggle-icon').switchClass("glyphicon-plus-sign", "glyphicon-minus-sign", 1000, "easeInOutQuad");
  1113. }
  1114. else {
  1115. $Toggler.find('.intro-toggle-text').text('Show description');
  1116. $Toggler.find('.intro-toggle-icon').switchClass("glyphicon-minus-sign", "glyphicon-plus-sign", 1000, "easeInOutQuad");
  1117. }
  1118. });
  1119. });
  1120. /* ================================================
  1121. Auto expand user input textareas
  1122. ================================================= */
  1123. /* Works for textareas already exsting in the html when the page loads -> User input
  1124. $.each($('textarea[data-autoresize]'), function() {
  1125. var offset = this.offsetHeight - this.clientHeight;
  1126. var resizeTextarea = function(el) {
  1127. $(el).css('height', 'auto').css('height', el.scrollHeight + offset);
  1128. };
  1129. $(this).on('keyup input', function() {
  1130. resizeTextarea(this);
  1131. }).removeAttr('data-autoresize');
  1132. });
  1133. /* ================================================
  1134. Limiting the number of lines in textareas
  1135. ================================================= */
  1136. // <textarea data-limit-rows="true" cols="60" rows="8"></textarea>
  1137. /*
  1138. $('.card').on('keypress', 'textarea[data-limit-rows=true]', function(event) {
  1139. var textarea = $(this),
  1140. text = textarea.val(),
  1141. // match() -> Searches a string for a match against a regular expression, and returns the matches, as an Array object.
  1142. numberOfLines = (text.match(/\n/g) || []).length + 1,
  1143. maxRows = parseInt(textarea.attr('rows'));
  1144. // if the number of lines have reached the max rows
  1145. if (numberOfLines === maxRows) {
  1146. return false;
  1147. }
  1148. });
  1149. */
  1150. /* ================================================
  1151. Handling user input, ADD items
  1152. A. Add button
  1153. B. Clicking enter
  1154. ================================================= */
  1155. /* ----------------------------------------------
  1156. add new idea slide effect
  1157. ----------------------------------------------- */
  1158. // When clicking on "add a new idea", Slide down and show the input field for adding a new item (from the begining, it is set to display:hidden with CSS). If clicked again, slide it up. After that, set the textarea in automatic focus
  1159. $('.card').on('click', 'a.add-idea', function(event) {
  1160. // stop the default behavior of the link (jumping back to the start of the page)
  1161. event.preventDefault();
  1162. // set the textarea automatically in focus
  1163. $(this).closest('.card').find('.user-input').slideToggle("slow", function() {
  1164. // When the toggle animation is complete:
  1165. // set the text area in focus
  1166. $(this).closest('.card').find('.new_item').val('');
  1167. $(this).closest('.card').find('.chars').text(maxLength);
  1168. $(this).closest('.card').find('.new_item').focus();
  1169. });
  1170. });
  1171. /* ----------------------------------------------
  1172. A. When we click the add btn to
  1173. add the item to the list
  1174. ----------------------------------------------- */
  1175. // event deligation to handle the present and future elements that are dynamically added
  1176. $('.card').on('click', '.add_btn', function() {
  1177. var new_item = $(this).closest('.card').find('.new_item').val();
  1178. var new_item_height = $(this).closest('.card').find('.new_item').height();
  1179. // number of items are in the list
  1180. var fieldItemCount = $(this).closest('.card').find('ul.item_list').find('li').length;
  1181. // new item added, increment the number of items
  1182. fieldItemCount++;
  1183. // add the input value as a textarea item
  1184. /* create a name attribute in the "field_nr[]" format to be able to tag each new item with the right field attr name (based on the field they are added to). This is to format the json file for each group of dynamically added items. We get the name attribute directly from the id attribute of the ul list in each card (the one we pressed the add button in) */
  1185. // if the new item input exist (is not empty), add the item
  1186. if (new_item) {
  1187. var field_attr = $(this).closest('.card').find('ul.item_list').attr('id') + '[]';
  1188. // The height of the newly added item = the height of the add new idea textarea
  1189. $(this).closest('.card').find('ul.item_list').append(
  1190. // '<li class="added_item"><span class="handle glyphicon glyphicon-move"></span><textarea maxlength="100" class="expandable" rows="3" data-limit-rows="true" data-autoresize name="' + field_attr + '">' + new_item + '</textarea><span class="remove glyphicon glyphicon-remove-circle"></span></li>'
  1191. '<li class="added_item"><div class="expandable" name="' + field_attr + '">' + new_item + '</div><br /><span class="handle glyphicon glyphicon-list"></span><span class="move-up glyphicon glyphicon-chevron-up"></span><span class="move-down glyphicon glyphicon-chevron-down"></span><span class="remove glyphicon glyphicon-remove-circle"></span></li>'
  1192. );
  1193. // Fix the heights only after a new item is added
  1194. // fixHeights();
  1195. }
  1196. // clear the new item the text area value
  1197. $(this).closest('.card').find('.new_item').val('');
  1198. /* When clicking on "add idea", hide the input field for adding a new item (slideUp() doesn't work nicely here)*/
  1199. $(this).closest('.card').find('.user-input').hide("fast", function() {
  1200. // Animation complete.
  1201. });
  1202. // Limit the text in the idea field if it reaches the max length
  1203. limitLengthOnInput();
  1204. // Apply event listener to the added idea handling the application of tags should the user writes one of the terms in it
  1205. // applyTagOnTypeMatch();
  1206. // Toggle textarea.expandable/div.expandable on edit
  1207. toggleTextElementOnFocus();
  1208. // Remove all tags from all fields
  1209. removeTags();
  1210. // Get the user's tags from the database and apply them on the canvas
  1211. getTags();
  1212. // Show "Tag selected term" link
  1213. if($(this).parent().parent().prev().prev().children().length > 0) {
  1214. $(this).parent().parent().prev().css("display", "block");
  1215. }
  1216. });
  1217. /* ----------------------------------------------
  1218. B. Clicking enter in the add idea textarea,
  1219. will add the new item to the card
  1220. ----------------------------------------------- */
  1221. $('.card').on('keypress', 'textarea[data-limit-rows=true]', function(event) {
  1222. var textarea = $(this);
  1223. var text = textarea.val();
  1224. /* The jQuery event.which -->
  1225. Returns which keyboard key was pressed: */
  1226. // if the enter is pressed, event.which === 13
  1227. if (event.which === 13 && !$("li.added_item .expandable").is(":focus")) {
  1228. var new_item = $(this).closest('.card').find('.new_item').val();
  1229. var new_item_height = $(this).closest('.card').find('.new_item').height();
  1230. // number of items are in the list
  1231. var fieldItemCount = $(this).closest('.card').find('ul.item_list')
  1232. .find('li').length;
  1233. // new item added, increment the number of items
  1234. fieldItemCount++;
  1235. // add the input value as a textarea item
  1236. /* create a name attribute in the "field_nr[]" format to be able to tag each new item with the right field attr name (based on the field they are added to). This is to format the json file for each group of dynamically added items. We get the name attribute directly from the id attribute of the ul list in each card (the one we pressed the add button in) */
  1237. // if the new item input exist (is not empty), add the item
  1238. if (new_item) {
  1239. var field_attr = $(this).closest('.card').find('ul.item_list').attr(
  1240. 'id') + '[]';
  1241. // The height of the newly added item = the height of the add new idea textarea
  1242. $(this).closest('.card').find('ul.item_list').append(
  1243. // '<li class="added_item"><span class="handle glyphicon glyphicon-move"></span><textarea maxlength="100" class="expandable" rows="3" data-limit-rows="true" data-autoresize name="' + field_attr + '">' + new_item + '</textarea><span class="remove glyphicon glyphicon-remove-circle"></span></li>');
  1244. '<li class="added_item"><div class="expandable" name="' + field_attr + '">' + new_item + '</div><br /><span class="handle glyphicon glyphicon-list"></span><span class="move-up glyphicon glyphicon-chevron-up"></span><span class="move-down glyphicon glyphicon-chevron-down"></span><span class="remove glyphicon glyphicon-remove-circle"></span></li>'
  1245. );
  1246. // Fix the heights only after a new item is added
  1247. // fixHeights();
  1248. }
  1249. // clear the new item the text area value
  1250. $(this).closest('.card').find('.new_item').val('');
  1251. /* When clicking on "add idea", hide the input field for adding a new item (slideUp() doesn't work nicely here)*/
  1252. $(this).closest('.card').find('.user-input').hide("fast", function() {
  1253. // Animation complete.
  1254. });
  1255. // Limit the text in the idea field if it reaches the max length
  1256. limitLengthOnInput();
  1257. // Apply event listener to the added idea handling the application of tags should the user writes one of the terms in it
  1258. // applyTagOnTypeMatch();
  1259. // Toggle textarea.expandable/div.expandable on edit
  1260. toggleTextElementOnFocus();
  1261. // Remove all tags from all fields
  1262. removeTags();
  1263. // Get the user's tags from the database and apply them on the canvas
  1264. getTags();
  1265. // Show "Tag selected term" link
  1266. if($(this).parent().parent().parent().prev().prev().children().length > 0) {
  1267. $(this).parent().parent().parent().prev().css("display", "block");
  1268. }
  1269. }
  1270. });
  1271. /* ================================================
  1272. Moving ideas to a different category
  1273. ================================================= */
  1274. // Declarations
  1275. // var categoryUl = $("div#move-window ul");
  1276. var categoryDestination;
  1277. var categoryLis = $("div#move-window ul").html();
  1278. var categoryAs = $("div#move-window ul a");
  1279. var categoryOrigin;
  1280. var ideaLi;
  1281. var linkLi;
  1282. var linkText;
  1283. // Remove the link to the current category, as the idea cannot be moved to its own category where it already is
  1284. function removeLinkToOriginCategory(text) {
  1285. // If the number of the category is between 10-99
  1286. if(text.substring(6, 7) != "0") {
  1287. categoryOrigin = text.substring(6, 9) - 1;
  1288. }
  1289. // If the number of the category is between 0-9
  1290. else {
  1291. categoryOrigin = text.substring(7, 9) - 1;
  1292. }
  1293. // Remove "a" tag
  1294. linkLi = $("div#move-window ul li").get(categoryOrigin);
  1295. linkText = categoryAs.get(categoryOrigin).innerHTML;
  1296. linkLi.innerHTML = linkText;
  1297. }
  1298. // When the user clicks on the "Move" icon, show the move window
  1299. $(".card").on("click", "span.handle", function() {
  1300. // Show the move window
  1301. $("div#shadow").css("display", "block");
  1302. $("div#move-window").css("display", "block");
  1303. // Assign the idea that is going to be moved
  1304. ideaLi = $(this).parent();
  1305. // Remove the link to the current category, as the idea cannot be moved to its own category where it already is
  1306. removeLinkToOriginCategory($(this).parent().parent().attr("id"));
  1307. });
  1308. // Close the move window
  1309. function closeMoveWindow() {
  1310. // Close the move window
  1311. $("div#shadow").css("display", "none");
  1312. $("div#move-window").css("display", "none");
  1313. }
  1314. // When the user clicks on the "Close" button
  1315. $("div#move-window button.close").on("click", function() {
  1316. // Close the move window
  1317. closeMoveWindow();
  1318. });
  1319. // When the user clicks on a category, move the idea
  1320. function moveIdeaOnClick() {
  1321. $("div#move-window ul a").on("click", function() {
  1322. // If the number of the category is between 10-99
  1323. if($(this).parent().text().trim().substring(1, 2) != ".") {
  1324. categoryDestination = $(this).parent().text().trim().substring(0, 2);
  1325. }
  1326. // If the number of the category is between 0-9
  1327. else {
  1328. categoryDestination = $(this).parent().text().trim().substring(0, 1);
  1329. }
  1330. // Move the idea
  1331. // If the number of the category is between 10-99
  1332. if(categoryDestination > 9) {
  1333. $("ul#field_" + categoryDestination).append(ideaLi);
  1334. // $("ul#field_0" + (categoryOrigin + 1)).parent().find("p.tag-selected-term").css("display", "none");
  1335. }
  1336. // If the number of the category is between 0-9
  1337. else {
  1338. $("ul#field_0" + categoryDestination).append(ideaLi);
  1339. // $("ul#field_0" + (categoryOrigin + 1)).parent().find("p.tag-selected-term").css("display", "none");
  1340. }
  1341. // Close the move window
  1342. closeMoveWindow();
  1343. // Restore links in the move window
  1344. if($("div#move-window li").length > $("div#move-window a").length) {
  1345. $("div#move-window ul").children().remove()
  1346. $("div#move-window ul").append(categoryLis);
  1347. moveIdeaOnClick();
  1348. }
  1349. // Hide "Tag selected term" link in the origin category if there are no ideas left
  1350. if($("ul#field_0" + (categoryOrigin + 1)).children().length === 0) {
  1351. // If the number of the category is between 10-99
  1352. if((categoryOrigin + 1) > 9) {
  1353. $("ul#field_" + (categoryOrigin + 1)).parent().find("p.tag-selected-term").css("display", "none");
  1354. }
  1355. // If the number of the category is between 0-9
  1356. else {
  1357. $("ul#field_0" + (categoryOrigin + 1)).parent().find("p.tag-selected-term").css("display", "none");
  1358. }
  1359. }
  1360. // Show "Tag selected term" link in the destination category
  1361. // If the number of the category is between 10-99
  1362. if((categoryOrigin + 1) > 9) {
  1363. $("ul#field_" + categoryDestination).parent().find("p.tag-selected-term").css("display", "block");
  1364. }
  1365. // If the number of the category is between 0-9
  1366. else {
  1367. $("ul#field_0" + categoryDestination).parent().find("p.tag-selected-term").css("display", "block");
  1368. }
  1369. });
  1370. }
  1371. moveIdeaOnClick();
  1372. /* ================================================
  1373. Moving ideas up
  1374. ================================================= */
  1375. // When the user clicks on the "Up" button
  1376. $('.card').on('click', 'span.move-up', function() {
  1377. // Declarations
  1378. var ideaLiCurrent = $(this).parent();
  1379. var ideaLiPrevious = $(this).parent().prev();
  1380. // Move the idea
  1381. if(ideaLiPrevious.is("li")) {
  1382. ideaLiCurrent.detach();
  1383. ideaLiPrevious.before(ideaLiCurrent);
  1384. }
  1385. });
  1386. /* ================================================
  1387. Moving ideas down
  1388. ================================================= */
  1389. // When the user clicks on the "Down" button
  1390. $('.card').on('click', 'span.move-down', function() {
  1391. // Declarations
  1392. var ideaLiCurrent = $(this).parent();
  1393. var ideaLiNext = $(this).parent().next();
  1394. // Move the idea
  1395. if(ideaLiNext.is("li")) {
  1396. ideaLiCurrent.detach();
  1397. ideaLiNext.after(ideaLiCurrent);
  1398. }
  1399. });
  1400. /* ================================================
  1401. Deleting ideas
  1402. ================================================= */
  1403. // when the cross beside the textarea is clicked (span.remove)
  1404. // remove that list item
  1405. $('.card').on('click', 'span.remove', function() {
  1406. // If there are no ideas left, hide "Tag selected term" link
  1407. if($(this).prev().parent().parent().children().length === 1) {
  1408. $(this).prev().parent().parent().next().css("display", "none");
  1409. }
  1410. // Remove the list item
  1411. $(this).closest('li').remove();
  1412. });
  1413. /* ================================================
  1414. Sortable field ideas
  1415. ================================================= */
  1416. // make items sortable in their fields and between fields
  1417. /*
  1418. $('.sortable').sortable({
  1419. connectWith: '.connectedList',
  1420. placeholder: "sort-placeholder",
  1421. // revert: true
  1422. zIndex: 300 // Or greater than any other relative/absolute/fixed elements and droppables
  1423. });
  1424. */
  1425. /* ================================================
  1426. Sorting and Dragging events
  1427. ================================================= */
  1428. /* sortstart
  1429. sortover
  1430. sortstop */
  1431. // Every textarea in a item needs to get the right name attribute once they have been dropped in another field (so it ends up in the right place in the json file)
  1432. /*
  1433. // Dragging starts
  1434. $(".sortable").on("sortstart", function(event, ui) {
  1435. // WHEN WE SORT CARDS, $(this) ---> the "begining" ul with the class of .sortable
  1436. });
  1437. // Dragging ends: item dropped
  1438. $(".sortable").on("sortstop", function(event, ui) {
  1439. // get the id of the field ul (to set the name attribute of textareas)
  1440. // # mouseleave is the right event for when we release and leave a card mouseup doesn't work properly in this case
  1441. $('.card').on('mouseleave', 'li', function() {
  1442. //$(selector).attr(attribute,value)
  1443. var fieldAttr = $(this).closest('ul.item_list').attr('id');
  1444. // $(this).find('textarea').attr('name', fieldAttr + '[]');
  1445. $(this).find('li.added_item .expandable').attr('name', fieldAttr + '[]');
  1446. });
  1447. });
  1448. */
  1449. /* ================================================
  1450. SAVING THE CANVAS:
  1451. CLICK ON #EXPORT JSON# form button
  1452. ================================================= */
  1453. /* ----------------------------------------------
  1454. When the user clicks on the SAVE CANVAS button
  1455. ----------------------------------------------- */
  1456. $('.canvas-form').on('click', '.json_exp', function() {
  1457. /* ----------------------------------------------
  1458. A: saving the canvas
  1459. as a registered user
  1460. ----------------------------------------------- */
  1461. // php variables are retieved in the header of the canvas index.php as js variables -->
  1462. var name_save_canvas = $('.form-header').find('.proj_title').val();
  1463. var date_save_canvas = $('.form-header').find('.proj_date').val();
  1464. var save_canvas_obj = {
  1465. 'email_save_canvas': email_save_canvas,
  1466. 'name_save_canvas': name_save_canvas,
  1467. 'date_save_canvas': date_save_canvas
  1468. };
  1469. var save_canvas = $.param(save_canvas_obj);
  1470. /* Post the JSON stringified object to the php file
  1471. (the php script will save it in a .json file )*/
  1472. var save_reg_url = "php/save-canvas.php";
  1473. $.post(save_reg_url, { save_canvas: save_canvas }, function(data, status) {
  1474. //the returned data is successful, is the $canvas_id
  1475. var canvas_id = data;
  1476. // send this canvas_id with the next ajax requestedto the php/canvas.php file and use it as the name of the json file to be saved
  1477. // Give the user feedback that the canvas has been saved
  1478. if (data !== 400 || data !== 401) {
  1479. if ($('.imp-exp-btn ').find(".save-canvas-feedback") !== null) {
  1480. $('.imp-exp-btn ').find(".save-canvas-feedback").remove();
  1481. }
  1482. $('.canvas-form').find('.imp-exp-btn ').append('<div class="save-canvas-feedback"><p><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>Your canvas has been saved in your dashbord</p></div>');
  1483. // remove the canvas is saves message as soon as user changes the canvas
  1484. // $('.canvas-form').on("change keyup", 'textarea', function() {
  1485. $('.canvas-form').on("change keyup", '.expandable', function() {
  1486. $('.imp-exp-btn ').find(".save-canvas-feedback").remove();
  1487. });
  1488. } else {
  1489. $('.canvas-form').find('.imp-exp-btn ').append('<div class="save-canvas-feedback-fail"><p>Oh! We could not save your canvas. Please try again or contact us at hello@ethicscanvas.org</p></div>');
  1490. }
  1491. // For the second AJAX request:
  1492. /* ----------------------------------------------
  1493. B: Exporing the form data json to a file
  1494. and save it on the server
  1495. ----------------------------------------------- */
  1496. // $('#result').text(JSON.stringify($('.canvas-form').serializeObject()));
  1497. // Make the JSON object into a JSON string
  1498. var JSONstrObj = JSON.stringify($('.canvas-form').serializeObject());
  1499. var url = "php/canvas.php";
  1500. /* Post the JSON stringified object to the php file
  1501. (the php script will save it in a .json file )*/
  1502. // also, send the canvas_id and use it for naming the file
  1503. $.post(url, {
  1504. JSONstrObj: JSONstrObj,
  1505. canvas_id: canvas_id
  1506. }, function(data, status) {
  1507. console.log(
  1508. 'Response from php when sending the form json object: \n' +
  1509. 'data:' + data + '\n status: ' + status);
  1510. }).fail(function(jqXHR) {
  1511. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  1512. });
  1513. }).fail(function(jqXHR) {
  1514. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  1515. });
  1516. // Prevent the card item list from reseting itself after clicking on the export button (submission). Because the type of the button is submit
  1517. return false;
  1518. });
  1519. /* ================================================
  1520. HANDLING CLICK ON : Share This Canvas BUTTON
  1521. ================================================= */
  1522. $('.canvas-form').on('click', '.share_canvas', function() {
  1523. $('.share_canvas_email').slideDown(1000, function() {
  1524. // SAVE THE PDF AS file
  1525. $.post('mpdf/canvas-pdf-save.php', function(data, status) { }); // save pdf as file
  1526. });
  1527. });
  1528. $('.canvas-form').on('click', '.share_canvas_send', function() {
  1529. var share_email = $('.share_canvas_email').find('input').serialize();
  1530. // This sends a serialized array share_email to the php file. Example: the value of this array will be: share-canvas-email=eternalflame.f%40gmail.com
  1531. $.post('php/share-canvas.php', {
  1532. share_email: share_email
  1533. }, function(data, status) {
  1534. if (data == 200) { // canvas successfully shared
  1535. $('.canvas-form').find('.imp-exp-btn ').append('<div class="save-canvas-feedback"><p><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>Your canvas has been shared by email</p></div>')
  1536. }
  1537. else {
  1538. $('.canvas-form').find('.imp-exp-btn ').append('<div class="save-canvas-feedback"><p><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>Your canvas could not be shared by email</p></div>')
  1539. }
  1540. });
  1541. // slide up the .share_canvas_email area
  1542. $('.share_canvas_email').slideUp();
  1543. });
  1544. /* ================================================
  1545. Controlling the height of divs dynamically
  1546. ================================================= */
  1547. // Call this function after adding a new item and importing
  1548. // $( window ).width(); ->Returns width of browser viewport
  1549. /*
  1550. function fixHeights() {
  1551. // Returns width of browser viewport
  1552. var screenSize = $(window).width();
  1553. //the longest card of the group 1 .masonry-layout7-5
  1554. var longest_1 = $('.masonry-layout7-5').height();
  1555. //the longest card of the group 2 .masonry-layout7-5
  1556. var longest_2 = $('.masonry-layout4').height();
  1557. // --- 5 COL Range ----
  1558. if (screenSize >= 1139) {
  1559. // inforcing a fixed height ".height(longest_1/2)" will create some layout issues when we try to add new items the add item area will go outside the box and the heights don't increase naturally
  1560. // card group 1:
  1561. // $('.field_05,.field_11, .field_07').css('min-height', longest_1 - longest_1 * 5 / 100);
  1562. // $('.field_06,.field_08, .field_12').css('min-height', longest_1 + longest_1 * 5 / 100);
  1563. // $('.field_01, .field_02').css('min-height', longest_1 * 2 + longest_1 * 1 / 100);
  1564. // card group 2:
  1565. // $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 2 - longest_2 * 20 / 100);
  1566. $('.field_05,.field_11, .field_07').css('min-height', longest_1 - longest_1 * 20 / 100);
  1567. $('.field_06,.field_08, .field_12').css('min-height', longest_1 - longest_1 * 20 / 100);
  1568. $('.field_01, .field_02').css('min-height', longest_1 + longest_1 * 40 / 100);
  1569. // card group 2:
  1570. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 - longest_2 * 10 / 100);
  1571. }
  1572. // 4 COL Range //
  1573. else if (screenSize >= 977 && screenSize <= 1138) {
  1574. // card group 1:
  1575. // row 1
  1576. $('.field_01,.field_06, .field_12,.field_08 ').css('min-height', longest_1 + longest_2 * 20 / 100);
  1577. // row 2
  1578. $('.field_05,.field_11, .field_07,.field_02 ').css('min-height', longest_1 + longest_2 * 20 / 100);
  1579. // card group 2:
  1580. // row 3
  1581. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 2 - longest_2 * 20 / 100);
  1582. } else if (screenSize >= 920 && screenSize <= 976) {
  1583. // card group 1:
  1584. // row 1
  1585. $('.field_01,.field_06, .field_12,.field_08 ').css('min-height', longest_1 + longest_2 * 20 / 100);
  1586. // row 2
  1587. $('.field_05,.field_11, .field_07,.field_02 ').css('min-height', longest_1 + longest_2 * 20 / 100);
  1588. // card group 2:
  1589. // row 3
  1590. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 2 - longest_2 * 20 / 100);
  1591. } else if (screenSize >= 485 && screenSize <= 919) {
  1592. // else if (500 <= screenSize < 920) {
  1593. // card group 1:
  1594. $('.field_01,.field_06, .field_12,.field_08 ').css('min-height', longest_1 * 80 / 100);
  1595. $('.field_05,.field_11, .field_07,.field_02 ').css('min-height', longest_1 * 80 / 100);
  1596. // card group 2:
  1597. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 80 / 100);
  1598. // --- 1 COL Range ----
  1599. } else {
  1600. // card group 1:
  1601. $('.field_01,.field_06, .field_12,.field_08 ').css('min-height', longest_1 * 20 / 100);
  1602. $('.field_05,.field_11, .field_07,.field_02 ').css('min-height', longest_1 * 20 / 100);
  1603. // card group 2:
  1604. $('.field_03, .field_09, .field_10, .field_04').css('min-height', longest_2 * 20 / 100);
  1605. }
  1606. }
  1607. function fixHeights() {
  1608. // Returns width of browser viewport
  1609. var screenSize = $(window).width();
  1610. var field01 = $("div.field_01");
  1611. var field02 = $("div.field_02");
  1612. var field03 = $("div.field_03");
  1613. var field04 = $("div.field_04");
  1614. var field05 = $("div.field_05");
  1615. var field06 = $("div.field_06");
  1616. var field07 = $("div.field_07");
  1617. var field08 = $("div.field_08");
  1618. var field09 = $("div.field_09");
  1619. // Set the height of every field
  1620. var heightOfField01 = field01.height();
  1621. var heightOfField03 = field03.height();
  1622. var heightOfField04 = field04.height();
  1623. var heightOfField03And04 = field03.height() + field04.height();
  1624. var heightOfField09 = field09.height();
  1625. var heightOfField05 = field05.height();
  1626. var heightOfField06 = field06.height();
  1627. var heightOfField05And06 = field05.height() + field06.height();
  1628. var heightOfField02 = field02.height();
  1629. // --- 5 COL Range ---
  1630. if (screenSize >= 935) {
  1631. var longestInGroup1 = Math.max(heightOfField01, heightOfField03, heightOfField04, heightOfField03And04, heightOfField09, heightOfField05, heightOfField06, heightOfField05And06, heightOfField02);
  1632. console.log(longestInGroup1);
  1633. // Field group 1
  1634. switch(longestInGroup1) {
  1635. case heightOfField01:
  1636. $(".field_04").css("min-height", longestInGroup1 + 10);
  1637. $(".field_09").css("min-height", longestInGroup1 + 448);
  1638. $(".field_06").css("min-height", longestInGroup1 + 10);
  1639. $(".field_02").css("min-height", longestInGroup1 + 448);
  1640. break;
  1641. case heightOfField03:
  1642. // Code goes here
  1643. break;
  1644. case heightOfField04:
  1645. // Code goes here
  1646. break;
  1647. case heightOfField03And04:
  1648. // Code goes here
  1649. break;
  1650. case heightOfField09:
  1651. // Code goes here
  1652. break;
  1653. case heightOfField05:
  1654. // Code goes here
  1655. break;
  1656. case heightOfField06:
  1657. // Code goes here
  1658. break;
  1659. case heightOfField05And06:
  1660. // Code goes here
  1661. break;
  1662. case heightOfField02:
  1663. // Code goes here
  1664. break;
  1665. default:
  1666. break;
  1667. }
  1668. }
  1669. // --- 4 COL Range ---
  1670. else if (screenSize >= 935 && screenSize <= 991) {
  1671. // Field group 1
  1672. // Code goes here
  1673. // Field group 2
  1674. // Code goes here
  1675. }
  1676. else if (screenSize >= 992 && screenSize <= 1153) {
  1677. // Field group 1
  1678. // Code goes here
  1679. // Field group 2
  1680. // Code goes here
  1681. }
  1682. // --- 2 COL Range ---
  1683. else if (screenSize >= 500 && screenSize <= 934) {
  1684. // Field group 1
  1685. // Code goes here
  1686. // Field group 2
  1687. // Code goes here
  1688. }
  1689. // --- 2-5 COL Range ---
  1690. if (screenSize >= 500) {
  1691. var longestInGroup2;
  1692. // Field group 2
  1693. // Determine the longest Field in group 2
  1694. if(field07.height() < field08.height()) {
  1695. longestInGroup2 = field08;
  1696. }
  1697. else {
  1698. longestInGroup2 = field07;
  1699. }
  1700. if(longestInGroup2 === field07) {
  1701. $(".field_08").css("min-height", longestInGroup2.height() + 10);
  1702. }
  1703. else {
  1704. $(".field_07").css("min-height", longestInGroup2.height() + 10);
  1705. }
  1706. }
  1707. // --- 1 COL Range ---
  1708. // Field group 1
  1709. // Code goes here
  1710. // Field group 2
  1711. // Code goes here
  1712. }
  1713. */
  1714. /*
  1715. new ResizeSensor($(".field_01, .field_02, .field_03, .field_04, .field_05, .field_06, .field_07, .field_08, .field_09"), function() {
  1716. // fixHeights();
  1717. });
  1718. */
  1719. });