diff --git a/blueprints/fields/openerclipboard.yml b/blueprints/fields/openerclipboard.yml new file mode 100644 index 0000000..791ba6b --- /dev/null +++ b/blueprints/fields/openerclipboard.yml @@ -0,0 +1,10 @@ +# opener example for opening file as popup window +type: opener +label: Example 9 +command: '{page.url}/copy:yes' +help: '{page.url}/copy:yes' +text: 'Copy URL of Frontend Page to Clipboard' +textprogress: 'Copy URL of Frontend Page to Clipboard' +textsuccess: 'Copied' +texterror: 'No Browser Support' +width: 1/2 diff --git a/blueprints/fields/openerconfirmation.yml b/blueprints/fields/openerconfirmation.yml new file mode 100644 index 0000000..3c92b81 --- /dev/null +++ b/blueprints/fields/openerconfirmation.yml @@ -0,0 +1,11 @@ +# opener example for opening file as popup window +type: opener +label: Example 10 +command: '/openerexample-route/success/{page.diruri.encoded}/{page.secret}' +help: '/openerexample-route/success/{page.diruri.encoded}/{page.secret}' +text: 'Like #6 but with Confirmation' +textprogress: 'baking...' +textsuccess: 'YML :)' +texterror: 'YML :(' +width: 1/2 +confirm: 'Hungry?' diff --git a/blueprints/openerexample.yml b/blueprints/openerexample.yml index 2140910..3568e3a 100644 --- a/blueprints/openerexample.yml +++ b/blueprints/openerexample.yml @@ -33,6 +33,8 @@ fields: example6: openersuccess example7: openererror example8: openercontroller + example9: openerclipboard + example10: openerconfirmation placeholders-line: type: line diff --git a/fields/opener/assets/js/script.coffee b/fields/opener/assets/js/script.coffee index 6e8515e..5c02f52 100644 --- a/fields/opener/assets/js/script.coffee +++ b/fields/opener/assets/js/script.coffee @@ -22,6 +22,7 @@ Opener = ($, $field) -> progress: this.$text.attr 'data-textprogress' success : this.$text.attr 'data-textsuccess' error : this.$text.attr 'data-texterror' + confirm : this.$text.attr 'data-textconfirm' this.parseResult = (result) -> if result[this.jsoncode] is 200 @@ -125,6 +126,12 @@ Opener = ($, $field) -> this.$field.find('a.opener').click (ev) -> ev.preventDefault() + confirmed = true + if self.$opener.hasClass 'confirm' + confirmed = confirm(self.l.confirm) + + if not confirmed then return + if self.$opener.hasClass 'no-ajax' window.open self.$button.attr('href') else if self.$opener.hasClass 'copy-clipboard' diff --git a/fields/opener/assets/js/script.js b/fields/opener/assets/js/script.js index 44e7279..d713d7d 100644 --- a/fields/opener/assets/js/script.js +++ b/fields/opener/assets/js/script.js @@ -1 +1 @@ -(function(){var t;t=function($,t){var e;return e=this,this.$field=t,this.$opener=t.find(".opener-button"),this.$button=t.find(".opener-button a"),this.$copy=t.find(".opener-button .copy"),this.$text=t.find(".opener-button a span"),this.$download=t.find(".opener-button a.opener-download"),this.useDownloadLink=this.$opener.hasClass("download"),this.refresh=this.$opener.hasClass("refresh"),this.delay=parseInt(this.$opener.attr("data-delay")),this.jsoncode=String(this.$opener.attr("data-jsoncode")),this.jsonmessage=String(this.$opener.attr("data-jsonmessage")),this.jsonfileurl=String(this.$opener.attr("data-jsonfileurl")),this.l={default:this.$text.attr("data-textdefault"),progress:this.$text.attr("data-textprogress"),success:this.$text.attr("data-textsuccess"),error:this.$text.attr("data-texterror")},this.parseResult=function(t){200===t[this.jsoncode]?e.hasSuccess(t[this.jsonmessage],t[this.jsonfileurl]):e.hasError(t.error,t[this.jsonmessage])},this.hasError=function(t,n){void 0!==n&&0!==n.trim().length||(n=e.l.error),void 0!==n&&0!==n.trim().length||(n=t),e.$button.addClass("btn-negative").children("span").text(n),setTimeout(function(){return e.$button.removeClass("btn-negative"),e.$button.children("span").text(e.l.default),e.$download.attr("href",""),e.$download.attr("download","")},e.delay)},this.hasSuccess=function(t,n){var o;void 0!==t&&0!==t.trim().length||(t=e.l.success),e.$button.addClass("btn-positive"),e.$button.children("span").text(t),void 0!==n&&n.trim().length>0&&(e.useDownloadLink?(o=n.split("/").pop(),e.$download.attr("href",n),e.$download.attr("download",o),e.$download[0].click()):window.open(n)),setTimeout(function(){if(e.$button.removeClass("btn-positive"),e.$button.children("span").text(e.l.default),e.$download.attr("href",""),e.$download.attr("download",""),e.refresh)return window.location.reload(!0)},e.delay)},this.init=function(){},this.copyToClipboard=function(t){var e,n,o,s,a,r,i,d;d="_hiddenCopyText_",o="INPUT"===t.tagName||"TEXTAREA"===t.tagName,a=void 0,s=void 0,o?(i=t,a=t.selectionStart,s=t.selectionEnd):(i=document.getElementById(d),i||(i=document.createElement("textarea"),i.style.position="absolute",i.style.left="-9999px",i.style.top="0",i.id=d,document.body.appendChild(i)),i.textContent=t.textContent),e=document.activeElement,i.focus(),i.setSelectionRange(0,i.value.length),r=void 0;try{r=document.execCommand("copy")}catch(t){n=t,r=!1}return e&&"function"==typeof e.focus&&e.focus(),o?t.setSelectionRange(a,s):i.textContent="",r},this.$field.find("a.opener").click(function(t){var n;t.preventDefault(),e.$opener.hasClass("no-ajax")?window.open(e.$button.attr("href")):e.$opener.hasClass("copy-clipboard")?(e.$copy.removeClass("jquery-hide"),e.copyToClipboard(e.$copy.get(0))?e.hasSuccess():(e.hasError(),window.open(e.$button.attr("href"))),e.$copy.addClass("jquery-hide")):(n=$(this).attr("name"),$.fn.OpenerAjax(e,$(this).attr("href")+"/panel:1"))}),this.init()},function($){$.fn.OpenerAjax=function(t,e){void 0!==t.$field&&(t.$field.hasClass("ajax")||(t.$field.addClass("ajax"),document.body.style.cursor="wait",t.$button.children("span").text(t.l.progress),$.ajax({url:e,type:"GET",success:function(e){return t.parseResult(e)},error:function(e,n,o){return t.hasError(n+o)},complete:function(){return t.$field.removeClass("ajax"),document.body.style.cursor="default"}})))},$.fn.opener=function(){return new t($,this)}}(jQuery)}).call(this); \ No newline at end of file +(function(){var t;t=function($,t){var e;return e=this,this.$field=t,this.$opener=t.find(".opener-button"),this.$button=t.find(".opener-button a"),this.$copy=t.find(".opener-button .copy"),this.$text=t.find(".opener-button a span"),this.$download=t.find(".opener-button a.opener-download"),this.useDownloadLink=this.$opener.hasClass("download"),this.refresh=this.$opener.hasClass("refresh"),this.delay=parseInt(this.$opener.attr("data-delay")),this.jsoncode=String(this.$opener.attr("data-jsoncode")),this.jsonmessage=String(this.$opener.attr("data-jsonmessage")),this.jsonfileurl=String(this.$opener.attr("data-jsonfileurl")),this.l={default:this.$text.attr("data-textdefault"),progress:this.$text.attr("data-textprogress"),success:this.$text.attr("data-textsuccess"),error:this.$text.attr("data-texterror"),confirm:this.$text.attr("data-textconfirm")},this.parseResult=function(t){200===t[this.jsoncode]?e.hasSuccess(t[this.jsonmessage],t[this.jsonfileurl]):e.hasError(t.error,t[this.jsonmessage])},this.hasError=function(t,n){void 0!==n&&0!==n.trim().length||(n=e.l.error),void 0!==n&&0!==n.trim().length||(n=t),e.$button.addClass("btn-negative").children("span").text(n),setTimeout(function(){return e.$button.removeClass("btn-negative"),e.$button.children("span").text(e.l.default),e.$download.attr("href",""),e.$download.attr("download","")},e.delay)},this.hasSuccess=function(t,n){var o;void 0!==t&&0!==t.trim().length||(t=e.l.success),e.$button.addClass("btn-positive"),e.$button.children("span").text(t),void 0!==n&&n.trim().length>0&&(e.useDownloadLink?(o=n.split("/").pop(),e.$download.attr("href",n),e.$download.attr("download",o),e.$download[0].click()):window.open(n)),setTimeout(function(){if(e.$button.removeClass("btn-positive"),e.$button.children("span").text(e.l.default),e.$download.attr("href",""),e.$download.attr("download",""),e.refresh)return window.location.reload(!0)},e.delay)},this.init=function(){},this.copyToClipboard=function(t){var e,n,o,s,a,r,i,d;d="_hiddenCopyText_",o="INPUT"===t.tagName||"TEXTAREA"===t.tagName,a=void 0,s=void 0,o?(i=t,a=t.selectionStart,s=t.selectionEnd):(i=document.getElementById(d),i||(i=document.createElement("textarea"),i.style.position="absolute",i.style.left="-9999px",i.style.top="0",i.id=d,document.body.appendChild(i)),i.textContent=t.textContent),e=document.activeElement,i.focus(),i.setSelectionRange(0,i.value.length),r=void 0;try{r=document.execCommand("copy")}catch(t){n=t,r=!1}return e&&"function"==typeof e.focus&&e.focus(),o?t.setSelectionRange(a,s):i.textContent="",r},this.$field.find("a.opener").click(function(t){var n,o;t.preventDefault(),n=!0,e.$opener.hasClass("confirm")&&(n=confirm(e.l.confirm)),n&&(e.$opener.hasClass("no-ajax")?window.open(e.$button.attr("href")):e.$opener.hasClass("copy-clipboard")?(e.$copy.removeClass("jquery-hide"),e.copyToClipboard(e.$copy.get(0))?e.hasSuccess():(e.hasError(),window.open(e.$button.attr("href"))),e.$copy.addClass("jquery-hide")):(o=$(this).attr("name"),$.fn.OpenerAjax(e,$(this).attr("href")+"/panel:1")))}),this.init()},function($){$.fn.OpenerAjax=function(t,e){void 0!==t.$field&&(t.$field.hasClass("ajax")||(t.$field.addClass("ajax"),document.body.style.cursor="wait",t.$button.children("span").text(t.l.progress),$.ajax({url:e,type:"GET",success:function(e){return t.parseResult(e)},error:function(e,n,o){return t.hasError(n+o)},complete:function(){return t.$field.removeClass("ajax"),document.body.style.cursor="default"}})))},$.fn.opener=function(){return new t($,this)}}(jQuery)}).call(this); \ No newline at end of file diff --git a/fields/opener/opener.php b/fields/opener/opener.php index db2d1ce..4cf01bb 100644 --- a/fields/opener/opener.php +++ b/fields/opener/opener.php @@ -17,6 +17,7 @@ class OpenerField extends BaseField { public $textsuccess; public $texterror; public $command; + public $confirm; public function input() { diff --git a/fields/opener/template.php b/fields/opener/template.php index 375c3df..ce63052 100644 --- a/fields/opener/template.php +++ b/fields/opener/template.php @@ -122,10 +122,12 @@ $placeholder = a::merge($placeholder, $fieldValues); } - if(c::get('debug', false)) { + if(c::get('plugin.opener.debug', false)) { echo '
'.a::show($placeholder, false).'
'; } + $confirm = isset($field->confirm) ? boolval($field->confirm) : false; + $href = url(str::template($field->command, $placeholder)); $download = str::contains($href, c::get('plugin.opener.trigger-download', 'download:yes')); @@ -150,7 +152,7 @@
text; ?> Download diff --git a/package.json b/package.json index 0cbf9e4..23c8eec 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "kirby-opener", "description": "Kirby Opener is a Kirby CMS Panel Field button that allows you to use placeholders to create dynamic urls which are called with and without ajax response or start downloads.", - "version": "0.8.2", + "version": "0.9.0", "author": "Bruno Meilick", "type": "kirby-plugin", "license": "Commercial" diff --git a/readme.md b/readme.md index 8ae321f..14f0ecd 100644 --- a/readme.md +++ b/readme.md @@ -15,6 +15,7 @@ Kirby Opener is a Kirby CMS Panel Field button that allows you to use placeholde - trigger downloading of files - trigger clipboard copy of url - trigger refresh of page on success +- trigger browser confirmation dialog - easily extendable *placeholders* - configural parsing of *json response*