forked from tomgreenfield/adapt-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update: Migrate to ES6 syntax (fixes #6)
- Loading branch information
Showing
5 changed files
with
207 additions
and
182 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
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,59 @@ | ||
import Adapt from 'core/js/adapt'; | ||
|
||
class InspectorContainerView extends Backbone.View { | ||
initialize() { | ||
const id = this.model.get('_id'); | ||
|
||
this.listenTo(Adapt, 'remove', this.remove); | ||
this.addTracURL(); | ||
this.$el.data(this.model); | ||
Adapt.trigger('inspector:id', id); | ||
} | ||
|
||
events() { | ||
return { | ||
mouseenter: 'onHover', | ||
mouseleave: 'onHover', | ||
touchend: 'onTouch' | ||
}; | ||
} | ||
|
||
addTracURL() { | ||
const config = Adapt.config.get('_inspector')._trac; | ||
if (!config || !config._isEnabled) return; | ||
|
||
const params = config._params || { | ||
summary: '{{_id}}{{#if displayTitle}} {{{displayTitle}}}{{/if}}{{inspector_location}}' | ||
}; | ||
|
||
const $div = $('<div>'); | ||
const data = this.model.toJSON(); | ||
let tracUrl = `${config._url}/newticket?`; | ||
|
||
for (const key in params) { | ||
if (!Object.prototype.hasOwnProperty.call(params, key)) continue; | ||
|
||
const value = $div.html(Handlebars.compile(params[key])(data)).text(); | ||
|
||
tracUrl += `&${key}=${encodeURIComponent(value)}`; | ||
} | ||
|
||
this.model.set('_tracUrl', tracUrl); | ||
} | ||
|
||
onHover() { | ||
_.defer(() => Adapt.trigger('inspector:hover')); | ||
} | ||
|
||
onTouch(event) { | ||
if (event.originalEvent.stopInspectorPropagation) return; | ||
|
||
event.originalEvent.stopInspectorPropagation = true; | ||
|
||
if ($(event.target).is('[class*=inspector-]')) return; | ||
|
||
Adapt.trigger('inspector:touch', this.$el); | ||
} | ||
} | ||
|
||
export default InspectorContainerView; |
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,115 @@ | ||
import Adapt from 'core/js/adapt'; | ||
import device from 'core/js/device'; | ||
|
||
class InspectorView extends Backbone.View { | ||
className() { | ||
return 'inspector'; | ||
} | ||
|
||
events() { | ||
return { | ||
mouseleave: 'onLeave' | ||
}; | ||
} | ||
|
||
initialize() { | ||
const config = Adapt.config.get('_inspector'); | ||
if (device.touch && config._isDisabledOnTouch) return; | ||
|
||
_.bindAll(this, 'onLeave', 'pushId', 'setVisibility', 'updateInspector', 'onResize', 'remove'); | ||
|
||
this.listenTo(Adapt, { | ||
'inspector:id': this.pushId, | ||
'inspector:hover': this.setVisibility, | ||
'inspector:touch': this.updateInspector, | ||
'device:resize': this.onResize, | ||
remove: this.remove | ||
}).render(); | ||
|
||
this.ids = []; | ||
} | ||
|
||
render() { | ||
$('#wrapper').append(this.$el); | ||
}; | ||
|
||
pushId(id) { | ||
this.ids.push(id); | ||
} | ||
|
||
setVisibility() { | ||
if (this.$el.is(':hover')) return; | ||
|
||
const reversedIds = this.ids.toReversed(); | ||
for (const id of reversedIds) { | ||
const $hovered = $(`[data-adapt-id="${id}"]:hover`); | ||
|
||
if ($hovered.length) return this.updateInspector($hovered); | ||
} | ||
|
||
$('.inspector-visible').removeClass('inspector-visible'); | ||
this.$el.hide(); | ||
} | ||
|
||
updateInspector($hovered) { | ||
const $previous = $('.inspector-visible'); | ||
if ($hovered.is($previous.last())) return; | ||
|
||
const data = []; | ||
const template = Handlebars.templates.inspector; | ||
|
||
$previous.removeClass('inspector-visible'); | ||
|
||
this.addOverlappedElements($hovered).each(function() { | ||
const $element = $(this); | ||
const attributes = $element.data().attributes; | ||
if (!attributes) return; | ||
|
||
data.push(attributes); | ||
$element.addClass('inspector-visible'); | ||
}); | ||
|
||
this.$el.html(template(data)).removeAttr('style').removeClass('inline'); | ||
|
||
const offset = $hovered.offset(); | ||
const offsetTop = offset.top; | ||
const targetTop = offsetTop - this.$el.outerHeight(); | ||
const shouldBeInline = targetTop < 0; | ||
|
||
this.$el.css({ | ||
top: shouldBeInline ? offsetTop : targetTop, | ||
left: offset.left + $hovered.outerWidth() / 2 - this.$el.width() / 2 | ||
}).toggleClass('inline', shouldBeInline); | ||
} | ||
|
||
addOverlappedElements($hovered) { | ||
this.ids.forEach((id) => { | ||
const $element = $(`[data-adapt-id="${id}"]`); | ||
this.checkOverlap($element, $hovered); | ||
}); | ||
|
||
return $hovered; | ||
} | ||
|
||
checkOverlap($element, $hovered) { | ||
const areOffsetsEqual = _.isEqual($element.offset(), $hovered.offset()); | ||
const areWidthsEqual = $element.width() === $hovered.width(); | ||
const isOverlapped = $element.height() && areOffsetsEqual && areWidthsEqual; | ||
|
||
if (isOverlapped) $hovered = $hovered.add($element); | ||
} | ||
|
||
onResize() { | ||
const $hovered = $('.inspector-visible'); | ||
if (!$hovered.length) return; | ||
|
||
$hovered.removeClass('inspector-visible'); | ||
this.updateInspector($hovered.last()); | ||
} | ||
|
||
onLeave() { | ||
_.defer(this.setVisibility.bind(this)); | ||
} | ||
}; | ||
|
||
export default InspectorView; |
Oops, something went wrong.