window.Copyray = {} return unless $ = window.jQuery # Max CSS z-index. The overlay and copyray bar use this. MAX_ZINDEX = 2147483647 # Initialize Copyray. Called immediately, but some setup is deferred until DOM ready. Copyray.init = do -> return if Copyray.initialized Copyray.initialized = true is_mac = navigator.platform.toUpperCase().indexOf('MAC') isnt -1 # Register keyboard shortcuts $(document).keydown (e) -> # cmd + shift + k if (is_mac and e.metaKey or !is_mac and e.ctrlKey) and e.shiftKey and e.keyCode is 75 if Copyray.isShowing then Copyray.hide() else Copyray.show() if Copyray.isShowing and e.keyCode is 27 # esc Copyray.hide() $ -> # Instantiate the overlay singleton. new Copyray.Overlay # Go ahead and do a pass on the DOM to find templates. Copyray.findBlurbs() # Ready to rock. console?.log "Ready to Copyray. Press #{if is_mac then 'cmd+shift+k' else 'ctrl+shift+k'} to scan your UI." # Returns all currently created Copyray.Specimen objects. Copyray.specimens = -> Copyray.BlurbSpecimen.all # Looks up the stored constructor info Copyray.constructorInfo = (constructor) -> if window.CopyrayPaths for own info, func of window.CopyrayPaths return JSON.parse(info) if func == constructor null # Scans the document for blurbs, creating Copyray.BlurbSpecimen for them. Copyray.findBlurbs = -> util.bm 'findBlurbs', -> # Find all comments comments = $('*:not(iframe,script)').contents().filter -> this.nodeType == 8 and this.data[0..12] == "COPYRAY START" # Find the comment for each. Everything between the for comment in comments [_, id, path, url] = comment.data.match(/^COPYRAY START (\d+) (\S*) (\S*)/) $blurbContents = new jQuery el = comment.nextSibling until !el or (el.nodeType == 8 and el.data == "COPYRAY END #{id}") if el.nodeType == 1 and el.tagName != 'SCRIPT' $blurbContents.push el el = el.nextSibling # Remove COPYRAY template comments from the DOM. el.parentNode.removeChild(el) if el?.nodeType == 8 comment.parentNode.removeChild(comment) # Add the template specimen Copyray.BlurbSpecimen.add $blurbContents, name: path.split('/').slice(-1)[0] path: path url: url # Open the given filesystem path by calling out to Copyray's server. Copyray.open = (url) -> window.open(url, null, 'width=700, height=500') # Show the Copyray overlay Copyray.show = (type = null) -> Copyray.Overlay.instance().show(type) # Hide the Copyray overlay Copyray.hide = -> Copyray.Overlay.instance().hide() Copyray.toggleSettings = -> Copyray.Overlay.instance().settings.toggle() # Wraps a DOM element that Copyray is tracking. This is subclassed by # Copyray.Blurbsspecimen class Copyray.Specimen @add: (el, info = {}) -> @all.push new this(el, info) @remove: (el) -> @find(el)?.remove() @find: (el) -> el = el[0] if el instanceof jQuery for specimen in @all return specimen if specimen.el == el null @reset: -> @all = [] constructor: (contents, info = {}) -> @el = if contents instanceof jQuery then contents[0] else contents @$contents = $(contents) @name = info.name @path = info.path @url = info.url remove: -> idx = @constructor.all.indexOf(this) @constructor.all.splice(idx, 1) unless idx == -1 isVisible: -> @$contents.length and @$contents.is(':visible') makeBox: -> @bounds = util.computeBoundingBox(@$contents) @$box = $("