rowlink.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* ============================================================
  2. * Bootstrap: rowlink.js v3.1.3
  3. * http://jasny.github.io/bootstrap/javascript/#rowlink
  4. * ============================================================
  5. * Copyright 2012-2014 Arnold Daniels
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ============================================================ */
  19. +function ($) { "use strict";
  20. var Rowlink = function (element, options) {
  21. this.$element = $(element)
  22. this.options = $.extend({}, Rowlink.DEFAULTS, options)
  23. this.$element.on('click.bs.rowlink mouseup.bs.rowlink', 'td:not(.rowlink-skip)', $.proxy(this.click, this))
  24. }
  25. Rowlink.DEFAULTS = {
  26. target: "a"
  27. }
  28. Rowlink.prototype.click = function(e, ctrlKey) {
  29. var target = $(e.currentTarget).closest('tr').find(this.options.target)[0]
  30. if (typeof target === 'undefined' || $(e.target)[0] === target) return
  31. if (e.type === 'mouseup' && e.which !== 2) return
  32. e.preventDefault();
  33. ctrlKey = ctrlKey || e.ctrlKey || (e.type === 'mouseup' && e.which === 2)
  34. if (!ctrlKey && target.click) {
  35. target.click()
  36. } else if (document.createEvent) {
  37. var evt = new MouseEvent("click", {
  38. view: window,
  39. bubbles: true,
  40. cancelable: true,
  41. ctrlKey: ctrlKey
  42. });
  43. target.dispatchEvent(evt);
  44. }
  45. }
  46. // ROWLINK PLUGIN DEFINITION
  47. // ===========================
  48. var old = $.fn.rowlink
  49. $.fn.rowlink = function (options) {
  50. return this.each(function () {
  51. var $this = $(this)
  52. var data = $this.data('bs.rowlink')
  53. if (!data) $this.data('bs.rowlink', (data = new Rowlink(this, options)))
  54. })
  55. }
  56. $.fn.rowlink.Constructor = Rowlink
  57. // ROWLINK NO CONFLICT
  58. // ====================
  59. $.fn.rowlink.noConflict = function () {
  60. $.fn.rowlink = old
  61. return this
  62. }
  63. // ROWLINK DATA-API
  64. // ==================
  65. $(document).on('click.bs.rowlink.data-api mouseup.bs.rowlink.data-api', '[data-link="row"]', function (e) {
  66. if (e.type === 'mouseup' && e.which !== 2) return
  67. if ($(e.target).closest('.rowlink-skip').length !== 0) return
  68. var $this = $(this)
  69. if ($this.data('bs.rowlink')) return
  70. $this.rowlink($this.data())
  71. var ctrlKey = e.ctrlKey || e.which === 2
  72. $(e.target).trigger('click.bs.rowlink', [ctrlKey])
  73. })
  74. }(window.jQuery);