Javascript API (application.js)

Note

DOM Element vs JavaScript Object

When talking about the DOM Element representing an item, I’ll use DOM .item — otherwise, for JavaScript or Python data sets, I’ll write: (Object/dict) Item.

Templates

class PageTemplate(data, name)
arg data:Object to pass to the template
arg name:name of the template (without the “view_” prefix)
PageTemplate.from([resource])
Arguments:
  • resource – optional resource to use instead of the data passed to constructor

Returns the HTML content

PageTemplate.draw([resource])
Arguments:
  • resource – optional resource to use instead of the data passed to constructor

Draws this template on screen, replacing #contents with PageTemplate.draw()

PageTemplate.clear()

Clears display

class ItemList(data, item_template)

Inherits PageTemplate()

Aims at handling a list of Resource()

Get the Resource() used for this link

Arguments:
  • link (String) – Child’s link
Return type:

Resource()

Refresh DOM informations of the item matching link

Arguments:
  • link (String) – Child’s link
  • metadata – an Object containing some properties to copy on this Resource()
ItemList.refresh_by_link(link, metadata)
ItemList.insert(resource)

Add a Resource() to current Page

ItemList.remove(resource)

Remove the given Resource()

ItemList.sort_by(dom_elt, criteria)

Call UI.fix_nav() and change the current sort criteria

ItemList.get_dom(link)

Get the DOM element used to display some child, by giving its link

Arguments:
  • link (String) – Child’s link
Returns:

The DOM element within that page

Resources and Items

class Resource(dict)

The most basic object you can work on

arg dict:the Object containing initial metadata for this Resource
Resource.type = 'resource'

More or less the lowercased name corresponding to this class name

Resource.searchable = 'title'

Space-separated list of properties available for filtering, see UI.filter_items()

Resource.dependencies = []

List of file names to load prior loading this item

Resource.stylesheet = false

Tells if a style.css file should be loaded for this kind of Item()

Resource.hg_size()

Returns a human readable size for this Item(), see UI.hr_size() .

Resource.getItem()

Returns a fresh item from this one, by requesting data to server.

Resource.post_view_callback()

Called when this item has been loaded. You may add your custom DOM processing here

Resource.edit()

Edit this item, if the Resource.link TODO: refactor it

Resource.del()

Deletes an item (from server)

Resource.view()

Displays an item, calling Nano.load_resource()

Resource.get_ref()

Returns resource’s path (HTML view)

Resource.get_raw_ref()

Returns resource’s path for RAW data

Resource.get_obj_ref()

Returns resource’s path for JSON metadata

Resource.get_obj_ref()

Returns resource’s path for JSON child resources list

class Item(dict)

Inherits Resource()

Just adds title from link in case it’s empty and sets a default description

GUI interactions

UI
The UI object ;)
UI.item_template = 'list_item_big'
UI.filter_items(filter)
Arguments:
  • filter (String) – (optional) pattern (regex to look for), if none given, #addsearch_form input is used

Filters the DOM content according to a pattern, if pattern is empty the display will be unfiltered. If pattern is prefixed by a name (without spaces) and colon (ex: type:), then the filtering will be done against this metadata name.

UI.fix_nav(link)

Handles the “click” on the given link in the .navbar (sort criteria)

Example usage:

<a href="#" onclick="UI.fix_nav(this); do_some_action();">link</a>
UI.hr_size(size)
Arguments:
  • size (Integer) – a number of bytes (file/data weight)
Returns:

Human readable size

Return type:

string

UI.render_dom(resource, opts)

Renders an Item() by calling it’s Resource.post_view_callback() after calling MimeManager.load_dependencies()

UI.edit_item(data)
Arguments:
UI.remove_item()

Removes the edited item and close the modal

UI.save_item()

Saves current item metadata

UI.find_item_from_child(dom)

Returns the DOM element owning the link from one of its child elements Useful to handle actions / clicks.

CORE FUNCTIONS

uncompress_resources(keys_values_array)

Uncompresses a list of “compact” (Object/dict) Items as returned by weye.root_objects.list_children() for instance.

Arguments:
  • keys_values_array

    tuple of property names and list of values. Ex:

    { 'c': ['link', 'age'], 'r': [ ['toto', 1], ['tata', 4], ['titi', 42] ] }
    
Returns:

“flat” array of objects. Ex:

[ {'link': 'toto', 'age': 1}, {'name': 'tata', 'age': 4}, {'name': 'titi', 'age': 42} ]

Nano

This is the main object to use in the API

Nano.doc_ref

Current document path, ex: “/”

Nano.content

Current document’s template, see ItemList()

Nano.current

Current Resource() in use (displayed / matches Nano.doc_ref)

Nano.mimes

Dictionary of “mime” : Item() with all registered mimes, see Defining a new mime type

Nano.set_content(item[, opts])
Displays given :arg:`item`
Arguments:
Nano.reload()

Reloads current Item()

Loads an Item() by its link name (using load_resource())

Arguments:
Nano.load_resource(resource[, opts])

Loads a Resource(), if it’s a shallow one (no size) then it will fetch the full object first. At the end, UI.render_dom() is called with the resource

Arguments:
Nano.level_up()

Back to upper level. Leaves the current navigation level and reach the parent calling Nano.load_link()

Arguments:
  • opts

    Available options:

    disable_history:
     passed (negatively) to Nano.load_link() as “history”
MimeManager

Object handling templates currently, will probably be refactored later.

MimeManager.find_choices(mime)
Arguments:
  • mime (String) – The original mime type, a list of mime types sorted by preference is returned
Return type:

Array of String

Returns:

The list of mimes

MimeManager.get_template(mime)

Get a template suitable for this mime type, the best value from MimeManager.find_choices() is returned

Arguments:
  • mime – The desired mime type
Returns:

a template

Return type:

Template()

MimeManager.load_dependencies(item[, opts])

Load dependencies for the given item

Arguments:
  • mime – The desired mime type
  • opts – Optional options :callback: a function called with the Resource() as parameter once all dependencies are loaded.

JavaScript reference

From MDN.

Object()
String()
Array()
Integer()

This Page