forked from Famous/famous
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dom-flushing' into develop
- Loading branch information
Showing
13 changed files
with
842 additions
and
570 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Created by lundfall on 02/06/2017. | ||
*/ | ||
|
||
|
||
define(function (require, exports, module) { | ||
|
||
/** | ||
* Singleton class optimized for high performance in DOM updates. All DOM updates that are done through this class will | ||
* be cached and can be flushed at the same order the instructions came in. | ||
* | ||
* | ||
* @type {{}} | ||
*/ | ||
var DOMBuffer = {}; | ||
var enqueuedOperations = []; | ||
|
||
|
||
DOMBuffer.assignProperty = function (object, property, value) { | ||
enqueuedOperations.push({ data: [object, property, value], operation: 'assignProperty' }); | ||
}; | ||
|
||
DOMBuffer.setAttribute = function (element, attribute, value) { | ||
enqueuedOperations.push({ data: [element, attribute, value], operation: 'setAttribute' }); | ||
}; | ||
|
||
DOMBuffer.addToObject = function (object, value) { | ||
enqueuedOperations.push({ data: [object, value], operation: 'addToObject' }); | ||
}; | ||
|
||
DOMBuffer.setAttributeOnDescendants = function (element, attribute, attributeValue) { | ||
enqueuedOperations.push({ data: [element, attribute, attributeValue], operation: 'setAttributeOnDescendants' }); | ||
}; | ||
|
||
DOMBuffer.removeFromObject = function (object, attribute) { | ||
enqueuedOperations.push({ data: [object, attribute], operation: 'removeFromObject' }); | ||
}; | ||
|
||
DOMBuffer.removeAttribute = function (element, attribute) { | ||
enqueuedOperations.push({ data: [element, attribute], operation: 'removeAttribute' }); | ||
}; | ||
|
||
DOMBuffer.removeChild = function (parent, childToRemove) { | ||
enqueuedOperations.push({ data: [parent, childToRemove], operation: 'removeChild' }); | ||
}; | ||
|
||
DOMBuffer.appendChild = function (parent, childToAppend) { | ||
enqueuedOperations.push({ data: [parent, childToAppend], operation: 'appendChild' }); | ||
}; | ||
|
||
DOMBuffer.insertBefore = function (parent, childBefore, childToInsert) { | ||
enqueuedOperations.push({ data: [parent, childBefore, childToInsert], operation: 'insertBefore' }); | ||
}; | ||
|
||
DOMBuffer.flushUpdates = function () { | ||
for (var index = 0; index < enqueuedOperations.length; index++) { | ||
var enqueuedOperation = enqueuedOperations[index]; | ||
var operationName = enqueuedOperation.operation; | ||
var data = enqueuedOperation.data; | ||
switch (operationName) { | ||
case 'appendChild': | ||
data[0].appendChild(data[1]); | ||
break; | ||
case 'insertBefore': | ||
data[0].insertBefore(data[1], data[2]); | ||
break; | ||
case 'setAttribute': | ||
data[0].setAttribute(data[1], data[2]); | ||
break; | ||
case 'removeChild': | ||
if (data[0].childNodes.length && data[0].contains(data[1])) { | ||
data[0].removeChild(data[1]); | ||
} | ||
break; | ||
case 'removeAttribute': | ||
data[0].removeAttribute(data[1]); | ||
break; | ||
case 'addToObject': | ||
data[0].add(data[1]); | ||
break; | ||
case 'removeFromObject': | ||
data[0].remove(data[1]); | ||
break; | ||
case 'assignProperty': | ||
data[0][data[1]] = data[2]; | ||
break; | ||
case 'setAttributeOnDescendants': | ||
/* Gets all the descendants for element | ||
* https://stackoverflow.com/questions/26325278/how-can-i-get-all-descendant-elements-for-parent-container | ||
* */ | ||
var descendants = data[0].querySelectorAll("*"); | ||
for (var i = 0; i < descendants.length; i++) { | ||
descendants[i].setAttribute(data[1], data[2]); | ||
} | ||
break; | ||
} | ||
} | ||
enqueuedOperations = []; | ||
}; | ||
|
||
module.exports = DOMBuffer; | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
/** | ||
* Created by lundfall on 01/06/2017. | ||
*/ | ||
|
||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* Owner: [email protected] | ||
* @license MPL 2.0 | ||
* @copyright Famous Industries, Inc. 2015 | ||
*/ | ||
|
||
define(function (require, exports, module) { | ||
|
||
var DOMEventHandler = {}; | ||
var EventEmitter = require('./EventEmitter.js'); | ||
var DOMBuffer = require('./DOMBuffer'); | ||
|
||
//TODO Add more to complete list | ||
var singleElementEvents = [ | ||
'submit', 'focus', 'blur', 'load', 'unload', 'change', 'reset', 'scroll' | ||
]; | ||
|
||
var initializedListeners = {}; | ||
|
||
DOMEventHandler.isNativeEvent = function(eventName) { | ||
return typeof document.body["on" + eventName] !== "undefined" | ||
|| | ||
/* Needed because otherwise not able to use mobile emulation in browser! */ | ||
['touchmove', 'touchstart', 'touchend'].includes(eventName); | ||
}; | ||
|
||
DOMEventHandler.addEventListener = function(id, element, type, callback){ | ||
if(!DOMEventHandler.isNativeEvent(type)){ | ||
return; | ||
} | ||
|
||
if(singleElementEvents.includes(type)){ | ||
return element.addEventListener(type, callback); | ||
} | ||
DOMBuffer.setAttribute(element, 'data-arvaid', id); | ||
var eventEmitter = initializedListeners[type]; | ||
if(!eventEmitter){ | ||
eventEmitter = initializedListeners[type] = new EventEmitter(); | ||
window.addEventListener(type, function (event) { | ||
var target = event.relatedTarget || event.target; | ||
var recievedID = target && target.getAttribute && target.getAttribute('data-arvaid'); | ||
if(recievedID){ | ||
eventEmitter.emit(recievedID, event); | ||
} | ||
}); | ||
} | ||
eventEmitter.on(id, callback); | ||
|
||
}; | ||
|
||
DOMEventHandler.removeEventListener = function(element, id, type, callback) { | ||
if(singleElementEvents.includes(type)){ | ||
return element.addEventListener(type, callback); | ||
} | ||
if(initializedListeners[type]){ | ||
initializedListeners[type].removeListener(id, callback); | ||
} | ||
}; | ||
|
||
module.exports = DOMEventHandler; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.