canvas.js 78 KB

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