Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
- added ability to click enter in the field
  • Loading branch information
Slgoetz committed Apr 20, 2020
1 parent 491ef55 commit 09c295d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 55 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ 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.5 - 2020-04-15
### Cleanup
- Code Clean-up

## 1.0.4 - 2020-04-15
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "milkshakestudio/craft-brandfetch",
"description": "Craft CMS Plugin that allows you quickly get the logo of a website via the Brandfetch API.",
"type": "craft-plugin",
"version": "1.0.3",
"version": "1.0.5",
"keywords": [
"craft",
"cms",
Expand Down
2 changes: 1 addition & 1 deletion src/Brandfetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function createSettingsModel()
*/
protected function settingsHtml(): string
{
// TODO: add erroro if no volumes
// TODO: add error if no volumes
// Render the settings template
$destinationOptions = [];
$volumes = Craft::$app->getVolumes();
Expand Down
46 changes: 23 additions & 23 deletions src/assetbundles/brandfetcherfield/dist/js/Brandfetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
defaults = {
actionUrl: "brandfetch/bfcontroller/fetch-logo",
getLogoBtn: ".js-get-brandfetch",
removeLogoBtn: ".js-remove-brandfetch",
logo: "#fields-brandfetch-logo img",
emptyView: "#fields-brandfetch-empty",
filledView: "#fields-brandfetch-filled",
imageIdField: "#fields-brandfetcher-img-id",
urlInput : '#fields-brandfetch-url'
};

// Plugin constructor
Expand All @@ -37,24 +37,29 @@

Plugin.prototype = {
init: function(id) {
console.log(this.options);
// Initial Callers
$(this.options.removeLogoBtn).on("click", this.removeLogo);
$(this.options.getLogoBtn).on("click", this.getLogo);

// watch for enter
$(this.options.urlInput).on('focus', ()=>{
$(document).keypress(function(e) {
if(e.which == 13) {
e.preventDefault();
self.getLogo();
}
});
})
// remove action
$(this.options.urlInput).on('blur', ()=>{
$(this.options.urlInput).off('focus')
});

},
removeLogo: function() {
$(self.options.imageIdField).val("");

$(self.options.logo).remove();
$(self.options.filledView).hide();

$(self.options.emptyView).show();
},


// API Call to get the logo
getLogo: function() {
self.removeErrors();
var fetchUrl = $("#fields-brandfetch-url").val();
var fetchUrl = $(this.options.urlInput).val();
if (
fetchUrl == null ||
fetchUrl == "" ||
Expand All @@ -74,16 +79,11 @@
if (textStatus === "success") {
if (response.success) {
//reset value
$("#fields-brandfetch-url").val("");

$(self.options.urlInput).val("");

$(self.options.imageIdField).html(response.brandfetch.html)

// var thumbLoader = new Craft.ElementThumbLoader();
// var $existing = $(self.options.imageIdField)
// thumbLoader.load($existing);

var tests = new Craft.BaseElementSelectInput({
new Craft.BaseElementSelectInput({
id: 'brandfetcher-img-id',
fieldId:'brandfetcher-img-upload',
name: 'fields[logo]',
Expand All @@ -92,7 +92,6 @@
limit: 1,
fieldId: self.options.id,
elements:([response.brandfetch.result.id]),
// defaultFieldLayoutId: 'brandfetcher-img-id',
modalSettings: {hideSidebar: true}
});

Expand All @@ -105,17 +104,18 @@
}, this)
);
},
// Basic URL validation
validateUrl: function(url) {
return /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(
url
);
},
setFieldErrors: function() {
$("#fields-brandfetch-url").addClass("error");
$(this.options.urlInput).addClass("error");
$("#fields-brandfetch-url-label").addClass("error");
},
removeErrors() {
$("#fields-brandfetch-url").removeClass("error");
$(this.options.urlInput).removeClass("error");
$("#fields-brandfetch-url-label").removeClass("error");
},

Expand Down
16 changes: 7 additions & 9 deletions src/fields/Brandfetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
use milkshakestudio\brandfetch\Brandfetch;
use milkshakestudio\brandfetch\assetbundles\brandfetcherfield\BrandfetcherFieldAsset;


// use craft\fields\Assets as AssetField;


use Craft;
use craft\base\ElementInterface;
use craft\base\Field;
Expand Down Expand Up @@ -101,7 +97,6 @@ public function normalizeValue($value, ElementInterface $element = null)
{
// get the logo if available
if($value){

$asset = Asset::find()->id($value)->one();
return parent::normalizeValue($asset, $element);
}else{
Expand All @@ -125,11 +120,11 @@ public function normalizeValue($value, ElementInterface $element = null)
*/
public function serializeValue($value, ElementInterface $element = null)
{
if( gettype($value) === 'object' )
{
return parent::serializeValue($value->id, $element);
if(!empty($value)){
// $asset = Asset::find()->id($value)->one();
return parent::normalizeValue($value->id, $element);
}else{
return parent::serializeValue($value, $element);
return parent::normalizeValue($value, $element);
}

// return $value->id;
Expand Down Expand Up @@ -173,6 +168,7 @@ public function getInputHtml($value, ElementInterface $element = null): string
}


// TODO: this should be the same as the varriabls that go to the field template
// Variables to pass down to our field JavaScript to let it namespace properly
$jsonVars = [
'id' => $id,
Expand All @@ -183,6 +179,8 @@ public function getInputHtml($value, ElementInterface $element = null): string
];
$jsonVars = Json::encode($jsonVars);
Craft::$app->getView()->registerJs("$('#{$namespacedId}-field').BrandfetchBrandfetcher(" . $jsonVars . ");");

// Is API SET?
$apiSet = !empty(Brandfetch::$plugin->getSettings()->brandfetch_api_key);

// Render the input template
Expand Down
38 changes: 17 additions & 21 deletions src/templates/_components/fields/Brandfetcher_input.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{# @var craft \craft\web\twig\variables\CraftVariable #}
{#
/**
* Brandfetch plugin for Craft CMS 3.x
Expand All @@ -14,10 +13,6 @@
#}

{% import "_includes/forms" as forms %}
{# {% do view.registerAssetBundle("craft\\web\\assets\\fields\\FieldsAsset") %} #}


{# {% include "_components/fieldtypes/assets/input" %} #}

{% if not apiSet %}
<div class="input">
Expand All @@ -29,21 +24,22 @@
{% else %}

<div id="brandfetch-logo">

{# Asset Field #}
{{forms.elementSelectField({
id: 'brandfetcher-img-id',
fieldId:'brandfetcher-img-upload',
name: name,
elementType: elementType,
selectionLabel: "Add Logo",
limit: 1,
viewMode:'large',
elements:(test is defined and test ? [test]),
modalSettings: {
hideSidebar: true
}
})}}

id: 'brandfetcher-img-id',
fieldId:'brandfetcher-img-upload',
name: name,
elementType: elementType,
selectionLabel: "Add Logo",
limit: 1,
viewMode:'large',
elements:(test is defined and test ? [test]),
modalSettings: {
hideSidebar: true
}
})}}

{# Brandfetch Custom Input #}
<div class="flex" id="brandfetch-empty">
<div class="flex-grow">
{{ forms.textField({
Expand All @@ -55,8 +51,8 @@
})
}}
</div>

<div class="btn add icon js-get-brandfetch ">Get Logo</div>
<input type="submit" form="brandfetch-url" class="btn submit js-get-brandfetch" value="Fetch Logo">
{# <div class="btn add icon js-get-brandfetch">Get Logo</div> #}
</div>
</div>

Expand Down

0 comments on commit 09c295d

Please sign in to comment.