browse-everything 0.8.4 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e8fb0f88b1f3c66e995d62163bdf5956ad135ac
4
- data.tar.gz: b8612b268a34c4bf2102de132e6daf37405b4faf
3
+ metadata.gz: 4c600921a1bdfb811b2d7a975fd53ed1f844155a
4
+ data.tar.gz: d561dc5a4f0436434269a065180225a1260600bc
5
5
  SHA512:
6
- metadata.gz: a5ef3c31fb7fee93213a0af2b5e417b13686e6c130c8f26a14cc7c5569822de0cb4f7457d2ffedae7f072f2bfe355c34be02c1b783374e913d083c1fddc796be
7
- data.tar.gz: c7e71366fb691754f664efab3da76d43576f2da398a3673d6844cc918c26df2c8f9534adb14fae8924fe696599b1c0eb5a4776fd3e180108379f515f34e4791e
6
+ metadata.gz: ebb0c0552035cb7bf939913b28f7772d6d683926dec4c6a9480ae4ba5f08d9f4920598dc36a0cd48494ca6a28384bddd27735c5c9bf4224cf857ad790cb7ba90
7
+ data.tar.gz: da5a404ff9f503cff21095b2d7f132c8d20980b35087204ea4d6598ad0a5dfa0d9a3b1d2890abd81127c78f6c17d90a0dfe33e80e154fa7d477957918e49d249
data/HISTORY.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### 0.9.0 (2015-10-21)
2
+ - Add Select All and Recursive Select capabilities
3
+ - Speed up Box API calls
4
+ - Add Jasmine tests
5
+
1
6
  ### 0.8.4 (2015-10-01)
2
7
  - Bug fixes for Box provider
3
8
  - Text fixture fixes for Dropbox provider
data/Rakefile CHANGED
@@ -9,3 +9,5 @@ require 'rspec/core/rake_task'
9
9
  require 'engine_cart/rake_task'
10
10
 
11
11
  task :default => [:ci]
12
+ require 'jasmine'
13
+ load 'jasmine/tasks/jasmine.rake'
@@ -37,6 +37,75 @@ $ ->
37
37
  $('input.ev-url').each () ->
38
38
  $("*[data-ev-location='#{$(this).val()}']").addClass('ev-selected')
39
39
 
40
+ fileIsSelected = (row) ->
41
+ result = false
42
+ $('input.ev-url').each () ->
43
+ if this.value == $(row).data('ev-location')
44
+ result = true
45
+ return result
46
+
47
+ toggleFileSelect = (row) ->
48
+ row.toggleClass('ev-selected')
49
+ if row.hasClass('ev-selected')
50
+ selectFile(row)
51
+ else
52
+ unselectFile(row)
53
+ updateFileCount()
54
+
55
+ selectFile = (row) ->
56
+ target_form = $('form.ev-submit-form')
57
+ file_location = row.data('ev-location')
58
+ hidden_input = $("<input type='hidden' class='ev-url' name='selected_files[]'/>").val(file_location)
59
+ target_form.append(hidden_input)
60
+
61
+ unselectFile = (row) ->
62
+ target_form = $('form.ev-submit-form')
63
+ file_location = row.data('ev-location')
64
+ $("form.ev-submit-form input[value='#{file_location}']").remove()
65
+
66
+ updateFileCount = () ->
67
+ count = $('input.ev-url').length
68
+ files = if count == 1 then "file" else "files"
69
+ $('.ev-status').html("#{count} #{files} selected")
70
+
71
+ toggleBranchSelect = (row) ->
72
+ if row.hasClass('collapsed')
73
+ node_id = row.find('td.ev-file-name a.ev-link').attr('href')
74
+ $('table#file-list').treetable('expandNode',node_id)
75
+
76
+ selectAll = (rows) ->
77
+ rows.each () ->
78
+ if $(this).data('tt-branch')
79
+ box = $(this).find('#select_all')[0]
80
+ $(box).prop('checked', true)
81
+ $(box).prop('value', "1")
82
+ toggleBranchSelect($(this))
83
+ else
84
+ toggleFileSelect($(this)) unless fileIsSelected($(this))
85
+
86
+ selectChildRows = (row, action) ->
87
+ $('table#file-list tr').each () ->
88
+ if $(this).data('tt-parent-id')
89
+ re = RegExp($(row).data('tt-id'), 'i')
90
+ if $(this).data('tt-parent-id').match(re)
91
+ if $(this).data('tt-branch')
92
+ box = $(this).find('#select_all')[0]
93
+ $(box).prop('value', action)
94
+ if action == "1"
95
+ $(box).prop("checked", true)
96
+ node_id = $(this).find('td.ev-file-name a.ev-link').attr('href')
97
+ $('table#file-list').treetable('expandNode',node_id)
98
+ else
99
+ $(box).prop("checked", false)
100
+ else
101
+ if action == "1"
102
+ $(this).addClass('ev-selected')
103
+ selectFile($(this)) unless fileIsSelected($(this))
104
+ else
105
+ $(this).removeClass('ev-selected')
106
+ unselectFile($(this))
107
+ updateFileCount()
108
+
40
109
  tableSetup = (table) ->
41
110
  table.treetable
42
111
  expandable: true
@@ -95,6 +164,8 @@ $ ->
95
164
  $(node).show()
96
165
  sizeColumns(table)
97
166
  indicateSelected()
167
+ if $(node.row).find('#select_all')[0].checked
168
+ selectAll(rows)
98
169
  .always ->
99
170
  clearInterval progressIntervalID
100
171
  $('body').css('cursor','default')
@@ -131,6 +202,12 @@ $ ->
131
202
  fail: -> this
132
203
  }
133
204
 
205
+ $.fn.browseEverything.toggleCheckbox = (box) ->
206
+ if box.value == "0"
207
+ $(box).prop('value', "1")
208
+ else
209
+ $(box).prop('value', "0")
210
+
134
211
  $(document).on 'ev.refresh', (event) -> refreshFiles()
135
212
 
136
213
  $(document).on 'click', 'button.ev-cancel', (event) ->
@@ -161,9 +238,6 @@ $ ->
161
238
  $('body').css('cursor','default')
162
239
  $('.ev-browser').modal('hide')
163
240
  $('#browse-btn').focus()
164
-
165
- $(document).on 'click', '.ev-files table tr', (event) ->
166
- $('a.ev-link',this).click() unless event.target.nodeName == 'A'
167
241
 
168
242
  $(document).on 'click', '.ev-files .ev-container a.ev-link', (event) ->
169
243
  event.stopPropagation()
@@ -201,18 +275,7 @@ $ ->
201
275
  $(document).on 'click', '.ev-file a', (event) ->
202
276
  event.preventDefault()
203
277
  target = $(this).closest('*[data-ev-location]')
204
- target_form = $('form.ev-submit-form')
205
- file_location = target.data('ev-location')
206
- target.toggleClass('ev-selected')
207
- if target.hasClass('ev-selected')
208
- hidden_input = $("<input type='hidden' class='ev-url' name='selected_files[]'/>").val(file_location)
209
- target_form.append(hidden_input)
210
- else
211
- $("form.ev-submit-form input[value='#{file_location}']").remove()
212
-
213
- count = $('input.ev-url').length
214
- files = if count == 1 then "file" else "files"
215
- $('.ev-status').html("#{count} #{files} selected")
278
+ toggleFileSelect(target)
216
279
 
217
280
  $(document).on 'click', '.ev-auth', (event) ->
218
281
  event.preventDefault()
@@ -224,6 +287,19 @@ $ ->
224
287
  window.setTimeout check_func, 1000
225
288
  check_func()
226
289
 
290
+ $(document).on 'change', 'input:checkbox', (event) ->
291
+ event.stopPropagation()
292
+ event.preventDefault()
293
+ $.fn.browseEverything.toggleCheckbox(this)
294
+ action = this.value
295
+ row = $(this).closest('tr')
296
+ node_id = row.find('td.ev-file-name a.ev-link').attr('href')
297
+ if row.hasClass('collapsed')
298
+ $('table#file-list').treetable('expandNode',node_id)
299
+ else
300
+ selectChildRows(row, action)
301
+
302
+
227
303
  auto_toggle = ->
228
304
  triggers = $('*[data-toggle=browse-everything]')
229
305
  triggers.each () -> $(this).browseEverything($(this).data())
@@ -23,6 +23,9 @@
23
23
  <% end %>
24
24
  <% end %>
25
25
  </td>
26
+ <td role="gridcell" class="ev-directory-select">
27
+ <%= check_box_tag(:select_all, "0", false, class: "ev-select-all") if file.container? %>
28
+ </td>
26
29
  <td role="gridcell" class="ev-file-size">
27
30
  <%= number_to_human_size(file.size).sub(/Bytes/,'bytes') %>
28
31
  </td>
@@ -8,6 +8,7 @@
8
8
  <thead>
9
9
  <tr role="row" tabindex="-1">
10
10
  <th role="columnheader">Name</th>
11
+ <th role="columnheader">Select All?</th>
11
12
  <th role="columnheader">Size</th>
12
13
  <th role="columnheader">Kind</th>
13
14
  <th role="columnheader">Modified</th>
@@ -41,5 +41,6 @@ Gem::Specification.new do |spec|
41
41
  spec.add_development_dependency "factory_girl_rails"
42
42
  spec.add_development_dependency "engine_cart"
43
43
  spec.add_development_dependency "capybara"
44
+ spec.add_development_dependency "jasmine", '~> 2.3'
44
45
 
45
46
  end
@@ -3,6 +3,8 @@ module BrowseEverything
3
3
  class Box < Base
4
4
  require 'ruby-box'
5
5
 
6
+ ITEM_LIMIT = 99999
7
+
6
8
  def icon
7
9
  'cloud'
8
10
  end
@@ -26,15 +28,15 @@ module BrowseEverything
26
28
  )
27
29
  end
28
30
  folder = path.empty? ? box_client.root_folder : box_client.folder(path)
29
- result += folder.items.collect do |f|
30
- BrowseEverything::FileEntry.new(
31
- File.join(path,f.name),#id here
32
- "#{self.key}:#{File.join(path,f.name)}",#single use link
33
- f.name,
34
- f.size,
35
- f.created_at,
36
- f.type == 'folder'
37
- )
31
+ result += folder.items(ITEM_LIMIT,0,['name','size','created_at']).collect do |f|
32
+ BrowseEverything::FileEntry.new(
33
+ File.join(path,f.name),#id here
34
+ "#{self.key}:#{File.join(path,f.name)}",#single use link
35
+ f.name,
36
+ f.size,
37
+ f.created_at,
38
+ f.type == 'folder'
39
+ )
38
40
  end
39
41
  result
40
42
  end
@@ -1,3 +1,3 @@
1
1
  module BrowseEverything
2
- VERSION = "0.8.4"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -0,0 +1,9 @@
1
+ describe("toggleCheckbox", function() {
2
+
3
+ it("toggles the value between 1 and 0", function() {
4
+ var html = '<input type="checkbox" name="select_all" id="select_all" value="0" class="ev-select-all"/>';
5
+ var toggled = $.fn.browseEverything.toggleCheckbox($(html)[0]);
6
+ expect($(toggled)[0].value).toEqual("1");
7
+ });
8
+
9
+ });
@@ -0,0 +1,838 @@
1
+ /*!
2
+ Jasmine-jQuery: a set of jQuery helpers for Jasmine tests.
3
+
4
+ Version 2.1.1
5
+
6
+ https://github.com/velesin/jasmine-jquery
7
+
8
+ Copyright (c) 2010-2014 Wojciech Zawistowski, Travis Jeffery
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining
11
+ a copy of this software and associated documentation files (the
12
+ "Software"), to deal in the Software without restriction, including
13
+ without limitation the rights to use, copy, modify, merge, publish,
14
+ distribute, sublicense, and/or sell copies of the Software, and to
15
+ permit persons to whom the Software is furnished to do so, subject to
16
+ the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be
19
+ included in all copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
+ */
29
+
30
+ (function (root, factory) {
31
+ if (typeof module !== 'undefined' && module.exports) {
32
+ factory(root, root.jasmine, require('jquery'));
33
+ } else {
34
+ factory(root, root.jasmine, root.jQuery);
35
+ }
36
+ }((function() {return this; })(), function (window, jasmine, $) { "use strict";
37
+
38
+ jasmine.spiedEventsKey = function (selector, eventName) {
39
+ return [$(selector).selector, eventName].toString()
40
+ }
41
+
42
+ jasmine.getFixtures = function () {
43
+ return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures()
44
+ }
45
+
46
+ jasmine.getStyleFixtures = function () {
47
+ return jasmine.currentStyleFixtures_ = jasmine.currentStyleFixtures_ || new jasmine.StyleFixtures()
48
+ }
49
+
50
+ jasmine.Fixtures = function () {
51
+ this.containerId = 'jasmine-fixtures'
52
+ this.fixturesCache_ = {}
53
+ this.fixturesPath = 'spec/javascripts/fixtures'
54
+ }
55
+
56
+ jasmine.Fixtures.prototype.set = function (html) {
57
+ this.cleanUp()
58
+ return this.createContainer_(html)
59
+ }
60
+
61
+ jasmine.Fixtures.prototype.appendSet= function (html) {
62
+ this.addToContainer_(html)
63
+ }
64
+
65
+ jasmine.Fixtures.prototype.preload = function () {
66
+ this.read.apply(this, arguments)
67
+ }
68
+
69
+ jasmine.Fixtures.prototype.load = function () {
70
+ this.cleanUp()
71
+ this.createContainer_(this.read.apply(this, arguments))
72
+ }
73
+
74
+ jasmine.Fixtures.prototype.appendLoad = function () {
75
+ this.addToContainer_(this.read.apply(this, arguments))
76
+ }
77
+
78
+ jasmine.Fixtures.prototype.read = function () {
79
+ var htmlChunks = []
80
+ , fixtureUrls = arguments
81
+
82
+ for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
83
+ htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex]))
84
+ }
85
+
86
+ return htmlChunks.join('')
87
+ }
88
+
89
+ jasmine.Fixtures.prototype.clearCache = function () {
90
+ this.fixturesCache_ = {}
91
+ }
92
+
93
+ jasmine.Fixtures.prototype.cleanUp = function () {
94
+ $('#' + this.containerId).remove()
95
+ }
96
+
97
+ jasmine.Fixtures.prototype.sandbox = function (attributes) {
98
+ var attributesToSet = attributes || {}
99
+ return $('<div id="sandbox" />').attr(attributesToSet)
100
+ }
101
+
102
+ jasmine.Fixtures.prototype.createContainer_ = function (html) {
103
+ var container = $('<div>')
104
+ .attr('id', this.containerId)
105
+ .html(html)
106
+
107
+ $(document.body).append(container)
108
+ return container
109
+ }
110
+
111
+ jasmine.Fixtures.prototype.addToContainer_ = function (html){
112
+ var container = $(document.body).find('#'+this.containerId).append(html)
113
+
114
+ if (!container.length) {
115
+ this.createContainer_(html)
116
+ }
117
+ }
118
+
119
+ jasmine.Fixtures.prototype.getFixtureHtml_ = function (url) {
120
+ if (typeof this.fixturesCache_[url] === 'undefined') {
121
+ this.loadFixtureIntoCache_(url)
122
+ }
123
+ return this.fixturesCache_[url]
124
+ }
125
+
126
+ jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
127
+ var self = this
128
+ , url = this.makeFixtureUrl_(relativeUrl)
129
+ , htmlText = ''
130
+ , request = $.ajax({
131
+ async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
132
+ cache: false,
133
+ url: url,
134
+ dataType: 'html',
135
+ success: function (data, status, $xhr) {
136
+ htmlText = $xhr.responseText
137
+ }
138
+ }).fail(function ($xhr, status, err) {
139
+ throw new Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + err.message + ')')
140
+ })
141
+
142
+ var scripts = $($.parseHTML(htmlText, true)).find('script[src]') || [];
143
+
144
+ scripts.each(function(){
145
+ $.ajax({
146
+ async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
147
+ cache: false,
148
+ dataType: 'script',
149
+ url: $(this).attr('src'),
150
+ success: function (data, status, $xhr) {
151
+ htmlText += '<script>' + $xhr.responseText + '</script>'
152
+ },
153
+ error: function ($xhr, status, err) {
154
+ throw new Error('Script could not be loaded: ' + url + ' (status: ' + status + ', message: ' + err.message + ')')
155
+ }
156
+ });
157
+ })
158
+
159
+ self.fixturesCache_[relativeUrl] = htmlText;
160
+ }
161
+
162
+ jasmine.Fixtures.prototype.makeFixtureUrl_ = function (relativeUrl){
163
+ return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
164
+ }
165
+
166
+ jasmine.Fixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) {
167
+ return this[methodName].apply(this, passedArguments)
168
+ }
169
+
170
+
171
+ jasmine.StyleFixtures = function () {
172
+ this.fixturesCache_ = {}
173
+ this.fixturesNodes_ = []
174
+ this.fixturesPath = 'spec/javascripts/fixtures'
175
+ }
176
+
177
+ jasmine.StyleFixtures.prototype.set = function (css) {
178
+ this.cleanUp()
179
+ this.createStyle_(css)
180
+ }
181
+
182
+ jasmine.StyleFixtures.prototype.appendSet = function (css) {
183
+ this.createStyle_(css)
184
+ }
185
+
186
+ jasmine.StyleFixtures.prototype.preload = function () {
187
+ this.read_.apply(this, arguments)
188
+ }
189
+
190
+ jasmine.StyleFixtures.prototype.load = function () {
191
+ this.cleanUp()
192
+ this.createStyle_(this.read_.apply(this, arguments))
193
+ }
194
+
195
+ jasmine.StyleFixtures.prototype.appendLoad = function () {
196
+ this.createStyle_(this.read_.apply(this, arguments))
197
+ }
198
+
199
+ jasmine.StyleFixtures.prototype.cleanUp = function () {
200
+ while(this.fixturesNodes_.length) {
201
+ this.fixturesNodes_.pop().remove()
202
+ }
203
+ }
204
+
205
+ jasmine.StyleFixtures.prototype.createStyle_ = function (html) {
206
+ var styleText = $('<div></div>').html(html).text()
207
+ , style = $('<style>' + styleText + '</style>')
208
+
209
+ this.fixturesNodes_.push(style)
210
+ $('head').append(style)
211
+ }
212
+
213
+ jasmine.StyleFixtures.prototype.clearCache = jasmine.Fixtures.prototype.clearCache
214
+ jasmine.StyleFixtures.prototype.read_ = jasmine.Fixtures.prototype.read
215
+ jasmine.StyleFixtures.prototype.getFixtureHtml_ = jasmine.Fixtures.prototype.getFixtureHtml_
216
+ jasmine.StyleFixtures.prototype.loadFixtureIntoCache_ = jasmine.Fixtures.prototype.loadFixtureIntoCache_
217
+ jasmine.StyleFixtures.prototype.makeFixtureUrl_ = jasmine.Fixtures.prototype.makeFixtureUrl_
218
+ jasmine.StyleFixtures.prototype.proxyCallTo_ = jasmine.Fixtures.prototype.proxyCallTo_
219
+
220
+ jasmine.getJSONFixtures = function () {
221
+ return jasmine.currentJSONFixtures_ = jasmine.currentJSONFixtures_ || new jasmine.JSONFixtures()
222
+ }
223
+
224
+ jasmine.JSONFixtures = function () {
225
+ this.fixturesCache_ = {}
226
+ this.fixturesPath = 'spec/javascripts/fixtures/json'
227
+ }
228
+
229
+ jasmine.JSONFixtures.prototype.load = function () {
230
+ this.read.apply(this, arguments)
231
+ return this.fixturesCache_
232
+ }
233
+
234
+ jasmine.JSONFixtures.prototype.read = function () {
235
+ var fixtureUrls = arguments
236
+
237
+ for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
238
+ this.getFixtureData_(fixtureUrls[urlIndex])
239
+ }
240
+
241
+ return this.fixturesCache_
242
+ }
243
+
244
+ jasmine.JSONFixtures.prototype.clearCache = function () {
245
+ this.fixturesCache_ = {}
246
+ }
247
+
248
+ jasmine.JSONFixtures.prototype.getFixtureData_ = function (url) {
249
+ if (!this.fixturesCache_[url]) this.loadFixtureIntoCache_(url)
250
+ return this.fixturesCache_[url]
251
+ }
252
+
253
+ jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
254
+ var self = this
255
+ , url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
256
+
257
+ $.ajax({
258
+ async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
259
+ cache: false,
260
+ dataType: 'json',
261
+ url: url,
262
+ success: function (data) {
263
+ self.fixturesCache_[relativeUrl] = data
264
+ },
265
+ error: function ($xhr, status, err) {
266
+ throw new Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + err.message + ')')
267
+ }
268
+ })
269
+ }
270
+
271
+ jasmine.JSONFixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) {
272
+ return this[methodName].apply(this, passedArguments)
273
+ }
274
+
275
+ jasmine.jQuery = function () {}
276
+
277
+ jasmine.jQuery.browserTagCaseIndependentHtml = function (html) {
278
+ return $('<div/>').append(html).html()
279
+ }
280
+
281
+ jasmine.jQuery.elementToString = function (element) {
282
+ return $(element).map(function () { return this.outerHTML; }).toArray().join(', ')
283
+ }
284
+
285
+ var data = {
286
+ spiedEvents: {}
287
+ , handlers: []
288
+ }
289
+
290
+ jasmine.jQuery.events = {
291
+ spyOn: function (selector, eventName) {
292
+ var handler = function (e) {
293
+ var calls = (typeof data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] !== 'undefined') ? data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)].calls : 0
294
+ data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] = {
295
+ args: jasmine.util.argsToArray(arguments),
296
+ calls: ++calls
297
+ }
298
+ }
299
+
300
+ $(selector).on(eventName, handler)
301
+ data.handlers.push(handler)
302
+
303
+ return {
304
+ selector: selector,
305
+ eventName: eventName,
306
+ handler: handler,
307
+ reset: function (){
308
+ delete data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
309
+ },
310
+ calls: {
311
+ count: function () {
312
+ return data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] ?
313
+ data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)].calls : 0;
314
+ },
315
+ any: function () {
316
+ return data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] ?
317
+ !!data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)].calls : false;
318
+ }
319
+ }
320
+ }
321
+ },
322
+
323
+ args: function (selector, eventName) {
324
+ var actualArgs = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)].args
325
+
326
+ if (!actualArgs) {
327
+ throw "There is no spy for " + eventName + " on " + selector.toString() + ". Make sure to create a spy using spyOnEvent."
328
+ }
329
+
330
+ return actualArgs
331
+ },
332
+
333
+ wasTriggered: function (selector, eventName) {
334
+ return !!(data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)])
335
+ },
336
+
337
+ wasTriggeredWith: function (selector, eventName, expectedArgs, util, customEqualityTesters) {
338
+ var actualArgs = jasmine.jQuery.events.args(selector, eventName).slice(1)
339
+
340
+ if (Object.prototype.toString.call(expectedArgs) !== '[object Array]')
341
+ actualArgs = actualArgs[0]
342
+
343
+ return util.equals(actualArgs, expectedArgs, customEqualityTesters)
344
+ },
345
+
346
+ wasPrevented: function (selector, eventName) {
347
+ var spiedEvent = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
348
+ , args = (jasmine.util.isUndefined(spiedEvent)) ? {} : spiedEvent.args
349
+ , e = args ? args[0] : undefined
350
+
351
+ return e && e.isDefaultPrevented()
352
+ },
353
+
354
+ wasStopped: function (selector, eventName) {
355
+ var spiedEvent = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
356
+ , args = (jasmine.util.isUndefined(spiedEvent)) ? {} : spiedEvent.args
357
+ , e = args ? args[0] : undefined
358
+
359
+ return e && e.isPropagationStopped()
360
+ },
361
+
362
+ cleanUp: function () {
363
+ data.spiedEvents = {}
364
+ data.handlers = []
365
+ }
366
+ }
367
+
368
+ var hasProperty = function (actualValue, expectedValue) {
369
+ if (expectedValue === undefined)
370
+ return actualValue !== undefined
371
+
372
+ return actualValue === expectedValue
373
+ }
374
+
375
+ beforeEach(function () {
376
+ jasmine.addMatchers({
377
+ toHaveClass: function () {
378
+ return {
379
+ compare: function (actual, className) {
380
+ return { pass: $(actual).hasClass(className) }
381
+ }
382
+ }
383
+ },
384
+
385
+ toHaveCss: function () {
386
+ return {
387
+ compare: function (actual, css) {
388
+ for (var prop in css){
389
+ var value = css[prop]
390
+ // see issue #147 on gh
391
+ ;if (value === 'auto' && $(actual).get(0).style[prop] === 'auto') continue
392
+ if ($(actual).css(prop) !== value) return { pass: false }
393
+ }
394
+ return { pass: true }
395
+ }
396
+ }
397
+ },
398
+
399
+ toBeVisible: function () {
400
+ return {
401
+ compare: function (actual) {
402
+ return { pass: $(actual).is(':visible') }
403
+ }
404
+ }
405
+ },
406
+
407
+ toBeHidden: function () {
408
+ return {
409
+ compare: function (actual) {
410
+ return { pass: $(actual).is(':hidden') }
411
+ }
412
+ }
413
+ },
414
+
415
+ toBeSelected: function () {
416
+ return {
417
+ compare: function (actual) {
418
+ return { pass: $(actual).is(':selected') }
419
+ }
420
+ }
421
+ },
422
+
423
+ toBeChecked: function () {
424
+ return {
425
+ compare: function (actual) {
426
+ return { pass: $(actual).is(':checked') }
427
+ }
428
+ }
429
+ },
430
+
431
+ toBeEmpty: function () {
432
+ return {
433
+ compare: function (actual) {
434
+ return { pass: $(actual).is(':empty') }
435
+ }
436
+ }
437
+ },
438
+
439
+ toBeInDOM: function () {
440
+ return {
441
+ compare: function (actual) {
442
+ return { pass: $.contains(document.documentElement, $(actual)[0]) }
443
+ }
444
+ }
445
+ },
446
+
447
+ toExist: function () {
448
+ return {
449
+ compare: function (actual) {
450
+ return { pass: $(actual).length }
451
+ }
452
+ }
453
+ },
454
+
455
+ toHaveLength: function () {
456
+ return {
457
+ compare: function (actual, length) {
458
+ return { pass: $(actual).length === length }
459
+ }
460
+ }
461
+ },
462
+
463
+ toHaveAttr: function () {
464
+ return {
465
+ compare: function (actual, attributeName, expectedAttributeValue) {
466
+ return { pass: hasProperty($(actual).attr(attributeName), expectedAttributeValue) }
467
+ }
468
+ }
469
+ },
470
+
471
+ toHaveProp: function () {
472
+ return {
473
+ compare: function (actual, propertyName, expectedPropertyValue) {
474
+ return { pass: hasProperty($(actual).prop(propertyName), expectedPropertyValue) }
475
+ }
476
+ }
477
+ },
478
+
479
+ toHaveId: function () {
480
+ return {
481
+ compare: function (actual, id) {
482
+ return { pass: $(actual).attr('id') == id }
483
+ }
484
+ }
485
+ },
486
+
487
+ toHaveHtml: function () {
488
+ return {
489
+ compare: function (actual, html) {
490
+ return { pass: $(actual).html() == jasmine.jQuery.browserTagCaseIndependentHtml(html) }
491
+ }
492
+ }
493
+ },
494
+
495
+ toContainHtml: function () {
496
+ return {
497
+ compare: function (actual, html) {
498
+ var actualHtml = $(actual).html()
499
+ , expectedHtml = jasmine.jQuery.browserTagCaseIndependentHtml(html)
500
+
501
+ return { pass: (actualHtml.indexOf(expectedHtml) >= 0) }
502
+ }
503
+ }
504
+ },
505
+
506
+ toHaveText: function () {
507
+ return {
508
+ compare: function (actual, text) {
509
+ var actualText = $(actual).text()
510
+ var trimmedText = $.trim(actualText)
511
+
512
+ if (text && $.isFunction(text.test)) {
513
+ return { pass: text.test(actualText) || text.test(trimmedText) }
514
+ } else {
515
+ return { pass: (actualText == text || trimmedText == text) }
516
+ }
517
+ }
518
+ }
519
+ },
520
+
521
+ toContainText: function () {
522
+ return {
523
+ compare: function (actual, text) {
524
+ var trimmedText = $.trim($(actual).text())
525
+
526
+ if (text && $.isFunction(text.test)) {
527
+ return { pass: text.test(trimmedText) }
528
+ } else {
529
+ return { pass: trimmedText.indexOf(text) != -1 }
530
+ }
531
+ }
532
+ }
533
+ },
534
+
535
+ toHaveValue: function () {
536
+ return {
537
+ compare: function (actual, value) {
538
+ return { pass: $(actual).val() === value }
539
+ }
540
+ }
541
+ },
542
+
543
+ toHaveData: function () {
544
+ return {
545
+ compare: function (actual, key, expectedValue) {
546
+ return { pass: hasProperty($(actual).data(key), expectedValue) }
547
+ }
548
+ }
549
+ },
550
+
551
+ toContainElement: function () {
552
+ return {
553
+ compare: function (actual, selector) {
554
+ return { pass: $(actual).find(selector).length }
555
+ }
556
+ }
557
+ },
558
+
559
+ toBeMatchedBy: function () {
560
+ return {
561
+ compare: function (actual, selector) {
562
+ return { pass: $(actual).filter(selector).length }
563
+ }
564
+ }
565
+ },
566
+
567
+ toBeDisabled: function () {
568
+ return {
569
+ compare: function (actual, selector) {
570
+ return { pass: $(actual).is(':disabled') }
571
+ }
572
+ }
573
+ },
574
+
575
+ toBeFocused: function (selector) {
576
+ return {
577
+ compare: function (actual, selector) {
578
+ return { pass: $(actual)[0] === $(actual)[0].ownerDocument.activeElement }
579
+ }
580
+ }
581
+ },
582
+
583
+ toHandle: function () {
584
+ return {
585
+ compare: function (actual, event) {
586
+ if ( !actual || actual.length === 0 ) return { pass: false };
587
+ var events = $._data($(actual).get(0), "events")
588
+
589
+ if (!events || !event || typeof event !== "string") {
590
+ return { pass: false }
591
+ }
592
+
593
+ var namespaces = event.split(".")
594
+ , eventType = namespaces.shift()
595
+ , sortedNamespaces = namespaces.slice(0).sort()
596
+ , namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)")
597
+
598
+ if (events[eventType] && namespaces.length) {
599
+ for (var i = 0; i < events[eventType].length; i++) {
600
+ var namespace = events[eventType][i].namespace
601
+
602
+ if (namespaceRegExp.test(namespace))
603
+ return { pass: true }
604
+ }
605
+ } else {
606
+ return { pass: (events[eventType] && events[eventType].length > 0) }
607
+ }
608
+
609
+ return { pass: false }
610
+ }
611
+ }
612
+ },
613
+
614
+ toHandleWith: function () {
615
+ return {
616
+ compare: function (actual, eventName, eventHandler) {
617
+ if ( !actual || actual.length === 0 ) return { pass: false };
618
+ var normalizedEventName = eventName.split('.')[0]
619
+ , stack = $._data($(actual).get(0), "events")[normalizedEventName]
620
+
621
+ for (var i = 0; i < stack.length; i++) {
622
+ if (stack[i].handler == eventHandler) return { pass: true }
623
+ }
624
+
625
+ return { pass: false }
626
+ }
627
+ }
628
+ },
629
+
630
+ toHaveBeenTriggeredOn: function () {
631
+ return {
632
+ compare: function (actual, selector) {
633
+ var result = { pass: jasmine.jQuery.events.wasTriggered(selector, actual) }
634
+
635
+ result.message = result.pass ?
636
+ "Expected event " + $(actual) + " not to have been triggered on " + selector :
637
+ "Expected event " + $(actual) + " to have been triggered on " + selector
638
+
639
+ return result;
640
+ }
641
+ }
642
+ },
643
+
644
+ toHaveBeenTriggered: function (){
645
+ return {
646
+ compare: function (actual) {
647
+ var eventName = actual.eventName
648
+ , selector = actual.selector
649
+ , result = { pass: jasmine.jQuery.events.wasTriggered(selector, eventName) }
650
+
651
+ result.message = result.pass ?
652
+ "Expected event " + eventName + " not to have been triggered on " + selector :
653
+ "Expected event " + eventName + " to have been triggered on " + selector
654
+
655
+ return result
656
+ }
657
+ }
658
+ },
659
+
660
+ toHaveBeenTriggeredOnAndWith: function (j$, customEqualityTesters) {
661
+ return {
662
+ compare: function (actual, selector, expectedArgs) {
663
+ var wasTriggered = jasmine.jQuery.events.wasTriggered(selector, actual)
664
+ , result = { pass: wasTriggered && jasmine.jQuery.events.wasTriggeredWith(selector, actual, expectedArgs, j$, customEqualityTesters) }
665
+
666
+ if (wasTriggered) {
667
+ var actualArgs = jasmine.jQuery.events.args(selector, actual, expectedArgs)[1]
668
+ result.message = result.pass ?
669
+ "Expected event " + actual + " not to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs) :
670
+ "Expected event " + actual + " to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs)
671
+
672
+ } else {
673
+ // todo check on this
674
+ result.message = result.pass ?
675
+ "Expected event " + actual + " not to have been triggered on " + selector :
676
+ "Expected event " + actual + " to have been triggered on " + selector
677
+ }
678
+
679
+ return result
680
+ }
681
+ }
682
+ },
683
+
684
+ toHaveBeenPreventedOn: function () {
685
+ return {
686
+ compare: function (actual, selector) {
687
+ var result = { pass: jasmine.jQuery.events.wasPrevented(selector, actual) }
688
+
689
+ result.message = result.pass ?
690
+ "Expected event " + actual + " not to have been prevented on " + selector :
691
+ "Expected event " + actual + " to have been prevented on " + selector
692
+
693
+ return result
694
+ }
695
+ }
696
+ },
697
+
698
+ toHaveBeenPrevented: function () {
699
+ return {
700
+ compare: function (actual) {
701
+ var eventName = actual.eventName
702
+ , selector = actual.selector
703
+ , result = { pass: jasmine.jQuery.events.wasPrevented(selector, eventName) }
704
+
705
+ result.message = result.pass ?
706
+ "Expected event " + eventName + " not to have been prevented on " + selector :
707
+ "Expected event " + eventName + " to have been prevented on " + selector
708
+
709
+ return result
710
+ }
711
+ }
712
+ },
713
+
714
+ toHaveBeenStoppedOn: function () {
715
+ return {
716
+ compare: function (actual, selector) {
717
+ var result = { pass: jasmine.jQuery.events.wasStopped(selector, actual) }
718
+
719
+ result.message = result.pass ?
720
+ "Expected event " + actual + " not to have been stopped on " + selector :
721
+ "Expected event " + actual + " to have been stopped on " + selector
722
+
723
+ return result;
724
+ }
725
+ }
726
+ },
727
+
728
+ toHaveBeenStopped: function () {
729
+ return {
730
+ compare: function (actual) {
731
+ var eventName = actual.eventName
732
+ , selector = actual.selector
733
+ , result = { pass: jasmine.jQuery.events.wasStopped(selector, eventName) }
734
+
735
+ result.message = result.pass ?
736
+ "Expected event " + eventName + " not to have been stopped on " + selector :
737
+ "Expected event " + eventName + " to have been stopped on " + selector
738
+
739
+ return result
740
+ }
741
+ }
742
+ }
743
+ })
744
+
745
+ jasmine.getEnv().addCustomEqualityTester(function(a, b) {
746
+ if (a && b) {
747
+ if (a instanceof $ || jasmine.isDomNode(a)) {
748
+ var $a = $(a)
749
+
750
+ if (b instanceof $)
751
+ return $a.length == b.length && a.is(b)
752
+
753
+ return $a.is(b);
754
+ }
755
+
756
+ if (b instanceof $ || jasmine.isDomNode(b)) {
757
+ var $b = $(b)
758
+
759
+ if (a instanceof $)
760
+ return a.length == $b.length && $b.is(a)
761
+
762
+ return $(b).is(a);
763
+ }
764
+ }
765
+ })
766
+
767
+ jasmine.getEnv().addCustomEqualityTester(function (a, b) {
768
+ if (a instanceof $ && b instanceof $ && a.size() == b.size())
769
+ return a.is(b)
770
+ })
771
+ })
772
+
773
+ afterEach(function () {
774
+ jasmine.getFixtures().cleanUp()
775
+ jasmine.getStyleFixtures().cleanUp()
776
+ jasmine.jQuery.events.cleanUp()
777
+ })
778
+
779
+ window.readFixtures = function () {
780
+ return jasmine.getFixtures().proxyCallTo_('read', arguments)
781
+ }
782
+
783
+ window.preloadFixtures = function () {
784
+ jasmine.getFixtures().proxyCallTo_('preload', arguments)
785
+ }
786
+
787
+ window.loadFixtures = function () {
788
+ jasmine.getFixtures().proxyCallTo_('load', arguments)
789
+ }
790
+
791
+ window.appendLoadFixtures = function () {
792
+ jasmine.getFixtures().proxyCallTo_('appendLoad', arguments)
793
+ }
794
+
795
+ window.setFixtures = function (html) {
796
+ return jasmine.getFixtures().proxyCallTo_('set', arguments)
797
+ }
798
+
799
+ window.appendSetFixtures = function () {
800
+ jasmine.getFixtures().proxyCallTo_('appendSet', arguments)
801
+ }
802
+
803
+ window.sandbox = function (attributes) {
804
+ return jasmine.getFixtures().sandbox(attributes)
805
+ }
806
+
807
+ window.spyOnEvent = function (selector, eventName) {
808
+ return jasmine.jQuery.events.spyOn(selector, eventName)
809
+ }
810
+
811
+ window.preloadStyleFixtures = function () {
812
+ jasmine.getStyleFixtures().proxyCallTo_('preload', arguments)
813
+ }
814
+
815
+ window.loadStyleFixtures = function () {
816
+ jasmine.getStyleFixtures().proxyCallTo_('load', arguments)
817
+ }
818
+
819
+ window.appendLoadStyleFixtures = function () {
820
+ jasmine.getStyleFixtures().proxyCallTo_('appendLoad', arguments)
821
+ }
822
+
823
+ window.setStyleFixtures = function (html) {
824
+ jasmine.getStyleFixtures().proxyCallTo_('set', arguments)
825
+ }
826
+
827
+ window.appendSetStyleFixtures = function (html) {
828
+ jasmine.getStyleFixtures().proxyCallTo_('appendSet', arguments)
829
+ }
830
+
831
+ window.loadJSONFixtures = function () {
832
+ return jasmine.getJSONFixtures().proxyCallTo_('load', arguments)
833
+ }
834
+
835
+ window.getJSONFixture = function (url) {
836
+ return jasmine.getJSONFixtures().proxyCallTo_('read', arguments)[url]
837
+ }
838
+ }));