canvas.js 76 KB

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