client/js/core/elem/elem.coffee in rsence-pre-3.0.0.4 vs client/js/core/elem/elem.coffee in rsence-pre-3.0.0.5
- old
+ new
@@ -26,11 +26,11 @@
###
The DOM Element abstraction engine
###
ELEM = HClass.extend
-
+
constructor: null
###
Main control for refresh speed
###
@@ -81,58 +81,58 @@
else
_id = @_nextElemId
@_nextElemId++
@_elements[_id] = _elem
_id
-
+
###
Initializes cache object helpers
###
_initCache: (_id)->
@_styleTodo[_id] = []
@_styleCache[_id] = {}
@_attrTodo[_id] = []
@_attrCache[_id] = {}
@_elemTodoH[_id] = false
null
-
+
###
Adds an existing document element by its id attribute
###
bindId: (_attrId)->
_elem = document.getElementById( _attrId )
_id = @_add( _elem )
@_initCache( _id )
_id
-
+
###
Binds the document element
###
bind: (_elem)->
_id = @_add( _elem )
@_initCache( _id )
_id
-
+
###
Returns an element by its id
###
get: (_id)->
@_elements[_id]
-
+
###
Sets the innerHTML contents of the element
###
setHTML: (_id, _html)->
@_elements[_id].innerHTML = _html unless @_elements[_id].innerHTML == _html
null
-
+
###
Returns the innerHTML of the element
###
getHTML: (_id)->
@_elements[_id].innerHTML
-
+
###
Deletes an element and its associated metadata
###
del: (_id)->
_elem = @_elements[_id]
@@ -142,32 +142,36 @@
delete @_styleCache[_id]
delete @_attrCache[_id]
delete @_elemTodoH[_id]
delete @_elements[_id]
@_freeElemIds.push(_id)
- _elem.parentNode.removeChild( _elem )
+ _parent = _elem.parentNode
+ if _parent?
+ _parent.removeChild( _elem )
+ else if !@isProduction
+ console.warn( 'ELEM.del(',_id,'): Invalid parent: ', _parent, " for elem:",_elem )
null
-
+
###
Places the source element inside the target element
###
append: (_srcId, _tgtId)->
@_elements[_tgtId].appendChild @_elements[_srcId]
-
+
###
- Replaces all styles of an element with a block of css text
+ Replaces all styles of an element with a block of css text
###
setCSS: (_id, _css)->
@_elements[_id].style.cssText = _css
null
-
+
###
Returns the current css text of an element
###
getCSS: (_id)->
@_elements[_id].style.cssText
-
+
###
Returns the visible size of an element as a [ width, height ] tuple
###
getVisibleSize: (_id)->
_elem = @_elements[_id]
@@ -181,18 +185,18 @@
h = _parentClientWidth - _elem.offsetTop if h > _parentClientHeight
_elem = _elem.parentNode
break unless _parent.parentNode
_parent = _parent.parentNode
[ w, h ]
-
+
###
Returns the full offset size of the element as a [ width, height ] tuple
###
getSize: (_id)->
_elem = @_elements[_id]
[ _elem.offsetWidth, _elem.offsetHeight ]
-
+
###
Returns the position of the element as a [ x, y ] tuple
###
getPosition: (_id)->
_elem = @_elements[_id]
@@ -209,11 +213,11 @@
Returns the scroll size of the element as a [ width, height ] tuple
###
getScrollSize: (_id)->
_elem = @_elements[_id]
[ _elem.scrollWidth, _elem.scrollHeight ]
-
+
###
Calculates the visible left position of an element
###
_getVisibleLeftPosition: (_id)->
_elem = @_elements[_id]
@@ -237,19 +241,19 @@
###
Returns the visible position of the element as a [ left, top ] tuple
###
getVisiblePosition: (_id)->
[ @_getVisibleLeftPosition(_id), @_getVisibleTopPosition(_id) ]
-
+
###
Returns the opacity on the element as a number equaling or between 0 and 1
###
getOpacity: (_id)->
_elem = @_elements[_id]
_opacity = @_getComputedStyle( _elem, 'opacity' )
parseFloat( _opacity )
-
+
###
Sets ActiveX alpha filter for IE6-8
###
_setOpacityIE: (_id, _opacity)->
_prevFilter = @getStyle( _id, 'filter', true )
@@ -266,17 +270,17 @@
if BROWSER_TYPE.ie7 or BROWSER_TYPE.ie8
@_setOpacityIE( _id, _opacity)
else
@_elements[_id].style.setProperty('opacity', _opacity.toString(), '')
null
-
+
###
Wrapper for getStyle, returns an integer number instead of a string
###
getIntStyle: (_id, _key)->
parseInt( @getStyle(_id, _key), 10 )
-
+
###
Sets element position ( id, [ x, y ] )
###
setPosition: (_id, x, y )->
unless y?
@@ -311,19 +315,19 @@
@setStyle( _id, 'left', x+'px' )
@setStyle( _id, 'top', y+'px' )
@setStyle( _id, 'width', w+'px' )
@setStyle( _id, 'height', h+'px' )
null
-
+
###
Gets box coordinates [ x, y, width, height )
###
getBoxCoords: (_id)->
[ x, y ] = @getPosition( _id )
[ w, h ] = @getSize( _id )
[ x, y, w, h ]
-
+
###
Computes extra size (padding and border size) of element
###
_getExtraSize: (_id, _side)->
@getIntStyle( _id, 'padding-'+_side ) + @getIntStyle( _id, 'border-'+_side+'-width' )
@@ -331,11 +335,11 @@
###
Returns left-side padding and border size
###
_getExtraLeftWidth: (_id)->
@_getExtraSize( _id, 'left' )
-
+
###
Returns right-side padding and border size
###
_getExtraRightWidth: (_id)->
@_getExtraSize( _id, 'right' )
@@ -343,45 +347,45 @@
###
Returns top-side padding and border size
###
_getExtraTopWidth: (_id)->
@_getExtraSize( _id, 'top' )
-
+
###
Returns right-side padding and border size
###
_getExtraBottomWidth: (_id)->
@_getExtraSize( _id, 'bottom' )
-
+
###
Returns extra width of element (caused by padding and borders)
###
getExtraWidth: (_id)->
@_getExtraSize( _id, 'left' ) + @_getExtraSize( _id, 'right' )
-
+
###
Returns extra height of element (caused by padding and borders)
###
getExtraHeight: (_id)->
@_getExtraSize( _id, 'top' ) + @_getExtraSize( _id, 'bottom' )
-
+
###
Sets delay between refreshes based on the target frame rate
###
setFPS: (_fps)->
@_minDelay = 1000/_fps
@_minDelay = @ELEMTickerInterval if @_minDelay < @ELEMTickerInterval
null
-
+
###
Sets slowness (weighted additional multiplier for slow browsers; frame-skip)
The d-efault 1.0 does not change the FPS, larger numbers gives more time for logic by skipping frames
###
setSlowness: (_slow)->
@_slowness = _slow
null
-
+
###
Sets the idle delay in ms
This is the maximum time between setting a style or property into the buffer and flushing the buffer to the DOM
###
setIdleDelay: (_idleDelay)->
@@ -395,11 +399,11 @@
_timeDelay = _delay unless _timeDelay
@_timer = setTimeout( ->
ELEM.flushLoop( _delay )
, _timeDelay )
null
-
+
###
Computes a default delay time based on various params
###
_defaultDelay: ->
_delay = Math.round( @_slowness * (@_flushTime / @_flushCounter) ) # + @ELEMTickerInterval ??
@@ -424,15 +428,15 @@
@_flushing = true
@_resetFlushLoop( _delay )
@_performFlush()
@_flushing = false
null
-
+
# Alias for flushLoop
flush: ->
@flushLoop()
-
+
###
Performs the flush of flushLoop
###
_performFlush: ->
_flushStartTime = new Date().getTime()
@@ -451,11 +455,11 @@
@_flushAttrCache( _id )
@_flushCounter++
@_flushTime += new Date().getTime()
@_needFlush = @_elemTodo.length != 0 # unless @_needFlush
null
-
+
###
Flushes the attribute cache
###
_flushAttrCache: (_id)->
_attrTodo = @_attrTodo[_id]
@@ -467,11 +471,11 @@
for i in [ 1.._loopMaxL ]
_key = _currTodo.shift()
_val = _attrCache[_key]
_elem[_key] = _val
null
-
+
###
Gets an element attribute directly from the element
###
_getAttrDirect: (_id, _key)->
_elem = @_elements[_id]
@@ -488,11 +492,11 @@
_val = @_attrCache[_id][_key]
if _noCache or not _val?
_val = @_getAttrDirect( _id, _key )
@_attrCache[_id][_key] = _val
_val
-
+
###
Sets a named element attribute into the cache and buffer or selectively direct
###
setAttr: (_id, _key, _value, _noCache)->
return null unless @_elements[_id]? # item is deleted
@@ -507,11 +511,11 @@
unless @_elemTodoH[_id]
@_elemTodo.push( _id )
@_elemTodoH[ _id ] = true
@_checkNeedFlush()
true
-
+
###
Deletes a named element attribute
###
delAttr: (_id, _key)->
return null unless @_elements[_id]? # item is deleted
@@ -529,11 +533,11 @@
###
hasClassName: (_id, _className)->
return null unless @_elements[_id]? # item is deleted
_classNames = @_elements[_id].className.split(' ')
return !!~_classNames.indexOf( _className )
-
+
###
Adds a named CSS className to the element
###
addClassName: (_id, _className)->
return null unless @_elements[_id]? # item is deleted
@@ -545,11 +549,11 @@
_classNames = _elem.className.trim().split(' ')
_classNames.push(_className)
_elem.className = _classNames.join(' ')
@_attrCache[_id]['className'] = _elem.className
null
-
+
###
Removes a named CSS className of the element
###
delClassName: (_id, _className)->
return null unless @_elements[_id]? # item is deleted
@@ -573,34 +577,34 @@
@_needFlush = true
unless @_flushing
clearTimeout( @_timer)
@_resetFlushLoop( @_minDelay )
null
-
+
###
Low-level style property setter
###
_setElementStyle: (_elem, _key, _value)->
_elem.style.setProperty( _key, _value, '' )
null
-
+
###
Camelizes string (mostly used for IE attribute name conversions)
###
_camelize: (_str)->
_str.replace( /((-)([a-z])(\w))/g, ($0, $1, $2, $3, $4)->
$3.toUpperCase()+$4
)
-
+
###
Decamelizes string (used for js property to css property conversion)
###
_deCamelize: (_str)->
_str.replace( /(([A-Z])(\w))/g, ($0, $1, $2, $3)->
'-'+$2.toLowerCase()+$3
)
-
+
###
IE version of _setElementStyle
###
_setElementStyleIE: (_elem, _key, _value)->
try
@@ -638,11 +642,11 @@
unless @_elemTodoH[_id]
@_elemTodo.push( _id )
@_elemTodoH[_id] = true
@_checkNeedFlush()
null
-
+
###
Sets multiple styles at once
###
setStyles: (_id, _styles, _noCache )->
if _styles instanceof Array
@@ -650,11 +654,11 @@
@setStyle( _id, _key, _value, _noCache )
else if typeof _styles == 'object'
for _key, _value of _styles
@setStyle( _id, _key, _value, _noCache )
null
-
+
###
Creates a new element inside another element
###
make: (_targetId, _tagName, _options)->
_targetId = 0 if _targetId == undefined
@@ -685,17 +689,17 @@
_size = [ window.innerWidth, window.innerHeight ]
if _size[0] == undefined or _size[1] == undefined
_docElem = document.documentElement
_size = [ _docElem.clientWidth, _docElem.clientHeight ]
_size
-
+
###
Returns computed style of element
###
_getComputedStyle: (_elem, _key)->
document.defaultView.getComputedStyle( _elem, null ).getPropertyValue( _key )
-
+
###
IE version of _getComputedStyle
###
_getComputedStyleIE: (_elem, _key)->
return _elem.clientWidth if _key == 'width'
@@ -740,11 +744,11 @@
@setOpacity( _id, _cached[_key] )
else
console.log( 'invalid style key:',_elem, _key, _cached) unless _key
@_setElementStyle( _elem, _key, _cached[_key] )
null
-
+
###
Final phase of startup, when document is loaded
###
_init: ->
RSenceInit() if RSenceInit?
@@ -753,11 +757,11 @@
@_setElementStyle = @_setElementStyleIE
@bind( document.body ) unless @_timer
@_flushDomLoadQueue() until @_initDone
@_resetFlushLoop( @_minDelay )
null
-
+
###
Runs a cmd
###
_runCmd: (_cmd)->
_type = ( typeof _cmd )
@@ -883,11 +887,11 @@
_browserType.ios = _iPhone or _iPad
_browserType.iphone = _iPhone
_browserType.ipad = _iPad
@_domWaiter()
null
-
+
###
Adds tasks to run when the document load check is completed
###
_domLoader: (_cmd)->
if ELEM._initDone
@@ -918,10 +922,10 @@
return
else if BROWSER_TYPE.safari and document.readyState == 'complete'
ELEM._domLoadStatus = true
else if document.body
ELEM._domLoadStatus = true
-
+
if ELEM._domLoadStatus
clearTimeout( @_domLoadTimer )
@_init()
else
ELEM._domLoadTimer = setTimeout( =>