From 1395c561faae447e41da8319b05e6e4bbac294c9 Mon Sep 17 00:00:00 2001 From: Jose Farias <31393016+josefarias@users.noreply.github.com> Date: Fri, 16 Jun 2023 11:06:23 -0500 Subject: [PATCH] Modernize the library (#134) * Implement Rollup as Blade buildtool replacement * Serve tests without Blade It won't show if we minify --- .blade.yml | 40 - .gitignore | 2 + .ruby-version | 2 +- CONTRIBUTING.md | 18 + Gemfile | 7 - Gemfile.lock | 285 +- README.md | 3 + Rakefile | 74 +- .../javascripts/local-time.es2017-esm.js | 1 + .../javascripts/local-time.es2017-umd.js | 1 + app/assets/javascripts/local-time.js | 1 - bin/blade | 17 - .../src/local-time/calendar_date.coffee | 2 + .../src/local-time/config/i18n.coffee | 2 + .../src/local-time/config/index.coffee | 3 + .../src/local-time/config/locale.coffee | 2 + .../src/local-time/config/timer.coffee | 2 + .../src/local-time/controller.coffee | 5 +- .../src/local-time/helpers/date_parse.coffee | 2 + .../src/local-time/helpers/dom.coffee | 22 +- .../src/local-time/helpers/i18n.coffee | 2 + .../src/local-time/helpers/index.coffee | 4 + .../src/local-time/helpers/strftime.coffee | 2 + .../javascripts/src/local-time/index.coffee | 25 +- .../src/local-time/local_time.coffee | 15 + .../src/local-time/page_observer.coffee | 2 + .../src/local-time/relative_time.coffee | 3 +- .../javascripts/src/local-time/start.coffee | 2 + package.json | 21 +- rollup.config.mjs | 55 + test/helpers/local_time_helper_test.rb | 1 + test/javascripts/fixtures/body.html | 8 - test/javascripts/fixtures/index.html | 20 + test/javascripts/server.mjs | 30 + test/javascripts/src/i18n_test.coffee | 2 + test/javascripts/src/index.coffee | 9 + test/javascripts/src/local_time_test.coffee | 2 + .../javascripts/src/relative_date_test.coffee | 2 + test/javascripts/src/strftime_test.coffee | 2 + .../src/{test.coffee => test_helpers.coffee} | 6 +- test/javascripts/src/time_ago_test.coffee | 2 + test/javascripts/vendor/moment.js | 5683 ++++- test/javascripts/vendor/sinon-timers.js | 385 - test/javascripts/vendor/sinon.js | 17742 ++++++++++++++++ yarn.lock | 680 + 45 files changed, 24503 insertions(+), 693 deletions(-) delete mode 100644 .blade.yml create mode 100644 CONTRIBUTING.md create mode 100644 app/assets/javascripts/local-time.es2017-esm.js create mode 100644 app/assets/javascripts/local-time.es2017-umd.js delete mode 100644 app/assets/javascripts/local-time.js delete mode 100755 bin/blade create mode 100644 lib/assets/javascripts/src/local-time/config/index.coffee create mode 100644 lib/assets/javascripts/src/local-time/helpers/index.coffee create mode 100644 lib/assets/javascripts/src/local-time/local_time.coffee create mode 100644 rollup.config.mjs delete mode 100644 test/javascripts/fixtures/body.html create mode 100644 test/javascripts/fixtures/index.html create mode 100644 test/javascripts/server.mjs create mode 100644 test/javascripts/src/index.coffee rename test/javascripts/src/{test.coffee => test_helpers.coffee} (92%) delete mode 100644 test/javascripts/vendor/sinon-timers.js create mode 100644 test/javascripts/vendor/sinon.js create mode 100644 yarn.lock diff --git a/.blade.yml b/.blade.yml deleted file mode 100644 index c819fcd..0000000 --- a/.blade.yml +++ /dev/null @@ -1,40 +0,0 @@ -load_paths: - - lib/assets/javascripts/src - - test/javascripts/src - - test/javascripts/vendor - - test/javascripts/fixtures - -logical_paths: - - local-time.js - - test.js - -build: - logical_paths: - - local-time.js - path: app/assets/javascripts - js_compressor: uglifier - -require: - - sprockets/export - -plugins: - sauce_labs: - test_config: - time_zone: Central - browsers: - Google Chrome: - version: -2 - os: Mac, Windows, Linux - Firefox: - version: -2 - os: Mac, Windows, Linux - Safari: - version: -2 - Microsoft Edge: - version: -2 - Internet Explorer: - version: -3 - iPhone: - version: -1 - Android: - version: -1 diff --git a/.gitignore b/.gitignore index 3fec32c..16c446d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ +test/javascripts/builds/ +/node_modules tmp/ diff --git a/.ruby-version b/.ruby-version index 0bee604..6a81b4c 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.3.3 +2.7.8 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..fa37007 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,18 @@ +# Contributing +## Getting started +Install the following dependencies: + +1. Ruby (version found [here](./.ruby-version)) +2. [bundler](https://bundler.io/) +3. [yarn](https://yarnpkg.com/) + +Then, open your console on the project directory and run `bundle install` and `yarn install`. + +Finally, run `rake assets:compile`. + +## Running Tests +This library has a Ruby component and a JavaScript component. Each component has its own test suite. + +To run both suites, open your console and run `rake test` from the project directory. + +Ruby tests will run first. You will then be prompted to open your web browser to run the JavaScript tests. diff --git a/Gemfile b/Gemfile index ee5fe88..91fd78d 100644 --- a/Gemfile +++ b/Gemfile @@ -3,10 +3,3 @@ source 'https://rubygems.org' gem 'rake' gem 'rails' gem 'rails-dom-testing' -gem 'sprockets' -gem 'sprockets-export' -gem 'coffee-script' -gem 'coffee-script-source' -gem 'uglifier' -gem 'blade' -gem 'blade-sauce_labs_plugin' diff --git a/Gemfile.lock b/Gemfile.lock index 506d3e2..76470af 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,187 +1,154 @@ GEM remote: https://rubygems.org/ specs: - actioncable (5.1.3) - actionpack (= 5.1.3) + actioncable (7.0.5) + actionpack (= 7.0.5) + activesupport (= 7.0.5) nio4r (~> 2.0) - websocket-driver (~> 0.6.1) - actionmailer (5.1.3) - actionpack (= 5.1.3) - actionview (= 5.1.3) - activejob (= 5.1.3) + websocket-driver (>= 0.6.1) + actionmailbox (7.0.5) + actionpack (= 7.0.5) + activejob (= 7.0.5) + activerecord (= 7.0.5) + activestorage (= 7.0.5) + activesupport (= 7.0.5) + mail (>= 2.7.1) + net-imap + net-pop + net-smtp + actionmailer (7.0.5) + actionpack (= 7.0.5) + actionview (= 7.0.5) + activejob (= 7.0.5) + activesupport (= 7.0.5) mail (~> 2.5, >= 2.5.4) + net-imap + net-pop + net-smtp rails-dom-testing (~> 2.0) - actionpack (5.1.3) - actionview (= 5.1.3) - activesupport (= 5.1.3) - rack (~> 2.0) - rack-test (~> 0.6.3) + actionpack (7.0.5) + actionview (= 7.0.5) + activesupport (= 7.0.5) + rack (~> 2.0, >= 2.2.4) + rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.1.3) - activesupport (= 5.1.3) + rails-html-sanitizer (~> 1.0, >= 1.2.0) + actiontext (7.0.5) + actionpack (= 7.0.5) + activerecord (= 7.0.5) + activestorage (= 7.0.5) + activesupport (= 7.0.5) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) + actionview (7.0.5) + activesupport (= 7.0.5) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.0.3) - activejob (5.1.3) - activesupport (= 5.1.3) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activejob (7.0.5) + activesupport (= 7.0.5) globalid (>= 0.3.6) - activemodel (5.1.3) - activesupport (= 5.1.3) - activerecord (5.1.3) - activemodel (= 5.1.3) - activesupport (= 5.1.3) - arel (~> 8.0) - activesupport (5.1.3) + activemodel (7.0.5) + activesupport (= 7.0.5) + activerecord (7.0.5) + activemodel (= 7.0.5) + activesupport (= 7.0.5) + activestorage (7.0.5) + actionpack (= 7.0.5) + activejob (= 7.0.5) + activerecord (= 7.0.5) + activesupport (= 7.0.5) + marcel (~> 1.0) + mini_mime (>= 1.1.0) + activesupport (7.0.5) concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (~> 0.7) - minitest (~> 5.1) - tzinfo (~> 1.1) - addressable (2.5.1) - public_suffix (~> 2.0, >= 2.0.2) - arel (8.0.0) - blade (0.7.0) - activesupport (>= 3.0.0) - blade-qunit_adapter (~> 2.0.1) - coffee-script - coffee-script-source - curses (~> 1.0.0) - eventmachine - faye - sprockets (>= 3.0) - thin (>= 1.6.0) - thor (~> 0.19.1) - useragent (~> 0.16.7) - blade-qunit_adapter (2.0.1) - blade-sauce_labs_plugin (0.7.2) - childprocess - faraday - selenium-webdriver - builder (3.2.3) - childprocess (0.7.1) - ffi (~> 1.0, >= 1.0.11) - coffee-script (2.4.1) - coffee-script-source - execjs - coffee-script-source (1.12.2) - concurrent-ruby (1.0.5) - cookiejar (0.3.3) - curses (1.0.2) - daemons (1.2.4) - em-http-request (1.1.5) - addressable (>= 2.3.4) - cookiejar (!= 0.3.1) - em-socksify (>= 0.3) - eventmachine (>= 1.0.3) - http_parser.rb (>= 0.6.0) - em-socksify (0.3.1) - eventmachine (>= 1.0.0.beta.4) - erubi (1.6.1) - eventmachine (1.2.5) - execjs (2.7.0) - faraday (0.12.2) - multipart-post (>= 1.2, < 3) - faye (1.2.4) - cookiejar (>= 0.3.0) - em-http-request (>= 0.3.0) - eventmachine (>= 0.12.0) - faye-websocket (>= 0.9.1) - multi_json (>= 1.0.0) - rack (>= 1.0.0) - websocket-driver (>= 0.5.1) - faye-websocket (0.10.7) - eventmachine (>= 0.12.0) - websocket-driver (>= 0.5.1) - ffi (1.9.18) - globalid (0.4.0) - activesupport (>= 4.2.0) - http_parser.rb (0.6.0) - i18n (0.8.6) - loofah (2.0.3) - nokogiri (>= 1.5.9) - mail (2.6.6) - mime-types (>= 1.16, < 4) - method_source (0.8.2) - mime-types (3.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) - mini_portile2 (2.2.0) - minitest (5.10.3) - multi_json (1.12.1) - multipart-post (2.0.0) - nio4r (2.1.0) - nokogiri (1.8.0) - mini_portile2 (~> 2.2.0) - public_suffix (2.0.5) - rack (2.0.3) - rack-test (0.6.3) - rack (>= 1.0) - rails (5.1.3) - actioncable (= 5.1.3) - actionmailer (= 5.1.3) - actionpack (= 5.1.3) - actionview (= 5.1.3) - activejob (= 5.1.3) - activemodel (= 5.1.3) - activerecord (= 5.1.3) - activesupport (= 5.1.3) - bundler (>= 1.3.0) - railties (= 5.1.3) - sprockets-rails (>= 2.0.0) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + builder (3.2.4) + concurrent-ruby (1.2.2) + crass (1.0.6) + date (3.3.3) + erubi (1.12.0) + globalid (1.1.0) + activesupport (>= 5.0) + i18n (1.14.1) + concurrent-ruby (~> 1.0) + loofah (2.21.3) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + mail (2.8.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.2) + method_source (1.0.0) + mini_mime (1.1.2) + mini_portile2 (2.8.2) + minitest (5.18.0) + net-imap (0.3.6) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.1) + timeout + net-smtp (0.3.3) + net-protocol + nio4r (2.5.9) + nokogiri (1.15.2) + mini_portile2 (~> 2.8.2) + racc (~> 1.4) + racc (1.7.1) + rack (2.2.7) + rack-test (2.1.0) + rack (>= 1.3) + rails (7.0.5) + actioncable (= 7.0.5) + actionmailbox (= 7.0.5) + actionmailer (= 7.0.5) + actionpack (= 7.0.5) + actiontext (= 7.0.5) + actionview (= 7.0.5) + activejob (= 7.0.5) + activemodel (= 7.0.5) + activerecord (= 7.0.5) + activestorage (= 7.0.5) + activesupport (= 7.0.5) + bundler (>= 1.15.0) + railties (= 7.0.5) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) - rails-html-sanitizer (1.0.3) - loofah (~> 2.0) - railties (5.1.3) - actionpack (= 5.1.3) - activesupport (= 5.1.3) + rails-html-sanitizer (1.6.0) + loofah (~> 2.21) + nokogiri (~> 1.14) + railties (7.0.5) + actionpack (= 7.0.5) + activesupport (= 7.0.5) method_source - rake (>= 0.8.7) - thor (>= 0.18.1, < 2.0) - rake (12.0.0) - rubyzip (1.2.1) - selenium-webdriver (3.4.4) - childprocess (~> 0.5) - rubyzip (~> 1.0) - sprockets (3.7.1) + rake (>= 12.2) + thor (~> 1.0) + zeitwerk (~> 2.5) + rake (13.0.6) + thor (1.2.2) + timeout (0.3.2) + tzinfo (2.0.6) concurrent-ruby (~> 1.0) - rack (> 1, < 3) - sprockets-export (1.0.0) - sprockets-rails (3.2.0) - actionpack (>= 4.0) - activesupport (>= 4.0) - sprockets (>= 3.0.0) - thin (1.7.2) - daemons (~> 1.0, >= 1.0.9) - eventmachine (~> 1.0, >= 1.0.4) - rack (>= 1, < 3) - thor (0.19.4) - thread_safe (0.3.6) - tzinfo (1.2.3) - thread_safe (~> 0.1) - uglifier (3.2.0) - execjs (>= 0.3.0, < 3) - useragent (0.16.8) - websocket-driver (0.6.5) + websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.2) + websocket-extensions (0.1.5) + zeitwerk (2.6.8) PLATFORMS ruby DEPENDENCIES - blade - blade-sauce_labs_plugin - coffee-script - coffee-script-source rails rails-dom-testing rake - sprockets - sprockets-export - uglifier BUNDLED WITH - 1.14.3 + 2.4.14 diff --git a/README.md b/README.md index 64d6531..5d87b56 100644 --- a/README.md +++ b/README.md @@ -145,3 +145,6 @@ LocalTime.config.locale = "es" [![Build Status](https://travis-ci.org/basecamp/local_time.svg?branch=master)](https://travis-ci.org/basecamp/local_time) [![Sauce Test Status](https://saucelabs.com/browser-matrix/basecamp_local_time.svg)](https://saucelabs.com/u/basecamp_local_time) + +## Contributing +Please read [CONTRIBUTING.md](./CONTRIBUTING.md). diff --git a/Rakefile b/Rakefile index 3d5789c..3f0c445 100644 --- a/Rakefile +++ b/Rakefile @@ -14,52 +14,58 @@ namespace :test do task assets: ["assets:compile", "assets:verify"] task :javascripts do - abort unless system "bin/blade ci" + system "yarn start" end end namespace :assets do desc "Compile assets" task :compile do - require "blade" - require "sprockets" - require "sprockets/export" - Blade.build + puts "[compile] JS assets" + system "yarn build" end desc "Verify compiled assets" task :verify do - file = "app/assets/javascripts/local-time.js" - pathname = Pathname.new("#{__dir__}/#{file}") + verify_file "app/assets/javascripts/local-time.es2017-umd.js" + verify_file "test/javascripts/builds/index.js" + verify_requireable + end +end - print "[verify] #{file} exists " - if pathname.exist? - puts "[OK]" - else - puts "[FAIL]" - fail - end +def verify_file(file) + pathname = Pathname.new("#{__dir__}/#{file}") + + print "[verify] #{file} exists " + if pathname.exist? + puts "[OK]" + else + puts "[FAIL]" + fail + end + + print "[verify] #{file} is a UMD module " + if pathname.read =~ /module\.exports.*define\.amd/m + puts "[OK]" + else + $stderr.puts "[FAIL]" + fail + end +end - print "[verify] #{file} is a UMD module " - if pathname.read =~ /module\.exports.*define\.amd/m - puts "[OK]" - else - $stderr.puts "[FAIL]" - fail - end +def verify_requireable + print "[verify] #{__dir__} can be required as a module " - print "[verify] #{__dir__} can be required as a module " - js = <<-JS - window = {} - document = { documentElement: {} } - require("#{__dir__}") - JS - _, stderr, status = Open3.capture3("node", "--print", js) - if status.success? - puts "[OK]" - else - puts "[FAIL]\n#{stderr}" - fail - end + js = <<-JS + window = {} + document = { documentElement: {} } + require("#{__dir__}") + JS + _, stderr, status = Open3.capture3("node", "--print", js) + if status.success? + puts "[OK]" + else + puts "[FAIL]\n#{stderr}" + fail end end diff --git a/app/assets/javascripts/local-time.es2017-esm.js b/app/assets/javascripts/local-time.es2017-esm.js new file mode 100644 index 0000000..2465aff --- /dev/null +++ b/app/assets/javascripts/local-time.es2017-esm.js @@ -0,0 +1 @@ +var t;t={config:{},run:function(){return this.getController().processElements()},process:function(...t){var e,r,a;for(r=0,a=t.length;r11?"pm":"am")).toUpperCase();case"P":return y("time."+(n>11?"pm":"am"));case"S":return f(o,l);case"w":return a;case"y":return f(u%100,l);case"Y":return u;case"Z":return g(t)}}))},f=function(t,e){return"-"===e?t:`0${t}`.slice(-2)},g=function(t){var e,r,a,n,s;return(e=null!=(r=(s=t.toString()).match(/\(([\w\s]+)\)$/))?r[1]:void 0)?/\s/.test(e)?e.match(/\b(\w)/g).join(""):e:(e=null!=(a=s.match(/(\w{3,4})\s\d{4}$/))?a[1]:void 0)||(e=null!=(n=s.match(/(UTC[\+\-]\d+)/))?n[1]:void 0)?e:""},C.CalendarDate=class{static fromDate(t){return new this(t.getFullYear(),t.getMonth()+1,t.getDate())}static today(){return this.fromDate(new Date)}constructor(t,e,r){this.date=new Date(Date.UTC(t,e-1)),this.date.setUTCDate(r),this.year=this.date.getUTCFullYear(),this.month=this.date.getUTCMonth()+1,this.day=this.date.getUTCDate(),this.value=this.date.getTime()}equals(t){return(null!=t?t.value:void 0)===this.value}is(t){return this.equals(t)}isToday(){return this.is(this.constructor.today())}occursOnSameYearAs(t){return this.year===(null!=t?t.year:void 0)}occursThisYear(){return this.occursOnSameYearAs(this.constructor.today())}daysSince(t){if(t)return(this.date-t.date)/864e5}daysPassed(){return this.constructor.today().daysSince(this)}},({strftime:b,translate:S,getI18nValue:p}=C),C.RelativeTime=class{constructor(t){this.date=t,this.calendarDate=C.CalendarDate.fromDate(this.date)}toString(){var t,e;return(e=this.toTimeElapsedString())?S("time.elapsed",{time:e}):(t=this.toWeekdayString())?(e=this.toTimeString(),S("datetime.at",{date:t,time:e})):S("date.on",{date:this.toDateString()})}toTimeOrDateString(){return this.calendarDate.isToday()?this.toTimeString():this.toDateString()}toTimeElapsedString(){var t,e,r,a,n;return r=(new Date).getTime()-this.date.getTime(),a=Math.round(r/1e3),e=Math.round(a/60),t=Math.round(e/60),r<0?null:a<10?(n=S("time.second"),S("time.singular",{time:n})):a<45?`${a} ${S("time.seconds")}`:a<90?(n=S("time.minute"),S("time.singular",{time:n})):e<45?`${e} ${S("time.minutes")}`:e<90?(n=S("time.hour"),S("time.singularAn",{time:n})):t<24?`${t} ${S("time.hours")}`:""}toWeekdayString(){switch(this.calendarDate.daysPassed()){case 0:return S("date.today");case 1:return S("date.yesterday");case-1:return S("date.tomorrow");case 2:case 3:case 4:case 5:case 6:return b(this.date,"%A");default:return""}}toDateString(){var t;return t=this.calendarDate.occursThisYear()?p("date.formats.thisYear"):p("date.formats.default"),b(this.date,t)}toTimeString(){return b(this.date,p("time.formats.default"))}},({elementMatchesSelector:M}=C),C.PageObserver=class{constructor(t,e){this.processMutations=this.processMutations.bind(this),this.processInsertion=this.processInsertion.bind(this),this.selector=t,this.callback=e}start(){if(!this.started)return this.observeWithMutationObserver()||this.observeWithMutationEvent(),this.started=!0}observeWithMutationObserver(){if("undefined"!=typeof MutationObserver&&null!==MutationObserver)return new MutationObserver(this.processMutations).observe(document.documentElement,{childList:!0,subtree:!0}),!0}observeWithMutationEvent(){return addEventListener("DOMNodeInserted",this.processInsertion,!1),!0}findSignificantElements(t){var e;return e=[],(null!=t?t.nodeType:void 0)===Node.ELEMENT_NODE&&(M(t,this.selector)&&e.push(t),e.push(...t.querySelectorAll(this.selector))),e}processMutations(t){var e,r,a,n,s,i,o,u;for(e=[],r=0,n=t.length;r11?"pm":"am")).toUpperCase();case"P":return p("time."+(n>11?"pm":"am"));case"S":return f(o,l);case"w":return a;case"y":return f(u%100,l);case"Y":return u;case"Z":return g(t)}}))},f=function(t,e){return"-"===e?t:`0${t}`.slice(-2)},g=function(t){var e,r,a,n,s;return(e=null!=(r=(s=t.toString()).match(/\(([\w\s]+)\)$/))?r[1]:void 0)?/\s/.test(e)?e.match(/\b(\w)/g).join(""):e:(e=null!=(a=s.match(/(\w{3,4})\s\d{4}$/))?a[1]:void 0)||(e=null!=(n=s.match(/(UTC[\+\-]\d+)/))?n[1]:void 0)?e:""},C.CalendarDate=class{static fromDate(t){return new this(t.getFullYear(),t.getMonth()+1,t.getDate())}static today(){return this.fromDate(new Date)}constructor(t,e,r){this.date=new Date(Date.UTC(t,e-1)),this.date.setUTCDate(r),this.year=this.date.getUTCFullYear(),this.month=this.date.getUTCMonth()+1,this.day=this.date.getUTCDate(),this.value=this.date.getTime()}equals(t){return(null!=t?t.value:void 0)===this.value}is(t){return this.equals(t)}isToday(){return this.is(this.constructor.today())}occursOnSameYearAs(t){return this.year===(null!=t?t.year:void 0)}occursThisYear(){return this.occursOnSameYearAs(this.constructor.today())}daysSince(t){if(t)return(this.date-t.date)/864e5}daysPassed(){return this.constructor.today().daysSince(this)}},({strftime:b,translate:S,getI18nValue:v}=C),C.RelativeTime=class{constructor(t){this.date=t,this.calendarDate=C.CalendarDate.fromDate(this.date)}toString(){var t,e;return(e=this.toTimeElapsedString())?S("time.elapsed",{time:e}):(t=this.toWeekdayString())?(e=this.toTimeString(),S("datetime.at",{date:t,time:e})):S("date.on",{date:this.toDateString()})}toTimeOrDateString(){return this.calendarDate.isToday()?this.toTimeString():this.toDateString()}toTimeElapsedString(){var t,e,r,a,n;return r=(new Date).getTime()-this.date.getTime(),a=Math.round(r/1e3),e=Math.round(a/60),t=Math.round(e/60),r<0?null:a<10?(n=S("time.second"),S("time.singular",{time:n})):a<45?`${a} ${S("time.seconds")}`:a<90?(n=S("time.minute"),S("time.singular",{time:n})):e<45?`${e} ${S("time.minutes")}`:e<90?(n=S("time.hour"),S("time.singularAn",{time:n})):t<24?`${t} ${S("time.hours")}`:""}toWeekdayString(){switch(this.calendarDate.daysPassed()){case 0:return S("date.today");case 1:return S("date.yesterday");case-1:return S("date.tomorrow");case 2:case 3:case 4:case 5:case 6:return b(this.date,"%A");default:return""}}toDateString(){var t;return t=this.calendarDate.occursThisYear()?v("date.formats.thisYear"):v("date.formats.default"),b(this.date,t)}toTimeString(){return b(this.date,v("time.formats.default"))}},({elementMatchesSelector:M}=C),C.PageObserver=class{constructor(t,e){this.processMutations=this.processMutations.bind(this),this.processInsertion=this.processInsertion.bind(this),this.selector=t,this.callback=e}start(){if(!this.started)return this.observeWithMutationObserver()||this.observeWithMutationEvent(),this.started=!0}observeWithMutationObserver(){if("undefined"!=typeof MutationObserver&&null!==MutationObserver)return new MutationObserver(this.processMutations).observe(document.documentElement,{childList:!0,subtree:!0}),!0}observeWithMutationEvent(){return addEventListener("DOMNodeInserted",this.processInsertion,!1),!0}findSignificantElements(t){var e;return e=[],(null!=t?t.nodeType:void 0)===Node.ELEMENT_NODE&&(M(t,this.selector)&&e.push(t),e.push(...t.querySelectorAll(this.selector))),e}processMutations(t){var e,r,a,n,s,i,o,u;for(e=[],r=0,n=t.length;r11?"pm":"am")).toUpperCase();case"P":return i("time."+(c>11?"pm":"am"));case"S":return n(h,m);case"w":return u;case"y":return n(f%100,m);case"Y":return f;case"Z":return r(e)}})},n=function(t,e){switch(e){case"-":return t;default:return("0"+t).slice(-2)}},r=function(t){var e,n,r,a,i;return i=t.toString(),(e=null!=(n=i.match(/\(([\w\s]+)\)$/))?n[1]:void 0)?/\s/.test(e)?e.match(/\b(\w)/g).join(""):e:(e=null!=(r=i.match(/(\w{3,4})\s\d{4}$/))?r[1]:void 0)?e:(e=null!=(a=i.match(/(UTC[\+\-]\d+)/))?a[1]:void 0)?e:""}}.call(this),function(){e.CalendarDate=function(){function t(t,e,n){this.date=new Date(Date.UTC(t,e-1)),this.date.setUTCDate(n),this.year=this.date.getUTCFullYear(),this.month=this.date.getUTCMonth()+1,this.day=this.date.getUTCDate(),this.value=this.date.getTime()}return t.fromDate=function(t){return new this(t.getFullYear(),t.getMonth()+1,t.getDate())},t.today=function(){return this.fromDate(new Date)},t.prototype.equals=function(t){return(null!=t?t.value:void 0)===this.value},t.prototype.is=function(t){return this.equals(t)},t.prototype.isToday=function(){return this.is(this.constructor.today())},t.prototype.occursOnSameYearAs=function(t){return this.year===(null!=t?t.year:void 0)},t.prototype.occursThisYear=function(){return this.occursOnSameYearAs(this.constructor.today())},t.prototype.daysSince=function(t){if(t)return(this.date-t.date)/864e5},t.prototype.daysPassed=function(){return this.constructor.today().daysSince(this)},t}()}.call(this),function(){var t,n,r;n=e.strftime,r=e.translate,t=e.getI18nValue,e.RelativeTime=function(){function a(t){this.date=t,this.calendarDate=e.CalendarDate.fromDate(this.date)}return a.prototype.toString=function(){var t,e;return(e=this.toTimeElapsedString())?r("time.elapsed",{time:e}):(t=this.toWeekdayString())?(e=this.toTimeString(),r("datetime.at",{date:t,time:e})):r("date.on",{date:this.toDateString()})},a.prototype.toTimeOrDateString=function(){return this.calendarDate.isToday()?this.toTimeString():this.toDateString()},a.prototype.toTimeElapsedString=function(){var t,e,n,a,i;return n=(new Date).getTime()-this.date.getTime(),a=Math.round(n/1e3),e=Math.round(a/60),t=Math.round(e/60),n<0?null:a<10?(i=r("time.second"),r("time.singular",{time:i})):a<45?a+" "+r("time.seconds"):a<90?(i=r("time.minute"),r("time.singular",{time:i})):e<45?e+" "+r("time.minutes"):e<90?(i=r("time.hour"),r("time.singularAn",{time:i})):t<24?t+" "+r("time.hours"):""},a.prototype.toWeekdayString=function(){switch(this.calendarDate.daysPassed()){case 0:return r("date.today");case 1:return r("date.yesterday");case-1:return r("date.tomorrow");case 2:case 3:case 4:case 5:case 6:return n(this.date,"%A");default:return""}},a.prototype.toDateString=function(){var e;return e=t(this.calendarDate.occursThisYear()?"date.formats.thisYear":"date.formats.default"),n(this.date,e)},a.prototype.toTimeString=function(){return n(this.date,t("time.formats.default"))},a}()}.call(this),function(){var t,n=function(t,e){return function(){return t.apply(e,arguments)}};t=e.elementMatchesSelector,e.PageObserver=function(){function e(t,e){this.selector=t,this.callback=e,this.processInsertion=n(this.processInsertion,this),this.processMutations=n(this.processMutations,this)}return e.prototype.start=function(){if(!this.started)return this.observeWithMutationObserver()||this.observeWithMutationEvent(),this.started=!0},e.prototype.observeWithMutationObserver=function(){var t;if("undefined"!=typeof MutationObserver&&null!==MutationObserver)return t=new MutationObserver(this.processMutations),t.observe(document.documentElement,{childList:!0,subtree:!0}),!0},e.prototype.observeWithMutationEvent=function(){return addEventListener("DOMNodeInserted",this.processInsertion,!1),!0},e.prototype.findSignificantElements=function(e){var n;return n=[],(null!=e?e.nodeType:void 0)===Node.ELEMENT_NODE&&(t(e,this.selector)&&n.push(e),n.push.apply(n,e.querySelectorAll(this.selector))),n},e.prototype.processMutations=function(t){var e,n,r,a,i,o,s,u;for(e=[],n=0,a=t.length;n new this date.getFullYear(), date.getMonth() + 1, date.getDate() diff --git a/lib/assets/javascripts/src/local-time/config/i18n.coffee b/lib/assets/javascripts/src/local-time/config/i18n.coffee index 2598ab2..48c9ae3 100644 --- a/lib/assets/javascripts/src/local-time/config/i18n.coffee +++ b/lib/assets/javascripts/src/local-time/config/i18n.coffee @@ -1,3 +1,5 @@ +import LocalTime from "../local_time" + LocalTime.config.i18n = en: date: diff --git a/lib/assets/javascripts/src/local-time/config/index.coffee b/lib/assets/javascripts/src/local-time/config/index.coffee new file mode 100644 index 0000000..512e86f --- /dev/null +++ b/lib/assets/javascripts/src/local-time/config/index.coffee @@ -0,0 +1,3 @@ +import "./i18n" +import "./locale" +import "./timer" diff --git a/lib/assets/javascripts/src/local-time/config/locale.coffee b/lib/assets/javascripts/src/local-time/config/locale.coffee index 9d12916..91b9566 100644 --- a/lib/assets/javascripts/src/local-time/config/locale.coffee +++ b/lib/assets/javascripts/src/local-time/config/locale.coffee @@ -1,2 +1,4 @@ +import LocalTime from "../local_time" + LocalTime.config.locale = "en" LocalTime.config.defaultLocale = "en" diff --git a/lib/assets/javascripts/src/local-time/config/timer.coffee b/lib/assets/javascripts/src/local-time/config/timer.coffee index 166a2ee..09bb234 100644 --- a/lib/assets/javascripts/src/local-time/config/timer.coffee +++ b/lib/assets/javascripts/src/local-time/config/timer.coffee @@ -1 +1,3 @@ +import LocalTime from "../local_time" + LocalTime.config.timerInterval = 60 * 1000 diff --git a/lib/assets/javascripts/src/local-time/controller.coffee b/lib/assets/javascripts/src/local-time/controller.coffee index 62e6fe0..4fe894c 100644 --- a/lib/assets/javascripts/src/local-time/controller.coffee +++ b/lib/assets/javascripts/src/local-time/controller.coffee @@ -1,5 +1,6 @@ -#= require ./relative_time -#= require ./page_observer +import LocalTime from "./local_time" +import "./relative_time" +import "./page_observer" {parseDate, strftime, getI18nValue, config} = LocalTime diff --git a/lib/assets/javascripts/src/local-time/helpers/date_parse.coffee b/lib/assets/javascripts/src/local-time/helpers/date_parse.coffee index ce3b22d..bd69618 100644 --- a/lib/assets/javascripts/src/local-time/helpers/date_parse.coffee +++ b/lib/assets/javascripts/src/local-time/helpers/date_parse.coffee @@ -1,3 +1,5 @@ +import LocalTime from "../local_time" + # Older browsers do not support ISO8601 (JSON) timestamps in Date.parse supportsISO8601 = not isNaN Date.parse("2011-01-01T12:00:00-05:00") diff --git a/lib/assets/javascripts/src/local-time/helpers/dom.coffee b/lib/assets/javascripts/src/local-time/helpers/dom.coffee index bbf340f..2231ae7 100644 --- a/lib/assets/javascripts/src/local-time/helpers/dom.coffee +++ b/lib/assets/javascripts/src/local-time/helpers/dom.coffee @@ -1,11 +1,13 @@ - LocalTime.elementMatchesSelector = do -> - element = document.documentElement - method = element.matches ? - element.matchesSelector ? - element.webkitMatchesSelector ? - element.mozMatchesSelector ? - element.msMatchesSelector +import LocalTime from "../local_time" - (element, selector) -> - if element?.nodeType is Node.ELEMENT_NODE - method.call(element, selector) +LocalTime.elementMatchesSelector = do -> + element = document.documentElement + method = element.matches ? + element.matchesSelector ? + element.webkitMatchesSelector ? + element.mozMatchesSelector ? + element.msMatchesSelector + + (element, selector) -> + if element?.nodeType is Node.ELEMENT_NODE + method.call(element, selector) diff --git a/lib/assets/javascripts/src/local-time/helpers/i18n.coffee b/lib/assets/javascripts/src/local-time/helpers/i18n.coffee index 0fe29a7..321a9d3 100644 --- a/lib/assets/javascripts/src/local-time/helpers/i18n.coffee +++ b/lib/assets/javascripts/src/local-time/helpers/i18n.coffee @@ -1,3 +1,5 @@ +import LocalTime from "../local_time" + {config} = LocalTime {i18n} = config diff --git a/lib/assets/javascripts/src/local-time/helpers/index.coffee b/lib/assets/javascripts/src/local-time/helpers/index.coffee new file mode 100644 index 0000000..d411cd0 --- /dev/null +++ b/lib/assets/javascripts/src/local-time/helpers/index.coffee @@ -0,0 +1,4 @@ +import "./date_parse" +import "./dom" +import "./i18n" +import "./strftime" diff --git a/lib/assets/javascripts/src/local-time/helpers/strftime.coffee b/lib/assets/javascripts/src/local-time/helpers/strftime.coffee index a6f331a..ebb0e4d 100644 --- a/lib/assets/javascripts/src/local-time/helpers/strftime.coffee +++ b/lib/assets/javascripts/src/local-time/helpers/strftime.coffee @@ -1,3 +1,5 @@ +import LocalTime from "../local_time" + {getI18nValue, translate} = LocalTime LocalTime.strftime = strftime = (time, formatString) -> diff --git a/lib/assets/javascripts/src/local-time/index.coffee b/lib/assets/javascripts/src/local-time/index.coffee index bb3ffcc..c463391 100644 --- a/lib/assets/javascripts/src/local-time/index.coffee +++ b/lib/assets/javascripts/src/local-time/index.coffee @@ -1,20 +1,7 @@ -#= export LocalTime -#= require_self -#= require_tree ./config -#= require_tree ./helpers -#= require ./controller -#= require ./start +import LocalTime from "./local_time" +import "./config" +import "./helpers" +import "./controller" +import "./start" -@LocalTime = - config: {} - - run: -> - @getController().processElements() - - process: (elements...) -> - for element in elements - @getController().processElement(element) - elements.length - - getController: -> - @controller ?= new LocalTime.Controller +export default LocalTime diff --git a/lib/assets/javascripts/src/local-time/local_time.coffee b/lib/assets/javascripts/src/local-time/local_time.coffee new file mode 100644 index 0000000..5ddb442 --- /dev/null +++ b/lib/assets/javascripts/src/local-time/local_time.coffee @@ -0,0 +1,15 @@ +LocalTime = + config: {} + + run: -> + @getController().processElements() + + process: (elements...) -> + for element in elements + @getController().processElement(element) + elements.length + + getController: -> + @controller ?= new LocalTime.Controller + +export default LocalTime diff --git a/lib/assets/javascripts/src/local-time/page_observer.coffee b/lib/assets/javascripts/src/local-time/page_observer.coffee index 10cc7d9..99950e1 100644 --- a/lib/assets/javascripts/src/local-time/page_observer.coffee +++ b/lib/assets/javascripts/src/local-time/page_observer.coffee @@ -1,3 +1,5 @@ +import LocalTime from "./local_time" + {elementMatchesSelector} = LocalTime class LocalTime.PageObserver diff --git a/lib/assets/javascripts/src/local-time/relative_time.coffee b/lib/assets/javascripts/src/local-time/relative_time.coffee index 396f105..41f616b 100644 --- a/lib/assets/javascripts/src/local-time/relative_time.coffee +++ b/lib/assets/javascripts/src/local-time/relative_time.coffee @@ -1,4 +1,5 @@ -#= require ./calendar_date +import LocalTime from "./local_time" +import "./calendar_date" {strftime, translate, getI18nValue} = LocalTime diff --git a/lib/assets/javascripts/src/local-time/start.coffee b/lib/assets/javascripts/src/local-time/start.coffee index 69be78c..1625095 100644 --- a/lib/assets/javascripts/src/local-time/start.coffee +++ b/lib/assets/javascripts/src/local-time/start.coffee @@ -1,3 +1,5 @@ +import LocalTime from "./local_time" + started = false domReady = -> diff --git a/package.json b/package.json index f33dd59..c173356 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ - { "name": "local-time", "version": "2.1.0", "description": "Local