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

Promises reworked, menubar functionality in events/options improved #30

Open
wants to merge 16 commits into
base: Milestone-Enhancements
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42,297 changes: 21,198 additions & 21,099 deletions dist/atv.js

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions dist/atv.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "atvjs",
"version": "0.2.5",
"version": "0.2.6",

Choose a reason for hiding this comment

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

I suggest update major version because of breaking changes in this PR.

Here are major semver rules:

  1. 1.X.X — Major update for breaking changes. Even if they are mostly backward compatible
  2. X.1.X — Minor update for new features with backward compatibility.
  3. X.X.1 — Patch update only for bugfixes.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, it's true. I could not afford to change major version of library that was created by another person)

"description": "Blazing fast Apple TV application development using pure JavaScript.",
"keywords": [
"atvjs",
Expand Down
43 changes: 32 additions & 11 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,57 @@ let handlers = {
*
* @private
* @param {Event} e The event passed while this handler was invoked
* @return {Promise}
*/
onMenuItemSelect(e) {
let element = e.target;
let menuId = element.getAttribute('id');
let elementType = element.nodeName.toLowerCase();
let page = element.page;
let options = element.options;

if (elementType === 'menuitem') {
// no need to proceed if the page is already loaded or there is no page definition present
if ((!element.pageDoc || element.getAttribute(menuItemReloadAttribute)) && page) {
// set a loading message intially to the menuitem
Menu.setDocument(Navigation.getLoaderDoc(Menu.getLoadingMessage()), menuId)
// load the page
page().then((doc) => {
// Loading element
let loading = function (res) {
Menu.setDocument(res, menuId);
};
// Load page
let loadPage = function(){
return page(options);
};
// Page loaded
let pageLoaded = function(doc) {
// if there is a document loaded, assign it to the menuitem
if (doc) {
// assign the pageDoc to disable reload everytime
element.pageDoc = doc;
Menu.setDocument(doc, menuId);
}
// dissmiss any open modals
// dismiss any open modals
Navigation.dismissModal();
}, (error) => {
};
// Error occurred
let error = function(error) {
// if there was an error loading the page, set an error page to the menu item
Menu.setDocument(Navigation.getErrorDoc(error), menuId);
// dissmiss any open modals
Navigation.dismissModal();
});
return Navigation.getErrorDoc(error)
.then(function (res) {
Menu.setDocument(res, menuId);
// dismiss any open modals
Navigation.dismissModal();
});
};

return Navigation
.getLoaderDoc(Menu.getLoadingMessage())
.then(loading)
.then(loadPage)
.then(pageLoaded)
.catch(error);
}
}
return Promise.resolve(false)
}
}
};
Expand All @@ -130,7 +151,7 @@ let handlers = {
function setOptions(cfg = {}) {
console.log('setting handler options...', cfg);
// override the default options
_.defaultsDeep(handlers, cfg.handlers);
_.defaultsDeep(handlers, cfg.handlers);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// external libraries
import 'babel-polyfill';
if (!global._babelPolyfill) {
require('babel-polyfill');
}
import _ from 'lodash';
import PubSub from 'pubsub-js';
import LZString from 'lz-string';
Expand Down
53 changes: 33 additions & 20 deletions src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import _ from 'lodash';
import Parser from './parser';

// base menu string for initial document creation
const docStr = '<document><menuBarTemplate><menuBar></menuBar></menuBarTemplate></document>';

let docStr = '<document><menuBarTemplate><menuBar></menuBar></menuBarTemplate></document>';
// indicate whether the menu was created
let created = false;

// few private instances
let doc = Parser.dom(docStr);
let menuBarEl = (doc.getElementsByTagName('menuBar')).item(0);
let menuBarFeature = menuBarEl && menuBarEl.getFeature('MenuBarDocument');
let doc;
let menuBarEl;
let menuBarFeature;
let itemsCache = {};

// default menu options
Expand Down Expand Up @@ -44,13 +43,13 @@ function setAttributes(el, attributes) {
/**
* Returns instance of the menu document (auto create if not already created)
*
* @return {Document} Instance of the created menu document.
* @return {Promise} Promise with instance of the created menu document.
*/
function get() {
if (!created) {
create();
return create();
}
return doc;
return Promise.resolve(doc);
}

/**
Expand All @@ -74,6 +73,8 @@ function addItem(item = {}) {
el.innerHTML = `<title>${(_.isFunction(item.name) ? item.name() : item.name)}</title>`;
// add page reference
el.page = item.page;
// add page options reference
el.options = item.options;
// appends to the menu
menuBarEl.insertBefore(el, null);
// cache for later use
Expand All @@ -83,29 +84,41 @@ function addItem(item = {}) {
}

/**
* Generates a menu from the configuration obejct.
* Generates a menu from the configuration object.
*
* @param {Object} cfg Menu related configurations
* @return {Document} The created menu document
* @return {Promise<Document>} Promise with created menu document
*/
function create(cfg = {}) {
if (created) {
console.warn('An instance of menu already exists, skipping creation...');
return;
return Promise.resolve(doc);
}
// defaults
_.assign(defaults, cfg);

console.log('creating menu...', defaults);

// set attributes to the menubar element
setAttributes(menuBarEl, defaults.attributes);
// add all items to the menubar
_.each(defaults.items, (item) => addItem(item));
// indicate done
created = true;

return doc;

let afterMenuDocCreated = function (res) {
doc = res;
menuBarEl = (doc.getElementsByTagName('menuBar')).item(0);
menuBarFeature = menuBarEl && menuBarEl.getFeature('MenuBarDocument');
// set attributes to the menubar element
setAttributes(menuBarEl, defaults.attributes);
// add all items to the menubar
_.each(defaults.items, (item) => addItem(item));
// indicate done
created = true;
return doc;
};

if(!!defaults.template){
// if template was specified, use it.
docStr = _.isFunction(defaults.template) ? defaults.template() : defaults.template
}

return Parser.dom(docStr)
.then(afterMenuDocCreated);
}

/**
Expand Down
Loading