Skip to content

Commit

Permalink
added basic structure to run cypress tests and open the cypress IDE
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Apr 30, 2021
1 parent 93aabea commit 7c723ba
Show file tree
Hide file tree
Showing 21 changed files with 653 additions and 154 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/docs/.jekyll-metadata
/pkg/
/test/e2e/cypress/videos/
/test/e2e/cypress/screenshots/
/tmp/
/docs/.jekyll-metadata
/.idea/
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem 'oj', require: false # false is for testing with or without it
gem 'rack'
gem 'rake'
gem 'rake-manifest'
gem 'rerun'

group :test do
gem 'codecov', require: false
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ GEM
simplecov (>= 0.15, < 0.22)
concurrent-ruby (1.1.8)
docile (1.3.5)
ffi (1.15.0)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
kalibera (0.1.1)
memoist (~> 0.11.0)
rbzip2 (~> 0.2.0)
listen (3.5.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
memoist (0.11.0)
memory_profiler (1.0.0)
minitest (5.14.4)
Expand All @@ -31,8 +35,13 @@ GEM
rainbow (3.0.0)
rake (13.0.3)
rake-manifest (0.2.0)
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
rbzip2 (0.2.0)
regexp_parser (2.1.1)
rerun (0.13.1)
listen (~> 3.0)
rexml (3.2.5)
rubocop (1.13.0)
parallel (~> 1.10)
Expand Down Expand Up @@ -91,6 +100,7 @@ DEPENDENCIES
rack
rake
rake-manifest
rerun
rubocop
rubocop-minitest
rubocop-packaging
Expand Down
39 changes: 17 additions & 22 deletions apps/basic_app.ru
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
# frozen_string_literal: true

# Basec self-contained rack/sinatra app
# Basic self-contained rack/sinatra app
# edit or duplicate this app to experiment with the pagy features

# USAGE: rackup -I lib -o 0.0.0.0 -p 8080 apps/basic_app.ru
# USAGE: rerun -- rackup -o 0.0.0.0 -p 8080 apps/basic_app.ru

# standard bundler using project Gemfile
require 'rubygems'
require 'bundler'
Bundler.require(:default, :apps)
require 'oj' # require false in Gemfile

$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'pagy'

# pagy initializer
require 'pagy/extras/navs'
require 'pagy/extras/items'
require 'pagy/extras/trim' if ENV['ENABLE_TRIM']
require 'oj' if ENV['ENABLE_OJ'] == 'true'
require 'pagy/extras/trim'
Pagy::VARS[:trim] = false # opt-in trim

# sinatra setup
require 'sinatra/base'
require 'sinatra/reloader'


# simple array-based collection that acts as standard DB collection
class MockCollection < Array
Expand Down Expand Up @@ -53,20 +50,19 @@ class PagyApp < Sinatra::Base
enable :inline_templates
end

configure :development do
register Sinatra::Reloader
end

include Pagy::Backend
include Pagy::Frontend

helpers do
include Pagy::Frontend
end

get '/pagy.js' do
content_type 'application/javascript'
send_file Pagy.root.join('javascripts', 'pagy.js')
end

get '/' do
redirect '/helpers'
erb :welcome
end

get '/helpers' do
Expand All @@ -75,10 +71,6 @@ class PagyApp < Sinatra::Base
erb :helpers
end

get '/no-pagy' do
erb :'no-pagy'
end

end

run PagyApp
Expand All @@ -99,6 +91,12 @@ __END__
</body>
</html>


@@ welcome
<h3>Pagy app</h3>
<p>This app runs on Sinatra/rackup/Puma</p>


@@ helpers
<br>
<%= pagy_nav(@pagy) %>
Expand All @@ -108,6 +106,3 @@ __END__
<%= pagy_combo_nav_js(@pagy) %>
<br>
<%= pagy_items_selector_js(@pagy) %>

@@ no-pagy
<p>Just a page without pagy</p>
197 changes: 102 additions & 95 deletions lib/javascripts/pagy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,116 @@

function Pagy(){}

Pagy.version = '4.4.0';
Pagy.version = '4.4.0'

Pagy.timeout = 100;
Pagy.delay = 100

Pagy.timeoutId = 0;
Pagy.init =
function(arg) {
var target = arg instanceof Event || arg === undefined ? document : arg,
jsonTags = target.getElementsByClassName('pagy-json')
for (var i = 0, len = jsonTags.length; i < len; i++) {
var args = JSON.parse(jsonTags[i].innerHTML),
fname = args.shift()
args.unshift(jsonTags[i].previousSibling)
Pagy[fname].apply(null, args)
}
}

Pagy.init = function(arg) {
var target = arg instanceof Event || arg === undefined ? document : arg,
jsonTags = target.getElementsByClassName('pagy-json');
for (var i = 0, len = jsonTags.length; i < len; i++) {
var args = JSON.parse(jsonTags[i].innerHTML),
fname = args.shift();
args.unshift(jsonTags[i].previousSibling);
Pagy[fname].apply(null, args);
}
}

Pagy.nav = function(pagyEl, tags, sequels, param) {
var lastWidth = undefined,
timeoutId = 0,
pageREg = new RegExp(/__pagy_page__/g),
widths = [],
wait = function() { clearTimeout(timeoutId); timeoutId = setTimeout(pagyEl.render, Pagy.timeout) };
Pagy.nav =
function(pagyEl, tags, sequels, trimParam) {
var lastWidth,
pageREg = new RegExp(/__pagy_page__/g),
widths = []
for (var width in sequels) {
if (sequels.hasOwnProperty(width)) { widths.push(parseInt(width, 10)) }
}
widths.sort(function(a, b) { return b - a })

for (var width in sequels) { widths.push(parseInt(width)) } // fine with sequels structure
widths.sort(function(a, b) { return b - a });
pagyEl.render =
function() {
var width, i, len
for (i = 0, len = widths.length; i < len; i++) {
if (this.parentElement.clientWidth > widths[i]) { width = widths[i]; break }
}
if (width !== lastWidth) {
var html = tags.before,
series = sequels[width]
for (i = 0, len = series.length; i < len; i++) {
var item = series[i]
if (typeof(trimParam) === 'string' && item === 1) { html += Pagy.trim(tags.link.replace(pageREg, item), trimParam) }
else if (typeof(item) === 'number') { html += tags.link.replace(pageREg, item) }
else if (item === 'gap') { html += tags.gap }
else if (typeof(item) === 'string') { html += tags.active.replace(pageREg, item) }
}
html += tags.after
this.innerHTML = ''
this.insertAdjacentHTML('afterbegin', html)
lastWidth = width
}
}.bind(pagyEl)
pagyEl.render()
}

pagyEl.render = function() {
if (this.parentElement.clientWidth === 0) { return wait() } // not ready yet
var width, i, len;
for (i = 0, len = widths.length; i < len; i++) {
if (this.parentElement.clientWidth > widths[i]) { width = widths[i]; break }
}
if (width !== lastWidth) {
var html = tags.before,
series = sequels[width];
for (i = 0, len = series.length; i < len; i++) {
var item = series[i];
if (typeof(param) === 'string' && item === 1) { html += Pagy.trim(tags.link.replace(pageREg, item), param) }
else if (typeof(item) === 'number') { html += tags.link.replace(pageREg, item) }
else if (item === 'gap') { html += tags.gap }
else if (typeof(item) === 'string') { html += tags.active.replace(pageREg, item) }
}
html += tags.after;
this.innerHTML = '';
this.insertAdjacentHTML('afterbegin', html);
lastWidth = width;
}
}.bind(pagyEl);
pagyEl.render();
}

Pagy.combo_nav = function(pagyEl, page, link, param) {
var input = pagyEl.getElementsByTagName('input')[0],
go = function() {
if (page !== input.value) {
var html = link.replace(/__pagy_page__/, input.value);
if (typeof (param) === 'string' && input.value === '1') { html = Pagy.trim(html, param) }
pagyEl.insertAdjacentHTML('afterbegin', html);
pagyEl.getElementsByTagName('a')[0].click();
}
};
Pagy.addInputEventListeners(input, go);
}
Pagy.combo_nav =
function(pagyEl, page, link, trimParam) {
var input = pagyEl.getElementsByTagName('input')[0],
toPage =
function() {
if (page !== input.value) {
var html = link.replace(/__pagy_page__/, input.value)
if (typeof (trimParam) === 'string' && input.value === '1') { html = Pagy.trim(html, trimParam) }
pagyEl.insertAdjacentHTML('afterbegin', html)
pagyEl.getElementsByTagName('a')[0].click()
}
}
Pagy.addInputEventListeners(input, toPage)
}

Pagy.items_selector = function(pagyEl, from, link, param) {
var input = pagyEl.getElementsByTagName('input')[0],
current = input.value,
go = function() {
var items = input.value;
if (current !== items) {
var page = Math.max(Math.ceil(from / items), 1),
html = link.replace(/__pagy_page__/, page).replace(/__pagy_items__/, items);
if (typeof (param) === 'string' && page === 1) { html = Pagy.trim(html, param) }
pagyEl.insertAdjacentHTML('afterbegin', html);
pagyEl.getElementsByTagName('a')[0].click();
}
};
Pagy.addInputEventListeners(input, go);
}
Pagy.items_selector =
function(pagyEl, from, link, param) {
var input = pagyEl.getElementsByTagName('input')[0],
current = input.value,
toPage =
function() {
var items = input.value
if (current !== items) {
var page = Math.max(Math.ceil(from / items), 1),
html = link.replace(/__pagy_page__/, page).replace(/__pagy_items__/, items)
if (typeof (param) === 'string' && page === 1) { html = Pagy.trim(html, param) }
pagyEl.insertAdjacentHTML('afterbegin', html)
pagyEl.getElementsByTagName('a')[0].click()
}
}
Pagy.addInputEventListeners(input, toPage)
}

Pagy.addInputEventListeners = function(input, handler) {
// select the content on click: easier for typing a number
input.addEventListener('click', function() { this.select() });
// go when the input looses focus
input.addEventListener('focusout', handler);
// … and when pressing enter inside the input
input.addEventListener('keyup', function(e) { if (e.which === 13) {handler()} }.bind(this));
}
Pagy.addInputEventListeners =
function(input, toPage) {
// select the content on click: easier for typing a number
input.addEventListener('click', function() { this.select() })
// toPage when the input looses focus
input.addEventListener('focusout', toPage)
// … and when pressing enter inside the input
input.addEventListener('keyup', function(e) { if (e.which === 13) {toPage()} }.bind(this))
}

Pagy.trim = function(html, param) {
var re = new RegExp('[?&]' + param + '=1\\b(?!&)|\\b' + param + '=1&');
return html.replace(re, '');
}
Pagy.trim =
function(html, param) {
var re = new RegExp('[?&]' + param + '=1\\b(?!&)|\\b' + param + '=1&')
return html.replace(re, '')
}

Pagy.renderNjs = function() {
var navs = document.getElementsByClassName('pagy-njs');
for (var i = 0, len = navs.length; i < len; i++) { navs[i].render() }
}
Pagy.renderNavs =
function() {
var navs = document.getElementsByClassName('pagy-njs') // 'pagy-njs' is common to all *nav_js helpers
for (var i = 0, len = navs.length; i < len; i++) { navs[i].render() }
}

Pagy.wait = function() {
clearTimeout(Pagy.timeoutId);
Pagy.timeoutId = setTimeout(Pagy.renderNjs, Pagy.timeout);
}
Pagy.waitForMe =
function() {
if (typeof(Pagy.tid) === 'number') { clearTimeout(Pagy.tid) }
Pagy.tid = setTimeout(Pagy.renderNavs, Pagy.delay)
}

window.addEventListener('resize', Pagy.wait, true);
window.addEventListener('resize', Pagy.waitForMe, true)
4 changes: 2 additions & 2 deletions lib/pagy/extras/semantic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def pagy_semantic_nav(pagy, pagy_id: nil, link_extra: '')
html << pagy_semantic_prev_html(pagy, link)
pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
html << case item
when Integer then link.call item # page link
when String then %(<a class="item active">#{item}</a>) # current page
when Integer then link.call item # page link
when String then %(<a class="item active">#{item}</a>) # current page
when :gap then %(<div class="disabled item">#{pagy_t 'pagy.nav.gap'}</div>) # page gap
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pagy/extras/uikit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def pagy_uikit_nav_js(pagy, deprecated_id=nil, pagy_id: nil, link_extra: '', ste
tags = { 'before' => pagy_uikit_prev_html(pagy, link),
'link' => %(<li>#{link.call(PAGE_PLACEHOLDER)}</li>),
'active' => %(<li class="uk-active"><span>#{PAGE_PLACEHOLDER}</span></li>),
'gap' => %(<li class="uk-disabled"><span>#{pagy_t('pagy.nav.gap')}</span></li>),
'gap' => %(<li class="uk-disabled"><span>#{pagy_t 'pagy.nav.gap'}</span></li>),
'after' => pagy_uikit_next_html(pagy, link) }

html = %(<ul#{p_id} class="pagy-njs pagy-uikit-nav-js uk-pagination uk-flex-center"></ul>)
Expand Down
Loading

0 comments on commit 7c723ba

Please sign in to comment.