canvas.js 74 KB

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