canvas.js 75 KB

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