PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/free-jqgrid/4.9.2/plugins/jquery.createcontexmenufromnavigatorbuttons.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 140 lines | 114 code | 5 blank | 21 comment | 31 complexity | 334173d51268c541a77095b430ea1ef4 MD5 | raw file
  1. /**
  2. * Copyright (c) 2015, Dr. Oleg Kiriljuk, oleg.kiriljuk@ok-soft-gmbh.com
  3. * Dual licensed under the MIT and GPL licenses
  4. * http://www.opensource.org/licenses/mit-license.php
  5. * http://www.gnu.org/licenses/gpl-2.0.html
  6. * Date: 2015-04-06
  7. * see the answers http://stackoverflow.com/a/8491939/315935
  8. * and http://stackoverflow.com/a/29048089/315935
  9. * and http://stackoverflow.com/q/29457007/315935
  10. */
  11. /*global jQuery */
  12. (function ($) {
  13. "use strict";
  14. /*global $ */
  15. /*jslint plusplus: true, browser: true, eqeq: true, unparam: true, white: true */
  16. $.jgrid.extend({
  17. createContexMenuFromNavigatorButtons: function (pager, opt) {
  18. var grid = this, menuId = "menu_" + grid[0].id, menuUl = $("<ul>"),
  19. menuDiv = $("<div>").attr("id", menuId),
  20. getSelectedText = function () {
  21. var text = "";
  22. if (window.getSelection) {
  23. text = window.getSelection();
  24. } else if (document.getSelection) {
  25. text = document.getSelection();
  26. } else if (document.selection) {
  27. text = document.selection.createRange().text;
  28. }
  29. return typeof text === "string" ? text : text.toString();
  30. };
  31. menuUl.appendTo(menuDiv);
  32. menuDiv.appendTo("body");
  33. grid.contextMenu(menuId, {
  34. bindings: {}, // the bindings will be created in the onShowMenu
  35. onContextMenu: function (e) {
  36. var p = grid[0].p, i, lastSelId, $target = $(e.target),
  37. rowId = $target.closest("tr.jqgrow").attr("id"),
  38. isInput = $target.is(":text:enabled") ||
  39. $target.is("input[type=textarea]:enabled") ||
  40. $target.is("textarea:enabled");
  41. if (rowId && !isInput && getSelectedText() === "") {
  42. i = $.inArray(rowId, p.selarrrow);
  43. if (p.selrow !== rowId && i < 0) {
  44. // prevent the row from be unselected
  45. // the implementation is for "multiselect:false" which we use,
  46. // but one can easy modify the code for "multiselect:true"
  47. grid.jqGrid("setSelection", rowId);
  48. } else if (p.multiselect) {
  49. // Edit will edit FIRST selected row.
  50. // rowId is allready selected, but can be not the last selected.
  51. // Se we swap rowId with the first element of the array p.selarrrow
  52. lastSelId = p.selarrrow[p.selarrrow.length - 1];
  53. if (i !== p.selarrrow.length - 1) {
  54. p.selarrrow[p.selarrrow.length - 1] = rowId;
  55. p.selarrrow[i] = lastSelId;
  56. p.selrow = rowId;
  57. }
  58. }
  59. return true;
  60. }
  61. return false; // no contex menu
  62. },
  63. onShowMenu: function (e, $menu) {
  64. var options = this, $menuUl = $menu.children("ul").first().empty(),
  65. versionParts = $.ui != null && typeof $.ui.version === "string" ? /^([0-9]+)\.([0-9]+)\.([0-9]+)$/.exec($.ui.version) : [],
  66. isAncorRequired = versionParts != null && versionParts.length === 4 && versionParts[1] === "1" && versionParts[2] < 11;
  67. $(pager).find(".navtable .ui-pg-button").filter(function () {
  68. return !($(this).prop("disabled") || $(this).hasClass("ui-state-disabled"));
  69. }).each(function () {
  70. var $spanIcon, text, $td, id, $li,
  71. $div = $(this).children("div.ui-pg-div").first();
  72. if ($div.length === 1) {
  73. text = $div.children(".ui-pg-button-text").html();
  74. $td = $div.parent();
  75. if ($.trim(text) === "") {
  76. text = $td.attr("title");
  77. }
  78. if (this.id !== "" && text !== "") {
  79. id = "menuitem_" + this.id;
  80. } else {
  81. // for custom buttons
  82. id = $.jgrid.randId();
  83. }
  84. $li = $("<li>").attr("id", id);
  85. $spanIcon = $div.children("span").not(".ui-pg-button-text").first();
  86. if ($spanIcon.length > 0) {
  87. // standard navGrid button or button added by navButtonAdd
  88. if (isAncorRequired) {
  89. $li.append($("<a>")
  90. .html(text)
  91. .prepend(
  92. $spanIcon
  93. .clone()
  94. .removeClass("ui-pg-button-icon-over-text")
  95. .css({
  96. "float": "left",
  97. marginTop: $spanIcon.hasClass("ui-icon") ? "0.25em" : "0.125em",
  98. marginRight: "0.5em"
  99. })
  100. ));
  101. } else {
  102. $li.html(text)
  103. .prepend(
  104. $spanIcon
  105. .clone()
  106. .removeClass("ui-pg-button-icon-over-text")
  107. .css({
  108. "float": "left",
  109. marginTop: $spanIcon.first().hasClass("ui-icon") ? "0.25em" : "0.125em",
  110. marginRight: "0.5em"
  111. })
  112. );
  113. }
  114. if ($div.parent().hasClass("ui-state-active")) {
  115. $li.find("span").addClass("ui-state-active");
  116. }
  117. if ($li.find("select,input").length > 0) {
  118. $li.hide(); // hide custom elements in the menu
  119. }
  120. $menuUl.append($li);
  121. options.bindings[id] = (function ($button) {
  122. return function () {
  123. $button.click();
  124. };
  125. }($div));
  126. }
  127. }
  128. });
  129. $.jgrid.fullBoolFeedback.call(grid, (opt || {}).onShowContextMenu, "jqGridShowContextMenu", $menuUl, options);
  130. return $menu;
  131. }
  132. });
  133. }
  134. });
  135. }(jQuery));