aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test/ujs/public/test/settings.js
blob: c68ca24d6a00360d0aedb33cf9f0b23fe7b6e4ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var App = App || {}

App.assertCallbackInvoked = function(callbackName) {
  ok(true, callbackName + ' callback should have been invoked')
}

App.assertCallbackNotInvoked = function(callbackName) {
  ok(false, callbackName + ' callback should not have been invoked')
}

App.assertGetRequest = function(requestEnv) {
  equal(requestEnv['REQUEST_METHOD'], 'GET', 'request type should be GET')
}

App.assertPostRequest = function(requestEnv) {
  equal(requestEnv['REQUEST_METHOD'], 'POST', 'request type should be POST')
}

App.assertRequestPath = function(requestEnv, path) {
  equal(requestEnv['PATH_INFO'], path, 'request should be sent to right url')
}

App.getVal = function(el) {
  return el.is('input,textarea,select') ? el.val() : el.text()
}

App.disabled = function(el) {
  return el.is('input,textarea,select,button') ?
    (el.is(':disabled') && $.rails.getData(el[0], 'ujs:disabled')) :
    $.rails.getData(el[0], 'ujs:disabled')
}

App.checkEnabledState = function(el, text) {
  ok(!App.disabled(el), el.get(0).tagName + ' should not be disabled')
  equal(App.getVal(el), text, el.get(0).tagName + ' text should be original value')
}

App.checkDisabledState = function(el, text) {
  ok(App.disabled(el), el.get(0).tagName + ' should be disabled')
  equal(App.getVal(el), text, el.get(0).tagName + ' text should be disabled value')
}

// hijacks normal form submit; lets it submit to an iframe to prevent
// navigating away from the test suite
$(document).bind('submit', function(e) {
  if (!e.isDefaultPrevented()) {
    var form = $(e.target), action = form.attr('action'),
        name = 'form-frame' + jQuery.guid++,
        iframe = $('<iframe name="' + name + '" />'),
        iframeInput = '<input name="iframe" value="true" type="hidden" />'
        targetInput = '<input name="_target" value="' + (form.attr('target') || '') + '" type="hidden" />'

    if (action && action.indexOf('iframe') < 0) {
      if (action.indexOf('?') < 0) {
        form.attr('action', action + '?iframe=true')
      } else {
        form.attr('action', action + '&iframe=true')
      }
    }
    form.attr('target', name).append(iframeInput, targetInput)
    $('#qunit-fixture').append(iframe)
    $.event.trigger('iframe:loading', form)
  }
})

var MouseEvent = window.MouseEvent

try {
  new MouseEvent()
} catch (e) {
  MouseEvent = function(type, options) {
    var evt = document.createEvent('MouseEvents')
    evt.initMouseEvent(type, options.bubbles, options.cancelable, window, options.detail, 0, 0, 80, 20, options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, 0, null)
    return evt
  }
}

$.fn.extend({
  // trigger a native click event
  triggerNative: function(type, options) {
    var el = this[0],
        event,
        Evt = {
          'click': MouseEvent,
          'change': Event,
          'pageshow': PageTransitionEvent,
          'submit': Event
        }[type]

    options = options || {}
    options.bubbles = true
    options.cancelable = true

    event = new Evt(type, options)

    el.dispatchEvent(event)

    if (type === 'submit' && !event.defaultPrevented) {
      el.submit()
    }
    return this
  },
  bindNative: function(event, handler) {
    if (!handler) return this

    this.bind(event, function(e) {
      var args = []
      if (e.originalEvent.detail) {
        args = e.originalEvent.detail.slice()
      }
      args.unshift(e)
      return handler.apply(this, args)
    })
    return this
  }
})