From 47f5c971bdabe595c63b09a883632407e1408609 Mon Sep 17 00:00:00 2001 From: Fedik Date: Thu, 18 Aug 2016 20:33:18 +0300 Subject: [PATCH 01/13] Joomla.getOptions and a script options without inline JavaScript --- media/system/js/core-uncompressed.js | 30 +++++++++++++++++++++++++++- media/system/js/core.js | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/media/system/js/core-uncompressed.js b/media/system/js/core-uncompressed.js index 744d5e3d479de..5829efc0cee7f 100644 --- a/media/system/js/core-uncompressed.js +++ b/media/system/js/core-uncompressed.js @@ -71,6 +71,34 @@ Joomla.editors.instances = Joomla.editors.instances || {}; } }; + /** + * Joomla options storage + * @type {{}} + */ + Joomla.optionsStorage = Joomla.optionsStorage || null; + + /** + * Get script(s) options + * + * @param {String} key Name in Storage + * @param mixed def Default value if nothing found + * + * @return mixed + */ + Joomla.getOptions = function( key, def) { + + // Load options if they not exists + if (!Joomla.optionsStorage) { + var element = document.getElementById('joomla-script-options'), + str = element ? element.innerText : null, + options = str ? JSON.parse(str) : null; + + Joomla.optionsStorage = options || {}; + } + + return Joomla.optionsStorage[key] || def; + }; + /** * Method to replace all request tokens on the page with a new one. * Used in Joomla Installation @@ -614,7 +642,7 @@ Joomla.editors.instances = Joomla.editors.instances || {}; parentElement.appendChild(loadingDiv); } // Show or hide the layer. - else + else { if (!document.getElementById('loading-logo')) { diff --git a/media/system/js/core.js b/media/system/js/core.js index 9de37a7588903..5d720d94e73ed 100644 --- a/media/system/js/core.js +++ b/media/system/js/core.js @@ -1 +1 @@ -Joomla=window.Joomla||{},Joomla.editors=Joomla.editors||{},Joomla.editors.instances=Joomla.editors.instances||{},function(e,t){"use strict";e.submitform=function(e,n,o){n||(n=t.getElementById("adminForm")),e&&(n.task.value=e),n.noValidate=!o,n.setAttribute("novalidate",!o);var r=t.createElement("input");r.style.display="none",r.type="submit",n.appendChild(r).click(),n.removeChild(r)},e.submitbutton=function(t){e.submitform(t)},e.JText={strings:{},_:function(e,t){return"undefined"!=typeof this.strings[e.toUpperCase()]?this.strings[e.toUpperCase()]:t},load:function(e){for(var t in e)e.hasOwnProperty(t)&&(this.strings[t.toUpperCase()]=e[t]);return this}},e.replaceTokens=function(e){if(/^[0-9A-F]{32}$/i.test(e)){var n,o,r,i=t.getElementsByTagName("input");for(n=0,r=i.length;r>n;n++)o=i[n],"hidden"==o.type&&"1"==o.value&&32==o.name.length&&(o.name=e)}},e.isEmail=function(e){var t=/^[\w.!#$%&‚Äô*+\/=?^`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9-]{2,})+$/i;return t.test(e)},e.checkAll=function(e,t){if(!e.form)return!1;t=t?t:"cb";var n,o,r,i=0;for(n=0,r=e.form.elements.length;r>n;n++)o=e.form.elements[n],o.type==e.type&&0===o.id.indexOf(t)&&(o.checked=e.checked,i+=o.checked?1:0);return e.form.boxchecked&&(e.form.boxchecked.value=i),!0},e.renderMessages=function(n){e.removeMessages();var o,r,i,a,l,s,d,c,u=t.getElementById("system-message-container");for(o in n)if(n.hasOwnProperty(o)){r=n[o],i=t.createElement("div"),c="notice"==o?"alert-info":"alert-"+o,c="message"==o?"alert-success":c,i.className="alert "+c;var f=t.createElement("button");for(f.setAttribute("type","button"),f.setAttribute("data-dismiss","alert"),f.className="close",f.innerHTML="×",i.appendChild(f),a=e.JText._(o),"undefined"!=typeof a&&(l=t.createElement("h4"),l.className="alert-heading",l.innerHTML=e.JText._(o),i.appendChild(l)),s=r.length-1;s>=0;s--)d=t.createElement("div"),d.innerHTML=r[s],i.appendChild(d);u.appendChild(i)}},e.removeMessages=function(){for(var e=t.getElementById("system-message-container");e.firstChild;)e.removeChild(e.firstChild);e.style.display="none",e.offsetHeight,e.style.display=""},e.ajaxErrorsMessages=function(t,n){var o={};if("parsererror"==n){for(var r=t.responseText.trim(),i=[],a=r.length-1;a>=0;a--)i.unshift(["&#",r[a].charCodeAt(),";"].join(""));r=i.join(""),o.error=[e.JText._("JLIB_JS_AJAX_ERROR_PARSE").replace("%s",r)]}else"nocontent"==n?o.error=[e.JText._("JLIB_JS_AJAX_ERROR_NO_CONTENT")]:"timeout"==n?o.error=[e.JText._("JLIB_JS_AJAX_ERROR_TIMEOUT")]:"abort"==n?o.error=[e.JText._("JLIB_JS_AJAX_ERROR_CONNECTION_ABORT")]:o.error=[e.JText._("JLIB_JS_AJAX_ERROR_OTHER").replace("%s",t.status)];return o},e.isChecked=function(e,n){if("undefined"==typeof n&&(n=t.getElementById("adminForm")),n.boxchecked.value=e?parseInt(n.boxchecked.value)+1:parseInt(n.boxchecked.value)-1,n.elements["checkall-toggle"]){var o,r,i,a=!0;for(o=0,i=n.elements.length;i>o;o++)if(r=n.elements[o],"checkbox"==r.type&&"checkall-toggle"!=r.name&&!r.checked){a=!1;break}n.elements["checkall-toggle"].checked=a}},e.popupWindow=function(e,t,n,o,r){var i=(screen.width-n)/2,a=(screen.height-o)/2,l="height="+o+",width="+n+",top="+a+",left="+i+",scrollbars="+r+",resizable";window.open(e,t,l).window.focus()},e.tableOrdering=function(n,o,r,i){"undefined"==typeof i&&(i=t.getElementById("adminForm")),i.filter_order.value=n,i.filter_order_Dir.value=o,e.submitform(r,i)},window.writeDynaList=function(e,n,o,r,i){var a,l,s,d="",t.writeln(d)},window.changeDynaList=function(e,n,o,r,i){for(var a,l,s,d,c=t.adminForm[e],u=o==r;c.firstChild;)c.removeChild(c.firstChild);a=0;for(l in n)n.hasOwnProperty(l)&&(s=n[l],s[0]==o&&(d=new Option,d.value=s[1],d.text=s[2],(u&&i==d.value||!u&&0===a)&&(d.selected=!0),c.options[a++]=d));c.length=a},window.radioGetCheckedValue=function(e){if(!e)return"";var t,n=e.length;if(void 0===n)return e.checked?e.value:"";for(t=0;n>t;t++)if(e[t].checked)return e[t].value;return""},window.getSelectedValue=function(e,n){var o=t[e][n],r=o.selectedIndex;return null!==r&&r>-1?o.options[r].value:null},window.listItemTask=function(e,n){var o,r=t.adminForm,i=0,a=r[e];if(!a)return!1;for(;;){if(o=r["cb"+i],!o)break;o.checked=!1,i++}return a.checked=!0,r.boxchecked.value=1,window.submitform(n),!1},window.submitbutton=function(t){e.submitbutton(t)},window.submitform=function(t){e.submitform(t)},window.saveorder=function(e,t){window.checkAll_button(e,t)},window.checkAll_button=function(n,o){o=o?o:"saveorder";var r,i;for(r=0;n>=r;r++){if(i=t.adminForm["cb"+r],!i)return void alert("You cannot change the order of items, as an item in the list is `Checked Out`");i.checked=!0}e.submitform(o)},e.loadingLayer=function(n,o){if(n=n||"show",o=o||t.body,"load"==n){var r=t.getElementsByTagName("body")[0].getAttribute("data-basepath")||"",i=t.createElement("div");i.id="loading-logo",i.style.position="fixed",i.style.top="0",i.style.left="0",i.style.width="100%",i.style.height="100%",i.style.opacity="0.8",i.style.filter="alpha(opacity=80)",i.style.overflow="hidden",i.style["z-index"]="10000",i.style.display="none",i.style["background-color"]="#fff",i.style["background-image"]='url("'+r+'/media/jui/images/ajax-loader.gif")',i.style["background-position"]="center",i.style["background-repeat"]="no-repeat",i.style["background-attachment"]="fixed",o.appendChild(i)}else t.getElementById("loading-logo")||e.loadingLayer("load",o),t.getElementById("loading-logo").style.display="show"==n?"block":"none";return t.getElementById("loading-logo")}}(Joomla,document); +Joomla=window.Joomla||{};Joomla.editors=Joomla.editors||{};Joomla.editors.instances=Joomla.editors.instances||{};(function(Joomla,document){"use strict";Joomla.submitform=function(task,form,validate){if(!form){form=document.getElementById("adminForm")}if(task){form.task.value=task}form.noValidate=!validate;form.setAttribute("novalidate",!validate);var button=document.createElement("input");button.style.display="none";button.type="submit";form.appendChild(button).click();form.removeChild(button)};Joomla.submitbutton=function(pressbutton){Joomla.submitform(pressbutton)};Joomla.JText={strings:{},_:function(key,def){return typeof this.strings[key.toUpperCase()]!=="undefined"?this.strings[key.toUpperCase()]:def},load:function(object){for(var key in object){if(!object.hasOwnProperty(key))continue;this.strings[key.toUpperCase()]=object[key]}return this}};Joomla.optionsStorage=Joomla.optionsStorage||null;Joomla.getOptions=function(key,def){if(!Joomla.optionsStorage){var element=document.getElementById("joomla-script-options"),str=element?element.innerText:null,options=str?JSON.parse(str):null;Joomla.optionsStorage=options||{}}return Joomla.optionsStorage[key]||def};Joomla.replaceTokens=function(newToken){if(!/^[0-9A-F]{32}$/i.test(newToken)){return}var els=document.getElementsByTagName("input"),i,el,n;for(i=0,n=els.length;i=0;i--){messageWrapper=document.createElement("div");messageWrapper.innerHTML=typeMessages[i];messagesBox.appendChild(messageWrapper)}messageContainer.appendChild(messagesBox)}};Joomla.removeMessages=function(){var messageContainer=document.getElementById("system-message-container");while(messageContainer.firstChild)messageContainer.removeChild(messageContainer.firstChild);messageContainer.style.display="none";messageContainer.offsetHeight;messageContainer.style.display=""};Joomla.ajaxErrorsMessages=function(jqXHR,textStatus,error){var msg={};if(textStatus=="parsererror"){var encodedJson=jqXHR.responseText.trim();var buf=[];for(var i=encodedJson.length-1;i>=0;i--){buf.unshift(["&#",encodedJson[i].charCodeAt(),";"].join(""))}encodedJson=buf.join("");msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_PARSE").replace("%s",encodedJson)]}else if(textStatus=="nocontent"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_NO_CONTENT")]}else if(textStatus=="timeout"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_TIMEOUT")]}else if(textStatus=="abort"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_CONNECTION_ABORT")]}else{msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_OTHER").replace("%s",jqXHR.status)]}return msg};Joomla.isChecked=function(isitchecked,form){if(typeof form==="undefined"){form=document.getElementById("adminForm")}form.boxchecked.value=isitchecked?parseInt(form.boxchecked.value)+1:parseInt(form.boxchecked.value)-1;if(!form.elements["checkall-toggle"])return;var c=true,i,e,n;for(i=0,n=form.elements.length;i",hasSelection=key==orig_key,i=0,selected,x,item;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}selected="";if(hasSelection&&orig_val==item[1]||!hasSelection&&i===0){selected='selected="selected"'}html+='";i++}html+="";document.writeln(html)};window.changeDynaList=function(listname,source,key,orig_key,orig_val){var list=document.adminForm[listname],hasSelection=key==orig_key,i,x,item,opt;while(list.firstChild)list.removeChild(list.firstChild);i=0;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}opt=new Option;opt.value=item[1];opt.text=item[2];if(hasSelection&&orig_val==opt.value||!hasSelection&&i===0){opt.selected=true}list.options[i++]=opt}list.length=i};window.radioGetCheckedValue=function(radioObj){if(!radioObj){return""}var n=radioObj.length,i;if(n===undefined){return radioObj.checked?radioObj.value:""}for(i=0;i-1){return srcList.options[i].value}else{return null}};window.listItemTask=function(id,task){var f=document.adminForm,i=0,cbx,cb=f[id];if(!cb)return false;while(true){cbx=f["cb"+i];if(!cbx)break;cbx.checked=false;i++}cb.checked=true;f.boxchecked.value=1;window.submitform(task);return false};window.submitbutton=function(pressbutton){Joomla.submitbutton(pressbutton)};window.submitform=function(pressbutton){Joomla.submitform(pressbutton)};window.saveorder=function(n,task){window.checkAll_button(n,task)};window.checkAll_button=function(n,task){task=task?task:"saveorder";var j,box;for(j=0;j<=n;j++){box=document.adminForm["cb"+j];if(box){box.checked=true}else{alert("You cannot change the order of items, as an item in the list is `Checked Out`");return}}Joomla.submitform(task)};Joomla.loadingLayer=function(task,parentElement){task=task||"show";parentElement=parentElement||document.body;if(task=="load"){var basePath=document.getElementsByTagName("body")[0].getAttribute("data-basepath")||"";var loadingDiv=document.createElement("div");loadingDiv.id="loading-logo";loadingDiv.style["position"]="fixed";loadingDiv.style["top"]="0";loadingDiv.style["left"]="0";loadingDiv.style["width"]="100%";loadingDiv.style["height"]="100%";loadingDiv.style["opacity"]="0.8";loadingDiv.style["filter"]="alpha(opacity=80)";loadingDiv.style["overflow"]="hidden";loadingDiv.style["z-index"]="10000";loadingDiv.style["display"]="none";loadingDiv.style["background-color"]="#fff";loadingDiv.style["background-image"]='url("'+basePath+'/media/jui/images/ajax-loader.gif")';loadingDiv.style["background-position"]="center";loadingDiv.style["background-repeat"]="no-repeat";loadingDiv.style["background-attachment"]="fixed";parentElement.appendChild(loadingDiv)}else{if(!document.getElementById("loading-logo")){Joomla.loadingLayer("load",parentElement)}document.getElementById("loading-logo").style["display"]=task=="show"?"block":"none"}return document.getElementById("loading-logo")}})(Joomla,document); \ No newline at end of file From 6f6c6f7ac509ca43060b8f79d06c07195c34c356 Mon Sep 17 00:00:00 2001 From: Fedik Date: Thu, 18 Aug 2016 20:38:06 +0300 Subject: [PATCH 02/13] Options rendering --- .../joomla/document/renderer/html/head.php | 30 +++++++------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/libraries/joomla/document/renderer/html/head.php b/libraries/joomla/document/renderer/html/head.php index 17f6ec4ae3032..1f9eb09199375 100644 --- a/libraries/joomla/document/renderer/html/head.php +++ b/libraries/joomla/document/renderer/html/head.php @@ -53,6 +53,13 @@ public function fetchHead($document) $document->_metaTags['name']['tags'] = implode(', ', $tagsHelper->getTagNames($document->_metaTags['name']['tags'])); } + $scriptOptions = $document->getScriptOptions(); + + if (!empty($scriptOptions)) + { + JHtml::_('behavior.core'); + } + // Trigger the onBeforeCompileHead event $app = JFactory::getApplication(); $app->triggerEvent('onBeforeCompileHead'); @@ -220,33 +227,16 @@ public function fetchHead($document) } // Generate scripts options - $scriptOptions = $document->getScriptOptions(); - if (!empty($scriptOptions)) { - $buffer .= $tab . '' . $lnEnd; + $buffer .= $jsonOptions; + $buffer .= '' . $lnEnd; } // Generate script declarations From 52df0085ba9f9a9bb4918404d405fed1c1da9168 Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 19 Aug 2016 18:19:13 +0300 Subject: [PATCH 03/13] IE8 compatibility --- media/system/js/core-uncompressed.js | 6 +++--- media/system/js/core.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/media/system/js/core-uncompressed.js b/media/system/js/core-uncompressed.js index 5829efc0cee7f..5c364d7aa9394 100644 --- a/media/system/js/core-uncompressed.js +++ b/media/system/js/core-uncompressed.js @@ -74,7 +74,7 @@ Joomla.editors.instances = Joomla.editors.instances || {}; /** * Joomla options storage * @type {{}} - */ + */ Joomla.optionsStorage = Joomla.optionsStorage || null; /** @@ -84,13 +84,13 @@ Joomla.editors.instances = Joomla.editors.instances || {}; * @param mixed def Default value if nothing found * * @return mixed - */ + */ Joomla.getOptions = function( key, def) { // Load options if they not exists if (!Joomla.optionsStorage) { var element = document.getElementById('joomla-script-options'), - str = element ? element.innerText : null, + str = element ? (element.text || element.textContent) : null, options = str ? JSON.parse(str) : null; Joomla.optionsStorage = options || {}; diff --git a/media/system/js/core.js b/media/system/js/core.js index 5d720d94e73ed..7c906463ca779 100644 --- a/media/system/js/core.js +++ b/media/system/js/core.js @@ -1 +1 @@ -Joomla=window.Joomla||{};Joomla.editors=Joomla.editors||{};Joomla.editors.instances=Joomla.editors.instances||{};(function(Joomla,document){"use strict";Joomla.submitform=function(task,form,validate){if(!form){form=document.getElementById("adminForm")}if(task){form.task.value=task}form.noValidate=!validate;form.setAttribute("novalidate",!validate);var button=document.createElement("input");button.style.display="none";button.type="submit";form.appendChild(button).click();form.removeChild(button)};Joomla.submitbutton=function(pressbutton){Joomla.submitform(pressbutton)};Joomla.JText={strings:{},_:function(key,def){return typeof this.strings[key.toUpperCase()]!=="undefined"?this.strings[key.toUpperCase()]:def},load:function(object){for(var key in object){if(!object.hasOwnProperty(key))continue;this.strings[key.toUpperCase()]=object[key]}return this}};Joomla.optionsStorage=Joomla.optionsStorage||null;Joomla.getOptions=function(key,def){if(!Joomla.optionsStorage){var element=document.getElementById("joomla-script-options"),str=element?element.innerText:null,options=str?JSON.parse(str):null;Joomla.optionsStorage=options||{}}return Joomla.optionsStorage[key]||def};Joomla.replaceTokens=function(newToken){if(!/^[0-9A-F]{32}$/i.test(newToken)){return}var els=document.getElementsByTagName("input"),i,el,n;for(i=0,n=els.length;i=0;i--){messageWrapper=document.createElement("div");messageWrapper.innerHTML=typeMessages[i];messagesBox.appendChild(messageWrapper)}messageContainer.appendChild(messagesBox)}};Joomla.removeMessages=function(){var messageContainer=document.getElementById("system-message-container");while(messageContainer.firstChild)messageContainer.removeChild(messageContainer.firstChild);messageContainer.style.display="none";messageContainer.offsetHeight;messageContainer.style.display=""};Joomla.ajaxErrorsMessages=function(jqXHR,textStatus,error){var msg={};if(textStatus=="parsererror"){var encodedJson=jqXHR.responseText.trim();var buf=[];for(var i=encodedJson.length-1;i>=0;i--){buf.unshift(["&#",encodedJson[i].charCodeAt(),";"].join(""))}encodedJson=buf.join("");msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_PARSE").replace("%s",encodedJson)]}else if(textStatus=="nocontent"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_NO_CONTENT")]}else if(textStatus=="timeout"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_TIMEOUT")]}else if(textStatus=="abort"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_CONNECTION_ABORT")]}else{msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_OTHER").replace("%s",jqXHR.status)]}return msg};Joomla.isChecked=function(isitchecked,form){if(typeof form==="undefined"){form=document.getElementById("adminForm")}form.boxchecked.value=isitchecked?parseInt(form.boxchecked.value)+1:parseInt(form.boxchecked.value)-1;if(!form.elements["checkall-toggle"])return;var c=true,i,e,n;for(i=0,n=form.elements.length;i",hasSelection=key==orig_key,i=0,selected,x,item;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}selected="";if(hasSelection&&orig_val==item[1]||!hasSelection&&i===0){selected='selected="selected"'}html+='";i++}html+="";document.writeln(html)};window.changeDynaList=function(listname,source,key,orig_key,orig_val){var list=document.adminForm[listname],hasSelection=key==orig_key,i,x,item,opt;while(list.firstChild)list.removeChild(list.firstChild);i=0;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}opt=new Option;opt.value=item[1];opt.text=item[2];if(hasSelection&&orig_val==opt.value||!hasSelection&&i===0){opt.selected=true}list.options[i++]=opt}list.length=i};window.radioGetCheckedValue=function(radioObj){if(!radioObj){return""}var n=radioObj.length,i;if(n===undefined){return radioObj.checked?radioObj.value:""}for(i=0;i-1){return srcList.options[i].value}else{return null}};window.listItemTask=function(id,task){var f=document.adminForm,i=0,cbx,cb=f[id];if(!cb)return false;while(true){cbx=f["cb"+i];if(!cbx)break;cbx.checked=false;i++}cb.checked=true;f.boxchecked.value=1;window.submitform(task);return false};window.submitbutton=function(pressbutton){Joomla.submitbutton(pressbutton)};window.submitform=function(pressbutton){Joomla.submitform(pressbutton)};window.saveorder=function(n,task){window.checkAll_button(n,task)};window.checkAll_button=function(n,task){task=task?task:"saveorder";var j,box;for(j=0;j<=n;j++){box=document.adminForm["cb"+j];if(box){box.checked=true}else{alert("You cannot change the order of items, as an item in the list is `Checked Out`");return}}Joomla.submitform(task)};Joomla.loadingLayer=function(task,parentElement){task=task||"show";parentElement=parentElement||document.body;if(task=="load"){var basePath=document.getElementsByTagName("body")[0].getAttribute("data-basepath")||"";var loadingDiv=document.createElement("div");loadingDiv.id="loading-logo";loadingDiv.style["position"]="fixed";loadingDiv.style["top"]="0";loadingDiv.style["left"]="0";loadingDiv.style["width"]="100%";loadingDiv.style["height"]="100%";loadingDiv.style["opacity"]="0.8";loadingDiv.style["filter"]="alpha(opacity=80)";loadingDiv.style["overflow"]="hidden";loadingDiv.style["z-index"]="10000";loadingDiv.style["display"]="none";loadingDiv.style["background-color"]="#fff";loadingDiv.style["background-image"]='url("'+basePath+'/media/jui/images/ajax-loader.gif")';loadingDiv.style["background-position"]="center";loadingDiv.style["background-repeat"]="no-repeat";loadingDiv.style["background-attachment"]="fixed";parentElement.appendChild(loadingDiv)}else{if(!document.getElementById("loading-logo")){Joomla.loadingLayer("load",parentElement)}document.getElementById("loading-logo").style["display"]=task=="show"?"block":"none"}return document.getElementById("loading-logo")}})(Joomla,document); \ No newline at end of file +Joomla=window.Joomla||{};Joomla.editors=Joomla.editors||{};Joomla.editors.instances=Joomla.editors.instances||{};(function(Joomla,document){"use strict";Joomla.submitform=function(task,form,validate){if(!form){form=document.getElementById("adminForm")}if(task){form.task.value=task}form.noValidate=!validate;form.setAttribute("novalidate",!validate);var button=document.createElement("input");button.style.display="none";button.type="submit";form.appendChild(button).click();form.removeChild(button)};Joomla.submitbutton=function(pressbutton){Joomla.submitform(pressbutton)};Joomla.JText={strings:{},_:function(key,def){return typeof this.strings[key.toUpperCase()]!=="undefined"?this.strings[key.toUpperCase()]:def},load:function(object){for(var key in object){if(!object.hasOwnProperty(key))continue;this.strings[key.toUpperCase()]=object[key]}return this}};Joomla.optionsStorage=Joomla.optionsStorage||null;Joomla.getOptions=function(key,def){if(!Joomla.optionsStorage){var element=document.getElementById("joomla-script-options"),str=element?element.text||element.textContent:null,options=str?JSON.parse(str):null;Joomla.optionsStorage=options||{}}return Joomla.optionsStorage[key]||def};Joomla.replaceTokens=function(newToken){if(!/^[0-9A-F]{32}$/i.test(newToken)){return}var els=document.getElementsByTagName("input"),i,el,n;for(i=0,n=els.length;i=0;i--){messageWrapper=document.createElement("div");messageWrapper.innerHTML=typeMessages[i];messagesBox.appendChild(messageWrapper)}messageContainer.appendChild(messagesBox)}};Joomla.removeMessages=function(){var messageContainer=document.getElementById("system-message-container");while(messageContainer.firstChild)messageContainer.removeChild(messageContainer.firstChild);messageContainer.style.display="none";messageContainer.offsetHeight;messageContainer.style.display=""};Joomla.ajaxErrorsMessages=function(jqXHR,textStatus,error){var msg={};if(textStatus=="parsererror"){var encodedJson=jqXHR.responseText.trim();var buf=[];for(var i=encodedJson.length-1;i>=0;i--){buf.unshift(["&#",encodedJson[i].charCodeAt(),";"].join(""))}encodedJson=buf.join("");msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_PARSE").replace("%s",encodedJson)]}else if(textStatus=="nocontent"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_NO_CONTENT")]}else if(textStatus=="timeout"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_TIMEOUT")]}else if(textStatus=="abort"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_CONNECTION_ABORT")]}else{msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_OTHER").replace("%s",jqXHR.status)]}return msg};Joomla.isChecked=function(isitchecked,form){if(typeof form==="undefined"){form=document.getElementById("adminForm")}form.boxchecked.value=isitchecked?parseInt(form.boxchecked.value)+1:parseInt(form.boxchecked.value)-1;if(!form.elements["checkall-toggle"])return;var c=true,i,e,n;for(i=0,n=form.elements.length;i",hasSelection=key==orig_key,i=0,selected,x,item;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}selected="";if(hasSelection&&orig_val==item[1]||!hasSelection&&i===0){selected='selected="selected"'}html+='";i++}html+="";document.writeln(html)};window.changeDynaList=function(listname,source,key,orig_key,orig_val){var list=document.adminForm[listname],hasSelection=key==orig_key,i,x,item,opt;while(list.firstChild)list.removeChild(list.firstChild);i=0;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}opt=new Option;opt.value=item[1];opt.text=item[2];if(hasSelection&&orig_val==opt.value||!hasSelection&&i===0){opt.selected=true}list.options[i++]=opt}list.length=i};window.radioGetCheckedValue=function(radioObj){if(!radioObj){return""}var n=radioObj.length,i;if(n===undefined){return radioObj.checked?radioObj.value:""}for(i=0;i-1){return srcList.options[i].value}else{return null}};window.listItemTask=function(id,task){var f=document.adminForm,i=0,cbx,cb=f[id];if(!cb)return false;while(true){cbx=f["cb"+i];if(!cbx)break;cbx.checked=false;i++}cb.checked=true;f.boxchecked.value=1;window.submitform(task);return false};window.submitbutton=function(pressbutton){Joomla.submitbutton(pressbutton)};window.submitform=function(pressbutton){Joomla.submitform(pressbutton)};window.saveorder=function(n,task){window.checkAll_button(n,task)};window.checkAll_button=function(n,task){task=task?task:"saveorder";var j,box;for(j=0;j<=n;j++){box=document.adminForm["cb"+j];if(box){box.checked=true}else{alert("You cannot change the order of items, as an item in the list is `Checked Out`");return}}Joomla.submitform(task)};Joomla.loadingLayer=function(task,parentElement){task=task||"show";parentElement=parentElement||document.body;if(task=="load"){var basePath=document.getElementsByTagName("body")[0].getAttribute("data-basepath")||"";var loadingDiv=document.createElement("div");loadingDiv.id="loading-logo";loadingDiv.style["position"]="fixed";loadingDiv.style["top"]="0";loadingDiv.style["left"]="0";loadingDiv.style["width"]="100%";loadingDiv.style["height"]="100%";loadingDiv.style["opacity"]="0.8";loadingDiv.style["filter"]="alpha(opacity=80)";loadingDiv.style["overflow"]="hidden";loadingDiv.style["z-index"]="10000";loadingDiv.style["display"]="none";loadingDiv.style["background-color"]="#fff";loadingDiv.style["background-image"]='url("'+basePath+'/media/jui/images/ajax-loader.gif")';loadingDiv.style["background-position"]="center";loadingDiv.style["background-repeat"]="no-repeat";loadingDiv.style["background-attachment"]="fixed";parentElement.appendChild(loadingDiv)}else{if(!document.getElementById("loading-logo")){Joomla.loadingLayer("load",parentElement)}document.getElementById("loading-logo").style["display"]=task=="show"?"block":"none"}return document.getElementById("loading-logo")}})(Joomla,document); \ No newline at end of file From 14151a3414bc59e3dcfcd3fde1db1bba9e63bfbf Mon Sep 17 00:00:00 2001 From: Fedik Date: Fri, 19 Aug 2016 18:24:05 +0300 Subject: [PATCH 04/13] Correct place to call 'behavior.core' --- libraries/joomla/document/renderer/html/head.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/joomla/document/renderer/html/head.php b/libraries/joomla/document/renderer/html/head.php index 1f9eb09199375..3595012f0bf5e 100644 --- a/libraries/joomla/document/renderer/html/head.php +++ b/libraries/joomla/document/renderer/html/head.php @@ -53,17 +53,15 @@ public function fetchHead($document) $document->_metaTags['name']['tags'] = implode(', ', $tagsHelper->getTagNames($document->_metaTags['name']['tags'])); } - $scriptOptions = $document->getScriptOptions(); + // Trigger the onBeforeCompileHead event + $app = JFactory::getApplication(); + $app->triggerEvent('onBeforeCompileHead'); - if (!empty($scriptOptions)) + if ($document->getScriptOptions()) { JHtml::_('behavior.core'); } - // Trigger the onBeforeCompileHead event - $app = JFactory::getApplication(); - $app->triggerEvent('onBeforeCompileHead'); - // Get line endings $lnEnd = $document->_getLineEnd(); $tab = $document->_getTab(); @@ -227,6 +225,8 @@ public function fetchHead($document) } // Generate scripts options + $scriptOptions = $document->getScriptOptions(); + if (!empty($scriptOptions)) { $buffer .= $tab . '' . $lnEnd; + } + $defaultJsMimes = array('text/javascript', 'application/javascript', 'text/x-javascript', 'application/x-javascript'); // Generate script file links @@ -224,21 +239,6 @@ public function fetchHead($document) $buffer .= '>' . $lnEnd; } - // Generate scripts options - $scriptOptions = $document->getScriptOptions(); - - if (!empty($scriptOptions)) - { - $buffer .= $tab . '' . $lnEnd; - } - // Generate script declarations foreach ($document->_script as $type => $content) { From dae97f20dfb85110ce15744311f8f38fb463474c Mon Sep 17 00:00:00 2001 From: Fedik Date: Sun, 21 Aug 2016 13:42:57 +0300 Subject: [PATCH 09/13] Respect to Boolean `false` --- media/system/js/core-uncompressed.js | 4 ++-- media/system/js/core.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/media/system/js/core-uncompressed.js b/media/system/js/core-uncompressed.js index 63612b2f49e40..7e224fb4df6e3 100644 --- a/media/system/js/core-uncompressed.js +++ b/media/system/js/core-uncompressed.js @@ -90,7 +90,7 @@ Joomla.editors.instances = Joomla.editors.instances || {}; * * @since __DEPLOY_VERSION__ */ - Joomla.getOptions = function( key, def) { + Joomla.getOptions = function( key, def ) { // Load options if they not exists if (!Joomla.optionsStorage) { @@ -101,7 +101,7 @@ Joomla.editors.instances = Joomla.editors.instances || {}; Joomla.optionsStorage = options || {}; } - return Joomla.optionsStorage[key] || def; + return Joomla.optionsStorage[key] !== undefined ? Joomla.optionsStorage[key] : def; }; /** diff --git a/media/system/js/core.js b/media/system/js/core.js index 7c906463ca779..29a4cdbb193fe 100644 --- a/media/system/js/core.js +++ b/media/system/js/core.js @@ -1 +1 @@ -Joomla=window.Joomla||{};Joomla.editors=Joomla.editors||{};Joomla.editors.instances=Joomla.editors.instances||{};(function(Joomla,document){"use strict";Joomla.submitform=function(task,form,validate){if(!form){form=document.getElementById("adminForm")}if(task){form.task.value=task}form.noValidate=!validate;form.setAttribute("novalidate",!validate);var button=document.createElement("input");button.style.display="none";button.type="submit";form.appendChild(button).click();form.removeChild(button)};Joomla.submitbutton=function(pressbutton){Joomla.submitform(pressbutton)};Joomla.JText={strings:{},_:function(key,def){return typeof this.strings[key.toUpperCase()]!=="undefined"?this.strings[key.toUpperCase()]:def},load:function(object){for(var key in object){if(!object.hasOwnProperty(key))continue;this.strings[key.toUpperCase()]=object[key]}return this}};Joomla.optionsStorage=Joomla.optionsStorage||null;Joomla.getOptions=function(key,def){if(!Joomla.optionsStorage){var element=document.getElementById("joomla-script-options"),str=element?element.text||element.textContent:null,options=str?JSON.parse(str):null;Joomla.optionsStorage=options||{}}return Joomla.optionsStorage[key]||def};Joomla.replaceTokens=function(newToken){if(!/^[0-9A-F]{32}$/i.test(newToken)){return}var els=document.getElementsByTagName("input"),i,el,n;for(i=0,n=els.length;i=0;i--){messageWrapper=document.createElement("div");messageWrapper.innerHTML=typeMessages[i];messagesBox.appendChild(messageWrapper)}messageContainer.appendChild(messagesBox)}};Joomla.removeMessages=function(){var messageContainer=document.getElementById("system-message-container");while(messageContainer.firstChild)messageContainer.removeChild(messageContainer.firstChild);messageContainer.style.display="none";messageContainer.offsetHeight;messageContainer.style.display=""};Joomla.ajaxErrorsMessages=function(jqXHR,textStatus,error){var msg={};if(textStatus=="parsererror"){var encodedJson=jqXHR.responseText.trim();var buf=[];for(var i=encodedJson.length-1;i>=0;i--){buf.unshift(["&#",encodedJson[i].charCodeAt(),";"].join(""))}encodedJson=buf.join("");msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_PARSE").replace("%s",encodedJson)]}else if(textStatus=="nocontent"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_NO_CONTENT")]}else if(textStatus=="timeout"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_TIMEOUT")]}else if(textStatus=="abort"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_CONNECTION_ABORT")]}else{msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_OTHER").replace("%s",jqXHR.status)]}return msg};Joomla.isChecked=function(isitchecked,form){if(typeof form==="undefined"){form=document.getElementById("adminForm")}form.boxchecked.value=isitchecked?parseInt(form.boxchecked.value)+1:parseInt(form.boxchecked.value)-1;if(!form.elements["checkall-toggle"])return;var c=true,i,e,n;for(i=0,n=form.elements.length;i",hasSelection=key==orig_key,i=0,selected,x,item;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}selected="";if(hasSelection&&orig_val==item[1]||!hasSelection&&i===0){selected='selected="selected"'}html+='";i++}html+="";document.writeln(html)};window.changeDynaList=function(listname,source,key,orig_key,orig_val){var list=document.adminForm[listname],hasSelection=key==orig_key,i,x,item,opt;while(list.firstChild)list.removeChild(list.firstChild);i=0;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}opt=new Option;opt.value=item[1];opt.text=item[2];if(hasSelection&&orig_val==opt.value||!hasSelection&&i===0){opt.selected=true}list.options[i++]=opt}list.length=i};window.radioGetCheckedValue=function(radioObj){if(!radioObj){return""}var n=radioObj.length,i;if(n===undefined){return radioObj.checked?radioObj.value:""}for(i=0;i-1){return srcList.options[i].value}else{return null}};window.listItemTask=function(id,task){var f=document.adminForm,i=0,cbx,cb=f[id];if(!cb)return false;while(true){cbx=f["cb"+i];if(!cbx)break;cbx.checked=false;i++}cb.checked=true;f.boxchecked.value=1;window.submitform(task);return false};window.submitbutton=function(pressbutton){Joomla.submitbutton(pressbutton)};window.submitform=function(pressbutton){Joomla.submitform(pressbutton)};window.saveorder=function(n,task){window.checkAll_button(n,task)};window.checkAll_button=function(n,task){task=task?task:"saveorder";var j,box;for(j=0;j<=n;j++){box=document.adminForm["cb"+j];if(box){box.checked=true}else{alert("You cannot change the order of items, as an item in the list is `Checked Out`");return}}Joomla.submitform(task)};Joomla.loadingLayer=function(task,parentElement){task=task||"show";parentElement=parentElement||document.body;if(task=="load"){var basePath=document.getElementsByTagName("body")[0].getAttribute("data-basepath")||"";var loadingDiv=document.createElement("div");loadingDiv.id="loading-logo";loadingDiv.style["position"]="fixed";loadingDiv.style["top"]="0";loadingDiv.style["left"]="0";loadingDiv.style["width"]="100%";loadingDiv.style["height"]="100%";loadingDiv.style["opacity"]="0.8";loadingDiv.style["filter"]="alpha(opacity=80)";loadingDiv.style["overflow"]="hidden";loadingDiv.style["z-index"]="10000";loadingDiv.style["display"]="none";loadingDiv.style["background-color"]="#fff";loadingDiv.style["background-image"]='url("'+basePath+'/media/jui/images/ajax-loader.gif")';loadingDiv.style["background-position"]="center";loadingDiv.style["background-repeat"]="no-repeat";loadingDiv.style["background-attachment"]="fixed";parentElement.appendChild(loadingDiv)}else{if(!document.getElementById("loading-logo")){Joomla.loadingLayer("load",parentElement)}document.getElementById("loading-logo").style["display"]=task=="show"?"block":"none"}return document.getElementById("loading-logo")}})(Joomla,document); \ No newline at end of file +Joomla=window.Joomla||{};Joomla.editors=Joomla.editors||{};Joomla.editors.instances=Joomla.editors.instances||{};(function(Joomla,document){"use strict";Joomla.submitform=function(task,form,validate){if(!form){form=document.getElementById("adminForm")}if(task){form.task.value=task}form.noValidate=!validate;form.setAttribute("novalidate",!validate);var button=document.createElement("input");button.style.display="none";button.type="submit";form.appendChild(button).click();form.removeChild(button)};Joomla.submitbutton=function(pressbutton){Joomla.submitform(pressbutton)};Joomla.JText={strings:{},_:function(key,def){return typeof this.strings[key.toUpperCase()]!=="undefined"?this.strings[key.toUpperCase()]:def},load:function(object){for(var key in object){if(!object.hasOwnProperty(key))continue;this.strings[key.toUpperCase()]=object[key]}return this}};Joomla.optionsStorage=Joomla.optionsStorage||null;Joomla.getOptions=function(key,def){if(!Joomla.optionsStorage){var element=document.getElementById("joomla-script-options"),str=element?element.text||element.textContent:null,options=str?JSON.parse(str):null;Joomla.optionsStorage=options||{}}return Joomla.optionsStorage[key]!==undefined?Joomla.optionsStorage[key]:def};Joomla.replaceTokens=function(newToken){if(!/^[0-9A-F]{32}$/i.test(newToken)){return}var els=document.getElementsByTagName("input"),i,el,n;for(i=0,n=els.length;i=0;i--){messageWrapper=document.createElement("div");messageWrapper.innerHTML=typeMessages[i];messagesBox.appendChild(messageWrapper)}messageContainer.appendChild(messagesBox)}};Joomla.removeMessages=function(){var messageContainer=document.getElementById("system-message-container");while(messageContainer.firstChild)messageContainer.removeChild(messageContainer.firstChild);messageContainer.style.display="none";messageContainer.offsetHeight;messageContainer.style.display=""};Joomla.ajaxErrorsMessages=function(jqXHR,textStatus,error){var msg={};if(textStatus=="parsererror"){var encodedJson=jqXHR.responseText.trim();var buf=[];for(var i=encodedJson.length-1;i>=0;i--){buf.unshift(["&#",encodedJson[i].charCodeAt(),";"].join(""))}encodedJson=buf.join("");msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_PARSE").replace("%s",encodedJson)]}else if(textStatus=="nocontent"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_NO_CONTENT")]}else if(textStatus=="timeout"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_TIMEOUT")]}else if(textStatus=="abort"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_CONNECTION_ABORT")]}else{msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_OTHER").replace("%s",jqXHR.status)]}return msg};Joomla.isChecked=function(isitchecked,form){if(typeof form==="undefined"){form=document.getElementById("adminForm")}form.boxchecked.value=isitchecked?parseInt(form.boxchecked.value)+1:parseInt(form.boxchecked.value)-1;if(!form.elements["checkall-toggle"])return;var c=true,i,e,n;for(i=0,n=form.elements.length;i",hasSelection=key==orig_key,i=0,selected,x,item;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}selected="";if(hasSelection&&orig_val==item[1]||!hasSelection&&i===0){selected='selected="selected"'}html+='";i++}html+="";document.writeln(html)};window.changeDynaList=function(listname,source,key,orig_key,orig_val){var list=document.adminForm[listname],hasSelection=key==orig_key,i,x,item,opt;while(list.firstChild)list.removeChild(list.firstChild);i=0;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}opt=new Option;opt.value=item[1];opt.text=item[2];if(hasSelection&&orig_val==opt.value||!hasSelection&&i===0){opt.selected=true}list.options[i++]=opt}list.length=i};window.radioGetCheckedValue=function(radioObj){if(!radioObj){return""}var n=radioObj.length,i;if(n===undefined){return radioObj.checked?radioObj.value:""}for(i=0;i-1){return srcList.options[i].value}else{return null}};window.listItemTask=function(id,task){var f=document.adminForm,i=0,cbx,cb=f[id];if(!cb)return false;while(true){cbx=f["cb"+i];if(!cbx)break;cbx.checked=false;i++}cb.checked=true;f.boxchecked.value=1;window.submitform(task);return false};window.submitbutton=function(pressbutton){Joomla.submitbutton(pressbutton)};window.submitform=function(pressbutton){Joomla.submitform(pressbutton)};window.saveorder=function(n,task){window.checkAll_button(n,task)};window.checkAll_button=function(n,task){task=task?task:"saveorder";var j,box;for(j=0;j<=n;j++){box=document.adminForm["cb"+j];if(box){box.checked=true}else{alert("You cannot change the order of items, as an item in the list is `Checked Out`");return}}Joomla.submitform(task)};Joomla.loadingLayer=function(task,parentElement){task=task||"show";parentElement=parentElement||document.body;if(task=="load"){var basePath=document.getElementsByTagName("body")[0].getAttribute("data-basepath")||"";var loadingDiv=document.createElement("div");loadingDiv.id="loading-logo";loadingDiv.style["position"]="fixed";loadingDiv.style["top"]="0";loadingDiv.style["left"]="0";loadingDiv.style["width"]="100%";loadingDiv.style["height"]="100%";loadingDiv.style["opacity"]="0.8";loadingDiv.style["filter"]="alpha(opacity=80)";loadingDiv.style["overflow"]="hidden";loadingDiv.style["z-index"]="10000";loadingDiv.style["display"]="none";loadingDiv.style["background-color"]="#fff";loadingDiv.style["background-image"]='url("'+basePath+'/media/jui/images/ajax-loader.gif")';loadingDiv.style["background-position"]="center";loadingDiv.style["background-repeat"]="no-repeat";loadingDiv.style["background-attachment"]="fixed";parentElement.appendChild(loadingDiv)}else{if(!document.getElementById("loading-logo")){Joomla.loadingLayer("load",parentElement)}document.getElementById("loading-logo").style["display"]=task=="show"?"block":"none"}return document.getElementById("loading-logo")}})(Joomla,document); \ No newline at end of file From 0b02d968cc87eb56071afd591bbf4695f605232a Mon Sep 17 00:00:00 2001 From: Fedik Date: Sun, 21 Aug 2016 14:08:28 +0300 Subject: [PATCH 10/13] Testing for Joomla.getOptions --- tests/javascript/core/fixtures/fixture.html | 7 +++++++ tests/javascript/core/spec.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tests/javascript/core/fixtures/fixture.html b/tests/javascript/core/fixtures/fixture.html index c7e001fa3c389..91e9127e8d15b 100644 --- a/tests/javascript/core/fixtures/fixture.html +++ b/tests/javascript/core/fixtures/fixture.html @@ -36,4 +36,11 @@
+
+ +
diff --git a/tests/javascript/core/spec.js b/tests/javascript/core/spec.js index 893731e9727ce..854d65c4aec3f 100644 --- a/tests/javascript/core/spec.js +++ b/tests/javascript/core/spec.js @@ -198,4 +198,22 @@ define(['jquery', 'testsRoot/core/spec-setup', 'jasmineJquery'], function ($) { expect(this.form.filter_order_Dir.value).toEqual('dir') }); }); + + describe('Core Joomla.getOptions', function () { + it('should be Joomla.optionsStorage = null', function () { + expect(Joomla.optionsStorage).toEqual(null) + }); + it('should return options array Joomla.getOptions("com_foobar")', function () { + expect(Joomla.getOptions("com_foobar")).toEqual(["my options"]) + }); + it('should return option string Joomla.getOptions("com_foobar2")', function () { + expect(Joomla.getOptions("com_foobar2")).toEqual("Alert message!") + }); + it('should return option Boolean false Joomla.getOptions("com_foobar3")', function () { + expect(Joomla.getOptions("com_foobar3")).toEqual(false) + }); + it('should return default value for not existing key Joomla.getOptions("com_foobar4", 123)', function () { + expect(Joomla.getOptions("com_foobar4", 123)).toEqual(123) + }); + }); }); From cfe2bed1f0b913b1a06bb697a57eb24c4d63efae Mon Sep 17 00:00:00 2001 From: Fedik Date: Sun, 21 Aug 2016 15:49:05 +0300 Subject: [PATCH 11/13] Joomla.loadOptions allows to load a dynamically added options --- media/system/js/core-uncompressed.js | 47 ++++++++++++++++++--- media/system/js/core.js | 2 +- tests/javascript/core/fixtures/fixture.html | 2 +- tests/javascript/core/spec.js | 19 +++++++++ 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/media/system/js/core-uncompressed.js b/media/system/js/core-uncompressed.js index 7e224fb4df6e3..8a6c5b858ac28 100644 --- a/media/system/js/core-uncompressed.js +++ b/media/system/js/core-uncompressed.js @@ -91,19 +91,54 @@ Joomla.editors.instances = Joomla.editors.instances || {}; * @since __DEPLOY_VERSION__ */ Joomla.getOptions = function( key, def ) { - // Load options if they not exists if (!Joomla.optionsStorage) { - var element = document.getElementById('joomla-script-options'), - str = element ? (element.text || element.textContent) : null, - options = str ? JSON.parse(str) : null; - - Joomla.optionsStorage = options || {}; + Joomla.loadOptions(); } return Joomla.optionsStorage[key] !== undefined ? Joomla.optionsStorage[key] : def; }; + /** + * Load new options from given options object or from Element + * + * @param {Object|undefined} options The options object to load. Eg {"com_foobar" : {"option1": 1, "option2": 2}} + * + * @since __DEPLOY_VERSION__ + */ + Joomla.loadOptions = function( options ) { + // Load form the script container + if (!options) { + var elements = document.getElementsByClassName('joomla-script-options-new'), + str, element, option; + + for (var i = 0, l = elements.length; i < l; i++) { + element = elements[i]; + str = element.text || element.textContent; + option = JSON.parse(str); + + option ? Joomla.loadOptions(option) : null; + + element.className = 'joomla-script-options-loaded'; + } + + return; + } + + // Initial loading + if (!Joomla.optionsStorage) { + Joomla.optionsStorage = options; + } + // Merge with existing + else { + for (var p in options) { + if (options.hasOwnProperty(p)) { + Joomla.optionsStorage[p] = options[p]; + } + } + } + }; + /** * Method to replace all request tokens on the page with a new one. * Used in Joomla Installation diff --git a/media/system/js/core.js b/media/system/js/core.js index 29a4cdbb193fe..f67fd8f5dc7ee 100644 --- a/media/system/js/core.js +++ b/media/system/js/core.js @@ -1 +1 @@ -Joomla=window.Joomla||{};Joomla.editors=Joomla.editors||{};Joomla.editors.instances=Joomla.editors.instances||{};(function(Joomla,document){"use strict";Joomla.submitform=function(task,form,validate){if(!form){form=document.getElementById("adminForm")}if(task){form.task.value=task}form.noValidate=!validate;form.setAttribute("novalidate",!validate);var button=document.createElement("input");button.style.display="none";button.type="submit";form.appendChild(button).click();form.removeChild(button)};Joomla.submitbutton=function(pressbutton){Joomla.submitform(pressbutton)};Joomla.JText={strings:{},_:function(key,def){return typeof this.strings[key.toUpperCase()]!=="undefined"?this.strings[key.toUpperCase()]:def},load:function(object){for(var key in object){if(!object.hasOwnProperty(key))continue;this.strings[key.toUpperCase()]=object[key]}return this}};Joomla.optionsStorage=Joomla.optionsStorage||null;Joomla.getOptions=function(key,def){if(!Joomla.optionsStorage){var element=document.getElementById("joomla-script-options"),str=element?element.text||element.textContent:null,options=str?JSON.parse(str):null;Joomla.optionsStorage=options||{}}return Joomla.optionsStorage[key]!==undefined?Joomla.optionsStorage[key]:def};Joomla.replaceTokens=function(newToken){if(!/^[0-9A-F]{32}$/i.test(newToken)){return}var els=document.getElementsByTagName("input"),i,el,n;for(i=0,n=els.length;i=0;i--){messageWrapper=document.createElement("div");messageWrapper.innerHTML=typeMessages[i];messagesBox.appendChild(messageWrapper)}messageContainer.appendChild(messagesBox)}};Joomla.removeMessages=function(){var messageContainer=document.getElementById("system-message-container");while(messageContainer.firstChild)messageContainer.removeChild(messageContainer.firstChild);messageContainer.style.display="none";messageContainer.offsetHeight;messageContainer.style.display=""};Joomla.ajaxErrorsMessages=function(jqXHR,textStatus,error){var msg={};if(textStatus=="parsererror"){var encodedJson=jqXHR.responseText.trim();var buf=[];for(var i=encodedJson.length-1;i>=0;i--){buf.unshift(["&#",encodedJson[i].charCodeAt(),";"].join(""))}encodedJson=buf.join("");msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_PARSE").replace("%s",encodedJson)]}else if(textStatus=="nocontent"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_NO_CONTENT")]}else if(textStatus=="timeout"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_TIMEOUT")]}else if(textStatus=="abort"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_CONNECTION_ABORT")]}else{msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_OTHER").replace("%s",jqXHR.status)]}return msg};Joomla.isChecked=function(isitchecked,form){if(typeof form==="undefined"){form=document.getElementById("adminForm")}form.boxchecked.value=isitchecked?parseInt(form.boxchecked.value)+1:parseInt(form.boxchecked.value)-1;if(!form.elements["checkall-toggle"])return;var c=true,i,e,n;for(i=0,n=form.elements.length;i",hasSelection=key==orig_key,i=0,selected,x,item;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}selected="";if(hasSelection&&orig_val==item[1]||!hasSelection&&i===0){selected='selected="selected"'}html+='";i++}html+="";document.writeln(html)};window.changeDynaList=function(listname,source,key,orig_key,orig_val){var list=document.adminForm[listname],hasSelection=key==orig_key,i,x,item,opt;while(list.firstChild)list.removeChild(list.firstChild);i=0;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}opt=new Option;opt.value=item[1];opt.text=item[2];if(hasSelection&&orig_val==opt.value||!hasSelection&&i===0){opt.selected=true}list.options[i++]=opt}list.length=i};window.radioGetCheckedValue=function(radioObj){if(!radioObj){return""}var n=radioObj.length,i;if(n===undefined){return radioObj.checked?radioObj.value:""}for(i=0;i-1){return srcList.options[i].value}else{return null}};window.listItemTask=function(id,task){var f=document.adminForm,i=0,cbx,cb=f[id];if(!cb)return false;while(true){cbx=f["cb"+i];if(!cbx)break;cbx.checked=false;i++}cb.checked=true;f.boxchecked.value=1;window.submitform(task);return false};window.submitbutton=function(pressbutton){Joomla.submitbutton(pressbutton)};window.submitform=function(pressbutton){Joomla.submitform(pressbutton)};window.saveorder=function(n,task){window.checkAll_button(n,task)};window.checkAll_button=function(n,task){task=task?task:"saveorder";var j,box;for(j=0;j<=n;j++){box=document.adminForm["cb"+j];if(box){box.checked=true}else{alert("You cannot change the order of items, as an item in the list is `Checked Out`");return}}Joomla.submitform(task)};Joomla.loadingLayer=function(task,parentElement){task=task||"show";parentElement=parentElement||document.body;if(task=="load"){var basePath=document.getElementsByTagName("body")[0].getAttribute("data-basepath")||"";var loadingDiv=document.createElement("div");loadingDiv.id="loading-logo";loadingDiv.style["position"]="fixed";loadingDiv.style["top"]="0";loadingDiv.style["left"]="0";loadingDiv.style["width"]="100%";loadingDiv.style["height"]="100%";loadingDiv.style["opacity"]="0.8";loadingDiv.style["filter"]="alpha(opacity=80)";loadingDiv.style["overflow"]="hidden";loadingDiv.style["z-index"]="10000";loadingDiv.style["display"]="none";loadingDiv.style["background-color"]="#fff";loadingDiv.style["background-image"]='url("'+basePath+'/media/jui/images/ajax-loader.gif")';loadingDiv.style["background-position"]="center";loadingDiv.style["background-repeat"]="no-repeat";loadingDiv.style["background-attachment"]="fixed";parentElement.appendChild(loadingDiv)}else{if(!document.getElementById("loading-logo")){Joomla.loadingLayer("load",parentElement)}document.getElementById("loading-logo").style["display"]=task=="show"?"block":"none"}return document.getElementById("loading-logo")}})(Joomla,document); \ No newline at end of file +Joomla=window.Joomla||{};Joomla.editors=Joomla.editors||{};Joomla.editors.instances=Joomla.editors.instances||{};(function(Joomla,document){"use strict";Joomla.submitform=function(task,form,validate){if(!form){form=document.getElementById("adminForm")}if(task){form.task.value=task}form.noValidate=!validate;form.setAttribute("novalidate",!validate);var button=document.createElement("input");button.style.display="none";button.type="submit";form.appendChild(button).click();form.removeChild(button)};Joomla.submitbutton=function(pressbutton){Joomla.submitform(pressbutton)};Joomla.JText={strings:{},_:function(key,def){return typeof this.strings[key.toUpperCase()]!=="undefined"?this.strings[key.toUpperCase()]:def},load:function(object){for(var key in object){if(!object.hasOwnProperty(key))continue;this.strings[key.toUpperCase()]=object[key]}return this}};Joomla.optionsStorage=Joomla.optionsStorage||null;Joomla.getOptions=function(key,def){if(!Joomla.optionsStorage){Joomla.loadOptions()}return Joomla.optionsStorage[key]!==undefined?Joomla.optionsStorage[key]:def};Joomla.loadOptions=function(options){if(!options){var elements=document.getElementsByClassName("joomla-script-options-new"),str,element,option;for(var i=0,l=elements.length;i=0;i--){messageWrapper=document.createElement("div");messageWrapper.innerHTML=typeMessages[i];messagesBox.appendChild(messageWrapper)}messageContainer.appendChild(messagesBox)}};Joomla.removeMessages=function(){var messageContainer=document.getElementById("system-message-container");while(messageContainer.firstChild)messageContainer.removeChild(messageContainer.firstChild);messageContainer.style.display="none";messageContainer.offsetHeight;messageContainer.style.display=""};Joomla.ajaxErrorsMessages=function(jqXHR,textStatus,error){var msg={};if(textStatus=="parsererror"){var encodedJson=jqXHR.responseText.trim();var buf=[];for(var i=encodedJson.length-1;i>=0;i--){buf.unshift(["&#",encodedJson[i].charCodeAt(),";"].join(""))}encodedJson=buf.join("");msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_PARSE").replace("%s",encodedJson)]}else if(textStatus=="nocontent"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_NO_CONTENT")]}else if(textStatus=="timeout"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_TIMEOUT")]}else if(textStatus=="abort"){msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_CONNECTION_ABORT")]}else{msg.error=[Joomla.JText._("JLIB_JS_AJAX_ERROR_OTHER").replace("%s",jqXHR.status)]}return msg};Joomla.isChecked=function(isitchecked,form){if(typeof form==="undefined"){form=document.getElementById("adminForm")}form.boxchecked.value=isitchecked?parseInt(form.boxchecked.value)+1:parseInt(form.boxchecked.value)-1;if(!form.elements["checkall-toggle"])return;var c=true,i,e,n;for(i=0,n=form.elements.length;i",hasSelection=key==orig_key,i=0,selected,x,item;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}selected="";if(hasSelection&&orig_val==item[1]||!hasSelection&&i===0){selected='selected="selected"'}html+='";i++}html+="";document.writeln(html)};window.changeDynaList=function(listname,source,key,orig_key,orig_val){var list=document.adminForm[listname],hasSelection=key==orig_key,i,x,item,opt;while(list.firstChild)list.removeChild(list.firstChild);i=0;for(x in source){if(!source.hasOwnProperty(x)){continue}item=source[x];if(item[0]!=key){continue}opt=new Option;opt.value=item[1];opt.text=item[2];if(hasSelection&&orig_val==opt.value||!hasSelection&&i===0){opt.selected=true}list.options[i++]=opt}list.length=i};window.radioGetCheckedValue=function(radioObj){if(!radioObj){return""}var n=radioObj.length,i;if(n===undefined){return radioObj.checked?radioObj.value:""}for(i=0;i-1){return srcList.options[i].value}else{return null}};window.listItemTask=function(id,task){var f=document.adminForm,i=0,cbx,cb=f[id];if(!cb)return false;while(true){cbx=f["cb"+i];if(!cbx)break;cbx.checked=false;i++}cb.checked=true;f.boxchecked.value=1;window.submitform(task);return false};window.submitbutton=function(pressbutton){Joomla.submitbutton(pressbutton)};window.submitform=function(pressbutton){Joomla.submitform(pressbutton)};window.saveorder=function(n,task){window.checkAll_button(n,task)};window.checkAll_button=function(n,task){task=task?task:"saveorder";var j,box;for(j=0;j<=n;j++){box=document.adminForm["cb"+j];if(box){box.checked=true}else{alert("You cannot change the order of items, as an item in the list is `Checked Out`");return}}Joomla.submitform(task)};Joomla.loadingLayer=function(task,parentElement){task=task||"show";parentElement=parentElement||document.body;if(task=="load"){var basePath=document.getElementsByTagName("body")[0].getAttribute("data-basepath")||"";var loadingDiv=document.createElement("div");loadingDiv.id="loading-logo";loadingDiv.style["position"]="fixed";loadingDiv.style["top"]="0";loadingDiv.style["left"]="0";loadingDiv.style["width"]="100%";loadingDiv.style["height"]="100%";loadingDiv.style["opacity"]="0.8";loadingDiv.style["filter"]="alpha(opacity=80)";loadingDiv.style["overflow"]="hidden";loadingDiv.style["z-index"]="10000";loadingDiv.style["display"]="none";loadingDiv.style["background-color"]="#fff";loadingDiv.style["background-image"]='url("'+basePath+'/media/jui/images/ajax-loader.gif")';loadingDiv.style["background-position"]="center";loadingDiv.style["background-repeat"]="no-repeat";loadingDiv.style["background-attachment"]="fixed";parentElement.appendChild(loadingDiv)}else{if(!document.getElementById("loading-logo")){Joomla.loadingLayer("load",parentElement)}document.getElementById("loading-logo").style["display"]=task=="show"?"block":"none"}return document.getElementById("loading-logo")}})(Joomla,document); \ No newline at end of file diff --git a/tests/javascript/core/fixtures/fixture.html b/tests/javascript/core/fixtures/fixture.html index 91e9127e8d15b..f6282b70ad599 100644 --- a/tests/javascript/core/fixtures/fixture.html +++ b/tests/javascript/core/fixtures/fixture.html @@ -37,7 +37,7 @@
-