Files
danbooru/app/javascript
evazion 2cbce9a525 notes: rewrite notes Javascript to object-oriented style.
Rewrite the notes Javascript from a procedural style to an
object-oriented style.

Before the notes Javascript had a lot of problems:

* There was hidden state everywhere, both locally and globally. We had
  state in global variables, in <meta> tags, in DOM data-* attributes
  (on multiple elements), and in jQuery .data() properties (which are
  different from data-* attributes, because they aren't visible in the
  DOM).

* Local state was hard to reason about. There was lots of direct DOM
  manipulation in random places. Functions had to constantly pass around
  note ids and look up elements in the DOM to get the state. State was
  invisible because it was stored as jQuery .data() properties. It was
  hard to follow where state was stored, how it was initialized, and how
  it changed.

* Global state was also a mess. There were a lot of global flags and
  variables only used in specific situations. Almost all of this state was
  unnecessary. Global state also prevented us from doing things like
  loading or unloading posts dynamically, or showing multiple posts with
  notes on the same page.

* There was a lot of duplication of code, especially for placing notes,
  and for loading or saving new notes versus saved notes.

Now the code is organized in an object-oriented fashion:

* The Note class represents a single note. A post has a list of notes,
  and each note object has a Note.Box and a Note.Body. Together these
  objects encapsulate the note's state.

* Notes have methods for doing things like placing note boxes, or showing
  and hiding note bodies, or creating, saving, or deleting notes. This
  makes the JS API cleaner.

* Global state is kept to a minimum.

This is one big patch because it was too hard to make these changes
incrementally. There are a couple minor bugfixes, but the actual
behavior of notes should remain unchanged.

Bugfixes:

* It was possible to enter translation mode, start dragging a new note,
  then press N to leave translation mode while still dragging the note.
  If you did this, then you would be stuck in translation mode and you
  couldn't stop dragging the note.

* Placement of new notes is now pixel-perfect. Before when placing a
  note, the note would shift by 1-2 pixels.

* Previewing an empty note didn't show the "Click to edit" message.

Other noteworthy changes:

* Most global state has been eliminated. There were a lot of flags and
  variables stored as global variables on `Danbooru.Note`. Most of these
  turned out to be either unnecessary or even unused.

* Notes now have an explicit minimum size of 10x10 pixels. Before this
  limit was hardcoded and undocumented.

* A lot of the note placement and note creation code has been simplified.

* `Note.add()` and `Note.create()` have been refactored into `new Note()`.
  Before `Note.add` was used to load an existing note, while `Note.create`
  was used to create a new note. These did the same thing, but had
  slightly different behavior.

* `Note.Box.scale()` and `Note.box.update_data_attributes` have been
  refactored into `Note.Box.place_note()`. Contrary to their names,
  these functions were actually both used to place notes.
2020-07-28 22:06:35 -05:00
..
2019-08-14 01:46:44 -05:00