canvas.js 91 KB

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