Skip to content

Commit

Permalink
1.0.0-beta.4 Improved hotspot editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Hintum committed Oct 31, 2019
1 parent 65c1a4a commit 68f8de9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.0-beta.4 - 2019-10-31
### Changed
- Improved hotspot editor.

## 1.0.0-beta.3 - 2019-10-30
### Added
- Added documentation
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "born05/craft-imagehotspots",
"description": "Image Hotspots fieldtype for Craft CMS",
"version": "1.0.0-beta.3",
"version": "1.0.0-beta.4",
"type": "craft-plugin",
"keywords": [
"hotspots",
Expand Down
8 changes: 8 additions & 0 deletions src/assetbundles/imagehotspotsfield/dist/css/Input.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
display: inline-block;
user-select: none;
z-index: 0;
width: 100%;
height: 100%;
}
.image-hotspot-editor .image-container.align-height {
width: auto;
}
.image-hotspot-editor .image-container.align-width {
height: auto;
}

.image-hotspot-editor img {
Expand Down
33 changes: 28 additions & 5 deletions src/assetbundles/imagehotspotsfield/dist/js/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ Born05.ImageHotspotEditor = Garnish.Modal.extend({
$cancelBtn: null,
$imgContainer: null,

asset: null,
pos: null,
listData: null,

init: function(assetUrl, pos, listData, settings) {
init: function(asset, pos, listData, settings) {
this.setSettings(settings, Born05.ImageHotspotEditor.defaults);

this.asset = asset;
this.pos = pos;
this.updateData(listData);

// Build the modal
Expand All @@ -21,19 +26,33 @@ Born05.ImageHotspotEditor = Garnish.Modal.extend({
var $footer = $('<div class="footer"/>').appendTo($container);

this.$primaryButtons = $('<div class="buttons right"/>').appendTo($footer);
this.$cancelBtn = $('<div class="btn">' + Craft.t('app', 'Cancel') + '</div>').appendTo(this.$primaryButtons);
this.$cancelBtn = $('<div class="btn submit">' + Craft.t('app', 'Done') + '</div>').appendTo(this.$primaryButtons);

this.$imgContainer = $('<div class="image-container"></div>').appendTo($body);
this.$img = $('<img src="'+assetUrl+'" alt="" />').appendTo(this.$imgContainer);
this.$img = $('<img src="'+this.asset.url+'" alt="" />').appendTo(this.$imgContainer);
this.$point = $('<div class="current"></div>').appendTo(this.$imgContainer);
this.setPoint(pos.x, pos.y);
this.setPoint(this.pos.x, this.pos.y);

this.base($container, this.settings);

this.addListener(this.$imgContainer, 'click', 'select');
this.addListener(this.$cancelBtn, 'activate', 'hide');
},

createImage: function(asset) {
this.$imgContainer.removeClass('align-height align-width');

var containerBounds = this.$imgContainer[0].getBoundingClientRect();
var containerRatio = containerBounds.width / containerBounds.height;
var imageRatio = asset.width / asset.height;

if (containerRatio > imageRatio) {
this.$imgContainer.addClass('align-height');
} else {
this.$imgContainer.addClass('align-width');
}
},

updateData: function(listData) {
this.listData = listData;
},
Expand All @@ -55,6 +74,10 @@ Born05.ImageHotspotEditor = Garnish.Modal.extend({
}, this);
},

onFadeIn: function() {
this.createImage(this.asset);
},

setPoint: function(x, y) {
this.$point.css({
top: (y * 100) + '%',
Expand All @@ -65,7 +88,7 @@ Born05.ImageHotspotEditor = Garnish.Modal.extend({
select: function(e) {
e.preventDefault();

const imageBounds = this.$img[0].getBoundingClientRect();
var imageBounds = this.$img[0].getBoundingClientRect();

var x = (e.clientX - imageBounds.left) / imageBounds.width;
var y = (e.clientY - imageBounds.top) / imageBounds.height;
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/imagehotspotsfield/dist/js/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Born05.ImageHotspotInput = Garnish.Base.extend({

createModal: function() {
return new Born05.ImageHotspotEditor(
this.settings.assetUrl,
this.settings.asset,
{
x: parseFloat(this.$xField.val() || 0.5),
y: parseFloat(this.$yField.val() || 0.5),
Expand Down
6 changes: 5 additions & 1 deletion src/templates/_components/field/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

{% set jsSettings = {
id: id|namespaceInputId,
assetUrl: (asset is not null) ? asset.url : null,
asset: (asset is not null) ? {
url: asset.url,
width: asset.width,
height: asset.height,
} : null,
} %}

{% js %}
Expand Down

0 comments on commit 68f8de9

Please sign in to comment.