PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/js/contextmenu.js

https://gitlab.com/abhijit13/taasika
JavaScript | 430 lines | 396 code | 34 blank | 0 comment | 99 complexity | 1da82097c854e46f8702dbdf7d469b8b MD5 | raw file
  1. var cutSrcEl = null;
  2. var paste_flag = false;
  3. var cut_or_copy = false; //false -> cut, true -> copy
  4. var clipboard = null; //deep copy of copied element.
  5. $(function () {
  6. $.contextMenu({
  7. selector: '.cell',
  8. className: 'data-title',
  9. autoHide: true,
  10. animation: {
  11. duration: 150,
  12. show: 'slideDown',
  13. hide: 'slideUp'
  14. },
  15. items: {
  16. "cut": {
  17. name: "Cut",
  18. icon: "cut",
  19. disabled: function (itemKey, opt, rootMenu, originalEvent) {
  20. if ((document.getElementById(opt.$trigger.attr("id"))) === cutSrcEl)
  21. return true;
  22. [i, j, k] = makeIJKFromId(opt.$trigger.attr("id"));
  23. if (helperTable[i - 1][j][k] !== null && helperTable[i - 1][j][k] !== 0 && parseInt(helperTable[i - 1][j][k]["isFixed"]) === 0)
  24. return false;
  25. else
  26. return true;
  27. },
  28. callback: function (itemKey, opt, rootMenu, originalEvent) {
  29. if (paste_flag == true) {
  30. contextMenuReset();
  31. }
  32. cutStartHandler(document.getElementById(opt.$trigger.attr("id")));
  33. paste_flag = true;
  34. cut_or_copy = false; //cut
  35. }
  36. },
  37. copy: {
  38. name: "Copy",
  39. icon: "copy",
  40. disabled: function (itemKey, opt, rootMenu, originalEvent) {
  41. if ((document.getElementById(opt.$trigger.attr("id"))) === cutSrcEl)
  42. return true;
  43. [i, j, k] = makeIJKFromId(opt.$trigger.attr("id"));
  44. if (helperTable[i - 1][j][k] !== null && helperTable[i - 1][j][k] !== 0 && parseInt(helperTable[i - 1][j][k]["isFixed"]) === 0)
  45. return false;
  46. else
  47. return true;
  48. },
  49. callback: function (itemKey, opt, rootMenu, originalEvent) {
  50. if (paste_flag == true) {
  51. contextMenuReset();
  52. }
  53. cutStartHandler(document.getElementById(opt.$trigger.attr("id")));
  54. paste_flag = true;
  55. cut_or_copy = true; //copy
  56. }
  57. },
  58. "paste": {
  59. name: "Paste",
  60. icon: "paste",
  61. disabled: function (itemKey, opt, rootMenu, originalEvent) {
  62. if ((document.getElementById(opt.$trigger.attr("id"))) === cutSrcEl)
  63. return true;
  64. if (paste_flag == true) {
  65. var temp;
  66. if (cut_or_copy == false) //cut
  67. temp = cut_pasteCheck(document.getElementById(opt.$trigger.attr("id")));
  68. else if (cut_or_copy == true) //copy
  69. temp = copy_pasteCheck(document.getElementById(opt.$trigger.attr("id")));
  70. console.log(temp);
  71. if (temp === "true") {
  72. $('.data-title').attr('data-menutitle', "Paste Valid");
  73. return false;
  74. } else {
  75. $('.data-title').attr('data-menutitle', "Paste Invalid: " + temp);
  76. return true;
  77. }
  78. } else
  79. return true;
  80. },
  81. callback: function (itemKey, opt, rootMenu, originalEvent) {
  82. if (cut_or_copy == false) {
  83. cut_pasteHandler(document.getElementById(opt.$trigger.attr("id")));
  84. cutEndHandler(document.getElementById(opt.$trigger.attr("id")));
  85. } else if (cut_or_copy == true) {
  86. copy_pasteHandler(document.getElementById(opt.$trigger.attr("id")));
  87. cutEndHandler(document.getElementById(opt.$trigger.attr("id")));
  88. }
  89. }
  90. },
  91. "sep1": "---------",
  92. "delete": {
  93. name: "Delete",
  94. icon: "delete",
  95. disabled: function (itemKey, opt, rootMenu, originalEvent) {
  96. if ((document.getElementById(opt.$trigger.attr("id"))) === cutSrcEl)
  97. return true;
  98. else {
  99. [i, j, k] = makeIJKFromId(opt.$trigger.attr("id"));
  100. if (helperTable[i - 1][j][k] !== null && helperTable[i - 1][j][k] !== 0)
  101. return false;
  102. else
  103. return true;
  104. }
  105. },
  106. callback: function (itemKey, opt, rootMenu, originalEvent) {
  107. deleteEntry(document.getElementById(opt.$trigger.attr("id")));
  108. $('.data-title').attr('data-menutitle', "Menu");
  109. }
  110. },
  111. "sep2": "---------",
  112. "cancel": {
  113. name: "Cancel",
  114. icon: "context-menu-icon context-menu-icon-quit",
  115. disabled: function () {
  116. if (paste_flag === true)
  117. return false;
  118. else
  119. return true;
  120. },
  121. callback: function () {
  122. contextMenuReset();
  123. }
  124. }
  125. }
  126. });
  127. $('.cell').on('click', function (e) {
  128. console.log('clicked', this);
  129. })
  130. $('.data-title').attr('data-menutitle', "Menu");
  131. });
  132. function cutStartHandler(element) {
  133. element.style.opacity = '0.4';
  134. cutSrcEl = element;
  135. [i, j, k] = makeIJKFromId(element.id);
  136. console.log("cutStartHandler: i = " + i + " j = " + j + " k = " + k);
  137. clipboard = jQuery.extend(true, {}, helperTable[i - 1][j][k]);
  138. document.querySelector('#' + makeIdFromIJK(i, j, k) + '.delete').style.pointerEvents = 'none';
  139. }
  140. function temporaryDeleteFromTimetable(i, j, k, helperTableObject) {
  141. if (helperTableObject !== null && helperTableObject !== 0) {
  142. var batchId, classId, teacherId, roomId, isFixed;
  143. var index_arr = [];
  144. var clone_arr = [];
  145. roomId = helperTableObject["roomId"];
  146. teacherId = helperTableObject["teacherId"];
  147. classId = helperTableObject["classId"];
  148. batchId = helperTableObject["batchId"];
  149. isFixed = helperTableObject["isFixed"];
  150. subjectId = helperTableObject["subjectId"];
  151. var subjRow = search(subject, "subjectId", subjectId);
  152. var count = 0;
  153. for (var m = 0; m < subjRow["eachSlot"]; m++) {
  154. var index = searchIndex(timeTable, "day", i, "slotNo", "" + (parseInt(j) + m),
  155. "subjectId", subjectId, "batchId", batchId,
  156. "classId", classId, "snapshotId", currentSnapshotId);
  157. if (index != -1) {
  158. index_arr[count] = index;
  159. clone_arr[count++] = timeTable.splice(index, 1);
  160. } else {
  161. console.log("ERROR: Deleting TT Entry for Paste Checking: Couldn't find index");
  162. }
  163. }
  164. if (batchId !== null && batchId !== "null") {
  165. var sbt = search(subjectBatchTeacher, "subjectId", subjectId,
  166. "batchId", batchId, "teacherId", teacherId);
  167. var osbt;
  168. if (sbt !== -1) {
  169. var osbt = searchMultipleRows(overlappingSBT, "sbtId1", sbt["sbtId"]);
  170. if (osbt !== -1) {
  171. for (var i in osbt) {
  172. var batchId = search(subjectBatchTeacher, "sbtId", osbt[i]["sbtId2"])["batchId"];
  173. var classId = search(batchClass, "batchId", batchId)["classId"];
  174. for (var j = 0; j < subjRow["eachSlot"]; j++) {
  175. var index = searchIndex(timeTable, "day", day, "slotNo", (parseInt(SlotNo) + j),
  176. "subjectId", subjRow["subjectId"], "batchId", batchId,
  177. "classId", classId, "snapshotId", currentSnapshotId);
  178. if (index != -1) {
  179. clone_arr[count] = timeTable.splice(index, 1);
  180. index_arr[count++] = index;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. return {
  188. index: index_arr,
  189. clone: clone_arr
  190. };
  191. }
  192. return null;
  193. }
  194. function temporaryDelete_ReinsertIntoTimetable(indexCloneObj) {
  195. for (var n = 0; n < indexCloneObj.index.length; n++)
  196. timeTable.splice(indexCloneObj.index[n], 0, indexCloneObj.clone[n][0]);
  197. return;
  198. }
  199. function copy_pasteCheck(element) {
  200. [i, j, k] = makeIJKFromId(element.id);
  201. var contentAtIJK = helperTable[i - 1][j][k];
  202. if (contentAtIJK === null)
  203. return subjectCheck(parseInt(i), parseInt(j), parseInt(k));
  204. if (contentAtIJK === 0) {
  205. if (type == "class") {
  206. for (var z = helperTable[0][0].length - 1; z >= 0; z--) {
  207. if (helperTable[i - 1][j][z] === null)
  208. return subjectCheck(parseInt(i), parseInt(j), parseInt(z));
  209. if (helperTable[i - 1][j][z] !== null && helperTable[i - 1][j][z] !== 0) {
  210. k = z;
  211. contentAtIJK = helperTable[i - 1][j][k];
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. if (contentAtIJK !== null && contentAtIJK !== 0) {
  218. var indexCloneObj = temporaryDeleteFromTimetable(parseInt(i), parseInt(j), parseInt(k), contentAtIJK);
  219. var result = subjectCheck(parseInt(i), parseInt(j), parseInt(k));
  220. temporaryDelete_ReinsertIntoTimetable(indexCloneObj);
  221. return result;
  222. }
  223. }
  224. function copy_pasteHandler(element) {
  225. var roomRow = search(room, "roomId", clipboard["roomId"]);
  226. var subjectRow = search(subject, "subjectId", clipboard["subjectId"]);
  227. var teacherId = clipboard["teacherId"];
  228. var classId = clipboard["classId"];
  229. var batchId = clipboard["batchId"];
  230. [i, j, k] = makeIJKFromId(element.getAttribute("id"));
  231. if (helperTable[i - 1][j][k] !== null && helperTable[i - 1][j][k] !== 0)
  232. deleteEntry(element);
  233. [i, j, k] = makeIJKFromId(element.getAttribute("id"));
  234. makeTimeTableEntry(i, j, roomRow["roomId"], classId, subjectRow["subjectId"],
  235. teacherId, batchId, currentSnapshotId, 0, parseInt(subjectRow["eachSlot"]));
  236. var crEntry = new createClassRoomEntry(classId, roomRow["roomId"]);
  237. if (batchId !== null)
  238. var brEntry = new createBatchRoomEntry(batchId, roomRow["roomId"]);
  239. var srEntry = new createSubjectRoomEntry(subjectRow["subjectId"], roomRow["roomId"]);
  240. if (search(classRoom, "classId", classId) === -1)
  241. classRoom.push(crEntry);
  242. if (batchId !== null && search(batchRoom, "batchId", batchId) === -1)
  243. batchRoom.push(brEntry);
  244. if (search(subjectRoom, "subjectId", subjectRow["subjectId"]) === -1)
  245. subjectRoom.push(srEntry);
  246. fillTable2(true);
  247. }
  248. function cut_pasteCheck(element) {
  249. console.log("pasteCheck: Entered ");
  250. if (cutSrcEl === null) {
  251. return copy_pasteCheck(element);
  252. } else if (cutSrcEl !== null) {
  253. [isrc, jsrc, ksrc] = makeIJKFromId(cutSrcEl.id);
  254. isrc = parseInt(isrc);
  255. jsrc = parseInt(jsrc);
  256. ksrc = parseInt(ksrc);
  257. var indexCloneObjSrc = temporaryDeleteFromTimetable(isrc, jsrc, ksrc, helperTable[isrc - 1][jsrc][ksrc]);
  258. var result = copy_pasteCheck(element);
  259. temporaryDelete_ReinsertIntoTimetable(indexCloneObjSrc);
  260. return result;
  261. }
  262. }
  263. function cut_pasteHandler(element) {
  264. console.log("cut_pasteHandler: Entered ");
  265. if(cutSrcEl !== null)
  266. deleteEntry(cutSrcEl);
  267. copy_pasteHandler(element);
  268. }
  269. function cutEndHandler() {
  270. if (cutSrcEl !== null) {
  271. cutSrcEl.style.opacity = 1;
  272. [i, j, k] = makeIJKFromId(cutSrcEl.id);
  273. if (document.querySelector('#' + makeIdFromIJK(i, j, k) + '.delete') !== null)
  274. document.querySelector('#' + makeIdFromIJK(i, j, k) + '.delete').style.pointerEvents = 'auto';
  275. }
  276. cutSrcEl = null;
  277. $('.data-title').attr('data-menutitle', "Menu");
  278. }
  279. function contextMenuReset() {
  280. if (cutSrcEl !== null)
  281. cutEndHandler();
  282. cutSrcEl = null;
  283. paste_flag = false;
  284. clipboard = null;
  285. }
  286. function pasteShortcut() {
  287. if(selectedCell === null)
  288. return;
  289. if(selectedCell === cutSrcEl) {
  290. selectedCell.style.borderColor = "red";
  291. setTimeout(function(){
  292. selectedCell.style.borderColor = "green";
  293. },300);
  294. return;
  295. }
  296. if(paste_flag === true) {
  297. var temp;
  298. if (cut_or_copy == false) { //cut
  299. temp = cut_pasteCheck(selectedCell);
  300. if(temp === "true") {
  301. cut_pasteHandler(selectedCell);
  302. cutEndHandler(selectedCell);
  303. return;
  304. }
  305. }
  306. else if (cut_or_copy == true) { //copy
  307. temp = copy_pasteCheck(selectedCell);
  308. if(temp === "true") {
  309. copy_pasteHandler(selectedCell);
  310. cutEndHandler(selectedCell);
  311. return;
  312. }
  313. }
  314. }
  315. selectedCell.style.borderColor = "red";
  316. setTimeout(function(){
  317. selectedCell.style.borderColor = "green";
  318. },300);
  319. return;
  320. }
  321. function copyShortcut() {
  322. if(selectedCell === null)
  323. return;
  324. if(selectedCell === cutSrcEl) {
  325. selectedCell.style.borderColor = "red";
  326. setTimeout(function(){
  327. selectedCell.style.borderColor = "green";
  328. },300);
  329. return;
  330. }
  331. [i, j, k] = makeIJKFromId(selectedCell.id);
  332. if (helperTable[i - 1][j][k] !== null && helperTable[i - 1][j][k] !== 0 && parseInt(helperTable[i - 1][j][k]["isFixed"]) === 0) {
  333. if (paste_flag == true) {
  334. contextMenuReset();
  335. }
  336. cutStartHandler(selectedCell);
  337. paste_flag = true;
  338. cut_or_copy = true;
  339. }
  340. else{
  341. selectedCell.style.borderColor = "red";
  342. setTimeout(function(){
  343. selectedCell.style.borderColor = "green";
  344. },300);
  345. return;
  346. }
  347. }
  348. function cutShortcut() {
  349. if(selectedCell === null)
  350. return;
  351. if(selectedCell === cutSrcEl) {
  352. selectedCell.style.borderColor = "red";
  353. setTimeout(function(){
  354. selectedCell.style.borderColor = "green";
  355. },300);
  356. return;
  357. }
  358. [i, j, k] = makeIJKFromId(selectedCell.id);
  359. if (helperTable[i - 1][j][k] !== null && helperTable[i - 1][j][k] !== 0 && parseInt(helperTable[i - 1][j][k]["isFixed"]) === 0) {
  360. if (paste_flag == true) {
  361. contextMenuReset();
  362. }
  363. cutStartHandler(selectedCell);
  364. paste_flag = true;
  365. cut_or_copy = false;
  366. }
  367. else{
  368. selectedCell.style.borderColor = "red";
  369. setTimeout(function(){
  370. selectedCell.style.borderColor = "green";
  371. },300);
  372. return;
  373. }
  374. }
  375. function deleteShortcut() {
  376. if(selectedCell === null)
  377. return;
  378. if(selectedCell === cutSrcEl) {
  379. selectedCell.style.borderColor = "red";
  380. setTimeout(function(){
  381. selectedCell.style.borderColor = "green";
  382. },300);
  383. return;
  384. }
  385. [i, j, k] = makeIJKFromId(selectedCell.id);
  386. if (helperTable[i - 1][j][k] !== null && helperTable[i - 1][j][k] !== 0) {
  387. deleteEntry(selectedCell);
  388. }
  389. else{
  390. selectedCell.style.borderColor = "red";
  391. setTimeout(function(){
  392. selectedCell.style.borderColor = "green";
  393. },300);
  394. return;
  395. }
  396. }