Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture code refactor for new interface #672

Merged
merged 12 commits into from
Aug 4, 2021

Conversation

waridrox
Copy link
Member

@waridrox waridrox commented Jul 2, 2021

Part of #645

Hi @jywarren, until further progress at the safari issue blocker, can we use this as an alternative.

I've implemented the following with this PR:

  • Refactoring the new capture code at new-capture.js file under assets that is only utilised by the new interface
  • Addition of login prompt modal (need a new capture controller to implement fully), overexposure warning messages.
  • Support for responsive layouts

I know it has a lot of changes but the problem is that the new-capture code requires the new interface which in turn requires the styles to function properly. So all of them are linked to each other...😅

I was thinking of implementing cross browser support on the old interface as well, but I think it will require some refactor of the old capture.js as well. So until the tests are implemented, I am putting that on hold...The asset pre-compilation test seems to be failing due to the use of ES6 arrow syntax under WebRTC.

getUserMedia: function(options) {
AdapterJS.webRTCReady(() => {
// The WebRTC API is ready.
const container = document.getElementById('webcam'),
video = document.createElement('video');
container.appendChild(video);
video.autoplay = true;
video.id = 'webcam-video'
const successCallback = stream => {
$('#heightIndicator').show()
$('#webcam-msg').hide()
attachMediaStream(video, stream)
if ($W.flipped == true) {
$W.flipped = false; // <= turn it false because f_h() will toggle it. messy.
$W.flip_horizontal();
}
};
const errorCallback = () => console.warn(error);

I've tried to fix it but still it's failing...it's related to Uglifier not being able to recognise the ES6 syntax.

@gitpod-io
Copy link

gitpod-io bot commented Jul 2, 2021

@waridrox waridrox force-pushed the captureRefactor branch 3 times, most recently from 13caa10 to 4abe613 Compare July 3, 2021 16:25
@waridrox waridrox changed the title Capture code refactor and responsive layout for new interface Capture code refactor for both old and new interface Jul 3, 2021
@waridrox waridrox force-pushed the captureRefactor branch 2 times, most recently from a87589d to 74370b3 Compare July 13, 2021 07:19
@waridrox waridrox changed the title Capture code refactor for both old and new interface Capture code refactor for old interface Jul 13, 2021
@waridrox waridrox force-pushed the captureRefactor branch 2 times, most recently from 279e9c3 to a52a5a4 Compare July 13, 2021 11:41
@waridrox
Copy link
Member Author

waridrox commented Jul 20, 2021

For reference, here's the new interface in action:

new.mov

@waridrox waridrox requested a review from jywarren July 20, 2021 00:23
@waridrox waridrox changed the title Capture code refactor for old interface Capture code refactor for new interface Jul 20, 2021
def webrtc?
request.env['HTTP_USER_AGENT'] && (request.env['HTTP_USER_AGENT'].match('Opera') || request.env['HTTP_USER_AGENT'].match('Firefox') || request.env['HTTP_USER_AGENT'].match('Chrome'))
request.env['HTTP_USER_AGENT'] && (request.env['HTTP_USER_AGENT'].match('Opera') || request.env['HTTP_USER_AGENT'].match('Safari') || request.env['HTTP_USER_AGENT'].match('Firefox') || request.env['HTTP_USER_AGENT'].match('Chrome'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@@ -17,7 +17,7 @@

<form class="form" action="/spectrums" method="post" id="saveForm" target="_blank" onSubmit="$W.cancelSave();window.location='#page1'">

<input placeholder="Title (required)" name="spectrum[title]" id="spectrum_notes" type="text" /><br />
<input placeholder="Title (required)" name="spectrum[title]" type="text" /><br />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, are you sure here? This is the legacy capture interface, would this break it?

Copy link
Member Author

@waridrox waridrox Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A warning was popping up in the console that there were 2 id's associated with the same name - spectrum_notes:

<input placeholder="Title (required)" name="spectrum[title]" id="spectrum_notes" type="text" /><br />

<textarea placeholder="Describe your spectrum: light source? Sample preparation? What's your goal?" style="span8" rows="6" name="spectrum[notes]" id="spectrum_notes"></textarea>

I don't see the id being used anywhere, so maybe it is ok to proceed 🤞

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, ok! As long as you can confirm it's not broken, sounds good! And indeed, a quick search shows it's not used in code anywhere -- https://github.com/publiclab/spectral-workbench/search?q=spectrum_notes

looks like it was simply duplicated! Good catch!

# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# Compress JavaScripts and CSS (Using ES6 syntax).
config.assets.js_compressor = Uglifier.new(harmony: true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha - ok so here we are missing something as it created this error:

Run bundle exec rake assets:precompile
rake aborted!
NameError: uninitialized constant Uglifier
/home/runner/work/spectral-workbench/spectral-workbench/config/environments/production.rb:38:in `block in <top (required)>'

https://github.com/publiclab/spectral-workbench/pull/672/checks?check_run_id=3109251932#step:5:7

Do you need to include Uglifier or something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually supposed to make the uglifier compatible with the ES6 syntax used in new-capture.js according to lautis/uglifier#127 but still the problem remains unsolved...😕

Copy link
Member Author

@waridrox waridrox Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is a new error uninitialised constant Uglifier which should be resolved by https://stackoverflow.com/questions/30338860/heroku-push-error-nameerror-uninitialized-constant-uglifierversion-on-rake but still the problem is persisting when I tried this out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, they note:

The problem seemed to be an incompatibility between Sprockets 3.1 and the Uglifier compressor version I was using, 2.1. Upgrading Uglifier by putting this line in my Gemfile resolved the issue:

gem 'uglifier', '~> 2.7'

Have you tried that? Else possibly @noi5e may be able to help a bit (or at least have some ideas?) as he's worked a lot with Rails precompilation in his React project!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK here is another possibility:

sstephenson/sprockets#740

Installing the gem "therubyracer" and rolling uglifier back to 2.6.1 in the Gemfile ( gem 'uglifier', '~> 2.6.1' ) fixes the problem.

...i think we removed therubyracer? Yes -

# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'

Try turning it back on and then pegging the uglifier gem to v2.6.1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -8,6 +8,7 @@
"dependencies": {
"ace-builds": "jywarren/ace-builds#v1.2.4",
"bootstrap-css": "jozefizso/bower-bootstrap-css#~2.3.2",
"bs-stepper": "^1.7.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@jywarren
Copy link
Member

Very cool @waridrox ! Looking awesome. I left some notes in the comments and once we get past this asset precompilation issue this looks good to merge!

@@ -0,0 +1,724 @@
//= require graph.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, hmm, so, is what's going on here that you've made a copy of the older version of capture.js (from https://github.com/publiclab/spectral-workbench.js/blob/main/examples/capture/capture.js) with some modifications in order to make it safari compatible?

But I'm not sure I'm understanding what the changes were -- here's a diff: https://gist.github.com/jywarren/534b0d8dfb4e58f7833ac92bdee8cd20/revisions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just not sure what those new sections auto_detect_sample_row etc are replacing - what older code are they overriding? I'd like to avoid duplicating the majority of this file if possible. What, as narrowly as possible, are the required changes we want to make here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if there is no other simpler way, what is our plan moving forward to stop using this redundant file and unify the codebase again?

Copy link
Member Author

@waridrox waridrox Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, hmm. I think perhaps this should be done upstream in that library, as well as perhaps in a separate PR. Is it possible to leave this file out and include exactly as it was in the demo?

<script src="../capture/capture.js" type="text/javascript"></script>
<script src="../../dist/capture.dist.js" type="text/javascript"></script>

I just think this is a major change and a duplication of code with the upstream library. Keep this file though! the refactor is not a bad idea it's just something we should do in the pure JS library, as well as in a distinct PR.

Copy link
Member Author

@waridrox waridrox Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! but

<script src="../capture/capture.js" type="text/javascript"></script>

is also calling different functions like getRow, getUsermedia, detect_sample_row which are in different files at https://github.com/publiclab/spectral-workbench.js/tree/main/examples/capture

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but are those functions represented in other files also included from the new-capture/index.html?

https://github.com/publiclab/spectral-workbench.js/blob/4f29a95119af0aef7a4a2e131ec4815df475e1a8/examples/new-capture/index.html#L27-L39

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than capture.dist.js and capture.js all other files are included as assets so I think there should be no problems there...

<span class="bs-stepper-label">Capture</span>
<script>
function capturePage() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof these are rough, huh? So basically the stepper plugin wasn't compatible with the canvas/getusermedia webcam stuff, when run in safari? Is this really the only way forward? It's so much extra code and work!

Could we at least move these all into one file so as not to mix them with the HTML?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stepper is just stopping the graph so I am rendering all the content first and then hiding it with the scripts instead of using the default stepper behaviour. Sure we could move this to a separate file for a cleaner code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also mark them as a temporary step. If we resolve the Safari issue, how much work would it be to revert back, and can we consolidate the code as well as clearly mark it with comments that it's addressing #665 and that upon solving that it could be removed and simplified?

Copy link
Member Author

@waridrox waridrox Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, the only thing that would be required in that case would be to simply remove the temporary script and add the pre-defined script section on the page which would use the buttons to toggle the divs that would then be rendered as different pages.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may even be helpful to make this particular set of changes all in a single commit. Then we can point at that commit which will include the complete diff of just this steps-fix, and nothing else.

<div>
<p><small>Using calibration:</small></p>
<select name="spectrum[calibration_id]" class="select-calibration select-calibration-capture span2">
<%= render partial: 'capture/calibrations', locals: { calibrations: @calibrations, calibration: @calibration } %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow very cool! Can you screenshot this as well, for documentation?

Copy link
Member Author

@waridrox waridrox Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still not functioning as intended (separated out) as of now since I tested it using the old capture controller. Will need to refactor this to the new capture controller.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be OK to use the same controller actions and routes between the two interfaces -- the functionality is the same, right?

@jywarren
Copy link
Member

Also just want to say, great work here, and keep it up! There's a lot going on and more to tweak before we're ready, but I appreciate the hard work!

@waridrox
Copy link
Member Author

@jywarren, I tried reverting changing the Uglifier version and installing rubyracer....still the asset pre-compilation fails 😞. @noi5e what are your thoughts on this ?

The error is NameError: uninitialized constant Uglifier

@waridrox waridrox requested a review from jywarren July 26, 2021 13:13
@jywarren
Copy link
Member

Oof, ok i'm sorry this is such a blocker. I'll try to work on it today or tomorrow. If @noi5e is able to see any way around this too, that's great as well!

@noi5e
Copy link

noi5e commented Jul 27, 2021

I should be honest and say that I don't have the expertise in this that might be desired. 😬 BUT, I am willing to get my hands dirty, maybe it will help to have an extra set of eyes on this.

I'm going to start by forking the repo and setting up locally. The comments are a lot to sift through here! Something that would help me: can you please give me a step-by-step to reproduce the error?

@waridrox
Copy link
Member Author

Sure @noi5e thanks a lot 🙌 !! The error is produced once the app gets ready for production and the assets pre-compile. We are using the ES6 arrow syntax in the sw.js lib at

https://github.com/publiclab/spectral-workbench.js/blob/4f29a95119af0aef7a4a2e131ec4815df475e1a8/dist/capture.dist.js#L259-L270

but the Uglifier is throwing an error

lautis/uglifier#127 was supposed to fix the ES6 issue but this created another one - NameError: uninitialized constant Uglifier

and so far we have tried -

but still no help 😕

@jywarren
Copy link
Member

jywarren commented Jul 28, 2021

Hmm, @waridrox I was actually able to run rake assets:precompile without an error here in Gitpod:

https://gist.github.com/jywarren/79073c832009089c58a885cfc5891f84

I wonder why this is different from the tests. Can you reproduce the error in Gitpod? I had to install bundler, yarn install, etc. first.

@waridrox
Copy link
Member Author

waridrox commented Jul 28, 2021

Oh @jywarren so could this be due to the bundler gem. I see the production environment set to false in the gitpod config file:

tasks:
- init: >
cp config/database.yml.gitpod config/database.yml &&
cp config/config.yml.example config/config.yml &&
cp db/schema.rb.example db/schema.rb &&
bundle install --without production &&
yarn install &&
rails db:create &&
rails db:schema:load &&
rails db:migrate

Also the commands like bundle install, yarn install are not executed automatically...which should otherwise execute as per the config 🤔

@jywarren
Copy link
Member

jywarren commented Jul 28, 2021 via email

@waridrox
Copy link
Member Author

waridrox commented Jul 28, 2021

Screenshot 2021-07-28 at 10 30 33 PM

Yeah now we see the error...

@jywarren
Copy link
Member

OK! I think i may have resolved it by using require 'uglifier' as in https://github.com/lautis/uglifier#usage

and by upgrading to the latest (v4) uglifier. Let's see. It'll need a rebase after but we can just see how it does here.

@waridrox waridrox force-pushed the captureRefactor branch 2 times, most recently from ec07d02 to 46ffb17 Compare July 29, 2021 11:50
@waridrox
Copy link
Member Author

waridrox commented Aug 2, 2021

Yay ✨ the test for asset pre-compilation passed!!

@@ -0,0 +1,610 @@
* {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's de-duplicate this with https://github.com/publiclab/spectral-workbench.js/blob/main/examples/new-capture/new-capture.css - and name it differently so it's clear that it isn't redundant - so this file in this PR should have only those things that are specific to the Rails app here, and everything else should go upstream in the JS library, and then https://github.com/publiclab/spectral-workbench.js/blob/main/examples/new-capture/new-capture.css would be referenced in addition to the local version here from index2.html. Make sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@waridrox i liked what you said about including both stylesheets in succession. Let's do that!

@waridrox
Copy link
Member Author

waridrox commented Aug 4, 2021

  • Reduced some css styling by utilising the already existing CSS styles from sw.js lib.
  • The files responsible for the new capture interface have been changed from new-capture.js and new-capture.css to new-interface.js and new-interface.css respectively.

@waridrox waridrox requested a review from jywarren August 4, 2021 20:34
@@ -0,0 +1,463 @@
@import 'spectral-workbench/examples/new-capture/new-capture.css';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@jywarren
Copy link
Member

jywarren commented Aug 4, 2021

OK! I think this is good to merge! Just in case, in the future can you comment that you think it's ready? But since we've reviewed this a lot i think we're OK. Merging! Great job!!

@jywarren jywarren merged commit 0a84661 into publiclab:main Aug 4, 2021
@noi5e
Copy link

noi5e commented Aug 6, 2021

Great job here @waridrox!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants