Skip to content

Commit

Permalink
resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
dethe committed Feb 20, 2015
2 parents d9084fe + 87fb52c commit 5887dbe
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 346 deletions.
2 changes: 1 addition & 1 deletion css/widget.css
Expand Up @@ -99,7 +99,7 @@ wb-search{
/* ACCORDION */

wb-accordion{
overfow: visible;
overflow: visible;
padding-left: 6px;
}

Expand Down
28 changes: 19 additions & 9 deletions js/app.js
Expand Up @@ -25,24 +25,34 @@ function info(text){

// Documentation for modal dialogs: https://github.com/kylepaulsen/NanoModal

Event.on(document.body, 'click', '.do-run', preload);
Event.on(document.body, 'ui:click', '.do-run', startScript);
Event.on(document.body, 'ui:click', '.do-stop', stopScript);

function preload(){
assets.load({
function startScript() {
preload().whenLoaded(runScript);
}

function stopScript() {
runtime.stopEventLoop();
runtime.clear();
}

function preload() {
return assets.load({
'wb-contains wb-expression[isAsset=true]': assets.loadMedia,
'wb-contains wb-expression[script^="geolocation."]':
assets.waitFor('locationchanged', util.geolocation.startTrackingLocation),
'wb-contains wb-expression[script="motion.tiltDirection"]':
assets.waitFor('motionchanged', util.motion.startTrackingMotion)
}).whenLoaded(run);
});
}

function run(){
var scope = {};
function runScript(){
var globalScope = {};
runtime.startEventLoop();
dom.findAll('wb-workspace > wb-contains > *').forEach(function(block){
if (block.run){
block.run(scope);
block.run(globalScope);
}
});
}
Expand Down Expand Up @@ -91,13 +101,13 @@ function handleFileButton(evt){
fileModel.show();
}

Event.on(document.body, 'click', '.open-files', handleFileButton);
Event.on(document.body, 'ui:click', '.open-files', handleFileButton);

function handleExampleButton(evt){
window.alert('No current examples or templates available. Check back later!');
}

Event.on(document.body, 'click', '.open-example', handleExampleButton);
Event.on(document.body, 'ui:click', '.open-example', handleExampleButton);

window.app = {
message: message,
Expand Down
35 changes: 19 additions & 16 deletions js/block.js
Expand Up @@ -130,6 +130,7 @@ StepProto.run = function(scope){
var fnName = this.getAttribute('script').split('.');
this.fn = runtime[fnName[0]][fnName[1]];
}
_gaq.push(['_trackEvent', 'Blocks', this.getAttribute('script')]);
// console.log('calling step %s with scope %s, values %o', this.getAttribute('script'), scope, this.gatherValues(scope));
return this.fn.apply(scope, this.gatherValues(scope));
};
Expand Down Expand Up @@ -179,11 +180,11 @@ function handleVariableBlur(evt){
oldVariableName = '';
}

Event.on(document.body, 'focus', '[isVariable] input', handleVariableFocus); // Mozilla
Event.on(document.body, 'focusin', '[isVariable] input', handleVariableFocus); // All other browsers
Event.on(document.body, 'input', '[isVariable] input', handleVariableInput);
Event.on(document.body, 'blur', '[isVariable] input', handleVariableBlur); // Mozilla
Event.on(document.body, 'focusout', '[isVariable] input', handleVariableBlur); // All other browsers
Event.on(document.body, 'editor:focus', '[isVariable] input', handleVariableFocus); // Mozilla
Event.on(document.body, 'editor:focusin', '[isVariable] input', handleVariableFocus); // All other browsers
Event.on(document.body, 'editor:input', '[isVariable] input', handleVariableInput);
Event.on(document.body, 'editor:blur', '[isVariable] input', handleVariableBlur); // Mozilla
Event.on(document.body, 'editor:focusout', '[isVariable] input', handleVariableBlur); // All other browsers

// Context Proto
// Instantiated as new WBContext or as <wb-context>
Expand Down Expand Up @@ -224,6 +225,7 @@ ContextProto.run = function(parentScope){
var scope = util.extend({}, parentScope);
// expressions are eagerly evaluated against scope, contains are late-evaluated
// I'm not yet sure if this is the Right Thing™
_gaq.push(['_trackEvent', 'Blocks', this.getAttribute('script')]);
// console.log('calling context %s with scope %o, values %o', this.getAttribute('script'), scope, this.gatherValues(scope));
return this.fn.call(scope, this.gatherValues(scope), this.gatherContains());
};
Expand Down Expand Up @@ -266,8 +268,8 @@ ContextProto.hideLocals = function(evt){

window.WBContext = document.registerElement('wb-context', {prototype: ContextProto});

Event.on(document.body, 'wb-added', 'wb-context', function(evt){ evt.target.showLocals(evt); });
Event.on(document.body, 'wb-added', 'wb-context', function(evt){ evt.target.hideLocals(evt); });
Event.on(document.body, 'editor:wb-added', 'wb-context', function(evt){ evt.target.showLocals(evt); });
Event.on(document.body, 'editor:wb-added', 'wb-context', function(evt){ evt.target.hideLocals(evt); });
/*****************
*
* wb-expression
Expand Down Expand Up @@ -343,6 +345,7 @@ ExpressionProto.run = function(scope){
var fnName = this.getAttribute('script').split('.');
this.fn = runtime[fnName[0]][fnName[1]];
}
_gaq.push(['_trackEvent', 'Blocks', this.getAttribute('script')]);
// console.log('calling expression %s with scope %o and values %o', this.getAttribute('script'), scope, this.gatherValues(scope));
return this.fn.apply(scope, this.gatherValues(scope));
};
Expand Down Expand Up @@ -419,7 +422,7 @@ function toggleClosed(evt){
}
}

Event.on(workspace, 'click', 'wb-disclosure', toggleClosed);
Event.on(workspace, 'editor:click', 'wb-disclosure', toggleClosed);

/*****************
*
Expand Down Expand Up @@ -454,7 +457,7 @@ function addItem(evt){
template.parentElement.insertBefore(newItem, template.nextElementSibling);
}

Event.on(document.body, 'click', 'wb-contains .add-item', addItem);
Event.on(document.body, 'editor:click', 'wb-contains .add-item', addItem);

/*****************
*
Expand All @@ -474,7 +477,7 @@ function removeItem(evt){
}
}

Event.on(document.body, 'click', 'wb-contains .remove-item', removeItem);
Event.on(document.body, 'editor:click', 'wb-contains .remove-item', removeItem);



Expand Down Expand Up @@ -583,7 +586,7 @@ ValueProto.getValue = function(scope){
ValueProto.attachedCallback = insertIntoHeader;
window.WBValue = document.registerElement('wb-value', {prototype: ValueProto});

Event.on(document.body, 'change', 'wb-contains input, wb-contains select', function(evt){
Event.on(document.body, 'editor:change', 'wb-contains input, wb-contains select', function(evt){
dom.closest(evt.target, 'wb-value').setAttribute('value', evt.target.value);
});

Expand All @@ -606,7 +609,7 @@ var dropTarget = null;
var BLOCK_MENU = document.querySelector('sidebar');
var blockTop = 0;

Event.on(document.body, 'drag-start', 'wb-step, wb-step *, wb-context, wb-context *, wb-expression, wb-expression *', function(evt){
Event.on(document.body, 'editor:drag-start', 'wb-step, wb-step *, wb-context, wb-context *, wb-expression, wb-expression *', function(evt){
origTarget = dom.closest(evt.target, 'wb-step, wb-context, wb-expression');
// Maybe move to object notation later
// return target.startDrag(evt);
Expand Down Expand Up @@ -634,7 +637,7 @@ Event.on(document.body, 'drag-start', 'wb-step, wb-step *, wb-context, wb-contex
dragTarget.style.top = (evt.pageY - 15) + 'px';
});

Event.on(document.body, 'dragging', null, function(evt){
Event.on(document.body, 'editor:dragging', null, function(evt){
if (!dragTarget){ return; }

// FIXME: hardcoded margin (???) values.
Expand Down Expand Up @@ -701,7 +704,7 @@ function dropTargetIsContainer(potentialDropTarget){
}
}

Event.on(document.body, 'drag-end', null, function(evt){
Event.on(document.body, 'editor:drag-end', null, function(evt){
if (!dropTarget){
if(dragTarget){
dragTarget.parentElement.removeChild(dragTarget);
Expand Down Expand Up @@ -752,13 +755,13 @@ Event.on(document.body, 'drag-end', null, function(evt){
resetDragging();
});

Event.on(document.body, 'drag-cancel', null, function(evt){
Event.on(document.body, 'editor:drag-cancel', null, function(evt){
dragTarget.parentElement.removeChild(dragTarget);
resetDragging();
});

// Handle resizing inputs when their content changes
Event.on(document.body, 'input', 'input', function(evt){
Event.on(document.body, 'editor:input', 'input', function(evt){
var target = evt.target;
if (! dom.matches(target, 'wb-value > input')) return;
resize(target);
Expand Down
112 changes: 69 additions & 43 deletions js/event.js
Expand Up @@ -19,6 +19,13 @@
// Maintain references to events so we can mass-remove them later
var allEvents = {};

// Give all scoped events a class for future reference.
function ScopedEvent(elem, eventname, listener) {
this.element = elem;
this.name = eventname;
this.listener = listener;
}

// Refactor this into an constructor with prototype methods
function cloneEvent(evt){
var newEvent = {};
Expand All @@ -38,35 +45,48 @@
return newEvent;
}

function on(elem, eventname, selector, handler, onceOnly){
var ns_name = eventname.split(':');
var namespace = 'global';
if (ns_name.length === 2){
namespace = ns_name[0];
eventname = ns_name[1];
}
if (typeof elem === 'string'){
// Bind all elements matched by `elem` selector. Not recommended due to
// multiple event listeners used when one could suffice and be
// matched using the `selector` argument.
return dom.makeArray(document.querySelectorAll(elem)).map(function(e){
return on(e, eventname, selector, handler);
});
function on(elem, originalEventname, selector, handler, onceOnly){
var eventname, ns_name, namespace;

// Argument validation.
if (typeof originalEventname !== 'string'){
console.error('second argument must be eventname: %s', eventname);
throw new Error('second argument must be eventname: ' + eventname);
}
if (!isDomObject(elem)){
console.error('first argument must be element, document, or window: %o', elem);
throw new Error('first argument must be element, document, or window');
}
if (typeof eventname !== 'string'){
console.error('second argument must be eventname: %s', eventname);
throw new Error('second argument must be eventname: ' + eventname);
}
if (selector && typeof selector !== 'string'){
throw new TypeError('third argument must be selector String or null');
}
if (typeof handler !== 'function'){
throw new TypeError('fourth argument must be handler');
}

// Allow use of selector for `elem`.
if (typeof elem === 'string'){
// Bind all elements matched by `elem` selector. Not recommended due to
// multiple event listeners used when one could suffice and be
// matched using the `selector` argument.
return dom.makeArray(document.querySelectorAll(elem)).map(function(e){
return on(e, originalEventname, selector, handler, onceOnly);
});
}

// Check for presence of namespace.
ns_name = originalEventname.split(':');
if (ns_name.length === 2){
namespace = ns_name[0];
eventname = ns_name[1];
} else if (ns_name.length === 1) {
namespace = 'global';
eventname = originalEventname;
console.warn('Registering `' + eventname + '` in the global namespace');
} else {
throw new Error('Invalid namespace: ' + originalEventname);
}

var listener = function listener(originalEvent){
var evt;
if (originalEvent.detail && originalEvent.detail.forwarded){
Expand All @@ -79,7 +99,7 @@
return;
}
if (onceOnly){
Event.off(elem, eventname, listener);
Event.off(elem, originalEventname, listener);
}
if (selector){
if (dom.matches(evt.target, selector)){
Expand All @@ -95,32 +115,37 @@
}
};
elem.addEventListener(eventname, listener, false);
util.setDefault(allEvents, namespace, []).push([elem, eventname, listener]);
util.setDefault(allEvents, namespace, []).push(new ScopedEvent(elem, eventname, listener));
return listener;
}

function off(elem, eventname, handler){
var events;
var ns_name = eventname.split(':');
var namespace = 'global';
if (ns_name.length === 2){
// It's a scoped event.
namespace = ns_name[0];
eventname = ns_name[1];
}
if (handler){
elem.removeEventListener(eventname, handler);
}else{
allEvents[namespace].slice().forEach(function(elem_name_hand, idx){
// Pass in null element to remove listeners from all elements
var el = elem || elem_name_hand[0];
var en = eventname === '*' ? elem_name_hand[1] : eventname;
var hd = elem_name_hand[2];
if (el === elem_name_hand[0] && eventname === elem_name_hand[1]){
elem.removeEventListener(elem_name_hand[1], elem_name_hand[2]);
allEvents[namespace].splice(idx, 1); // remove elem_name_hand from allEvents
}
});
events = allEvents[namespace];

if (!events) {
// No events registered for the given namespace.
return;
}

// Copy the array, because we'll be modifying it.
events.slice().forEach(function(scopedEvent, idx){
// Pass in null element to remove listeners from all elements
var el = elem || scopedEvent.element;
var name = eventname === '*' ? scopedEvent.name : eventname;
var listener = handler || scopedEvent.listener;
if (el === scopedEvent.element && name === scopedEvent.name){
el.removeEventListener(name, listener);
allEvents[namespace].splice(idx, 1); // remove the event from allEvents
}
});
}

function once(elem, eventname, selector, handler){
Expand Down Expand Up @@ -360,19 +385,20 @@
stagePointerX: 0,
stagePointerY: 0,
keys: {},
keyHandlers: {}
keyHandlers: {},
clearRuntime: clearRuntime
};


Event.on(document.body, 'touchstart', null, initDrag);
Event.on(document.body, 'touchmove', null, dragging);
Event.on(document.body, 'touchend', null, endDrag);
Event.on(document.body, 'mousedown', null, initDrag);
Event.on(document.body, 'mousemove', null, dragging);
Event.on(window, 'mouseup', null, endDrag);
Event.on(window, 'keyup', null, cancelDrag);
Event.on(window, 'keydown', null, handleKeyDown);
Event.on(window, 'keyup', null, handleKeyUp);
Event.on(document.body, 'dragging:touchstart', null, initDrag);
Event.on(document.body, 'dragging:touchmove', null, dragging);
Event.on(document.body, 'dragging:touchend', null, endDrag);
Event.on(document.body, 'dragging:mousedown', null, initDrag);
Event.on(document.body, 'dragging:mousemove', null, dragging);
Event.on(window, 'dragging:mouseup', null, endDrag);
Event.on(window, 'dragging:keyup', null, cancelDrag);
Event.on(window, 'input:keydown', null, handleKeyDown);
Event.on(window, 'input:keyup', null, handleKeyUp);


})();
12 changes: 6 additions & 6 deletions js/file.js
Expand Up @@ -225,11 +225,11 @@
}
}

Event.on(window, 'load', null, loadCurrentScripts);
Event.on(window, 'beforeunload', null, saveCurrentScripts);
Event.on(document.body, 'wb-added', null, bareUrl); // Remove gist or other argument on script change
Event.on(document.body, 'wb-removed', null, bareUrl);
Event.on(document.body, 'wb-changed', null, bareUrl);
Event.on(window, 'ui:load', null, loadCurrentScripts);
Event.on(window, 'ui:beforeunload', null, saveCurrentScripts);
Event.on(document.body, 'ui:wb-added', null, bareUrl); // Remove gist or other argument on script change
Event.on(document.body, 'ui:wb-removed', null, bareUrl);
Event.on(document.body, 'ui:wb-changed', null, bareUrl);

window.File = {
scriptsToString: scriptsToString,
Expand All @@ -243,6 +243,6 @@
loadScriptsFromFilesystem: loadScriptsFromFilesystem,
loadCurrentScripts: loadCurrentScripts,
getFiles: getFiles
}
};

})();

0 comments on commit 5887dbe

Please sign in to comment.