jquery-load-json
Version:
jQuery plugin that enables developers to load JSON data from the server and load JSON object into the DOM.
18 lines • 4.95 kB
JavaScript
/*!
* jQuery loadJSON plugin
* v1.3.3
* https://github.com/kevindb/jquery-load-json
*
* This is a jQuery plugin that enables developers to load JSON data from the server and load JSON object into the DOM.
*
* Copyright 2015 Kevin Morris
* Copyright 2011 Jovan Popovic
* Copyright 2008 Alexandre Caprais
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*/
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof module&&module.exports?module.exports=function(b,c){return void 0===c&&(c="undefined"!=typeof window?require("jquery"):require("jquery")(b)),a(c),c}:a(jQuery)}(function(a){"use strict";a.fn.loadJSON=function(b,c){function d(b){try{a.isFunction(b.selectmenu)&&b.selectmenu("refresh")}catch(a){}}function e(b){try{a.isFunction(b.checkboxradio)&&b.checkboxradio("refresh")}catch(a){}}function f(c,e,f){for(var g=jQuery.makeArray(c),h=a(g[g.length-1]).clone(!0),i=b.length,j=i-1,k=0;k<i;++k){var l=h.clone(!0).insertAfter(g[g.length-1]);a(l).attr("value",b[j].value),a(l).text(b[j].text),b[j].selected&&a(l).attr("selected",!0),j--}d(a(c))}function g(c,g,h){var i=a(c),j=c.type||c.tagName;if(null!==j||(j=c[0].type||c[0].tagName,null!==j))switch(j=j.toLowerCase()){case"radio":g.toString().toLowerCase()==c.value.toLowerCase()?i.prop("checked",!0):i.prop("checked",!1),e(i);break;case"checkbox":var k=i.val();g.constructor==Array&&g.contains(k)||g==k?i.prop("checked",!0):i.prop("checked",!1),e(i);break;case"option":i.attr("value",g.value),i.text(g.text),g.selected&&i.attr("selected",!0);break;case"select-multiple":var l=c[0];if(null!==c[0].options&&void 0!==c[0].options||(l=c),l.options.length>1){for(var m=g.constructor==Array?g:[g],n=0;n<l.options.length;n++)for(var o=0;o<m.length;o++)l.options[n].selected|=l.options[n].value==m[o];d(i)}else f(c,g,h);break;case"select":case"select-one":g.constructor==Array?f(c,g,h):(i.val(g),d(i));break;case"color":case"date":case"datetime":case"datetime-local":case"email":case"hidden":case"month":case"number":case"password":case"range":case"search":case"tel":case"text":case"time":case"url":case"week":i.val(g);break;case"a":var p=i.attr("href");if(p){var q=p.indexOf("#");q>1e6?p=p.substr(0,q)+"&"+h+"="+g+p.substr(q):(q=p.indexOf("?"),q>0?p+="&"+h+"="+g:p=p+"?"+h+"="+g)}else p=g;i.attr("href",p);break;case"img":if("String"==b.constructor){var q=g.indexOf("$"),r="",s="";if(q>0)r=g.substring(0,q),s=g.substring(q+1);else{r=g;var t=g.lastIndexOf("/")+1,u=g.indexOf(".");s=g.substring(t,u)}i.attr("src",r),i.attr("alt",s)}else i.attr("src",b.src),i.attr("alt",b.alt),i.attr("title",b.title);break;case"textarea":case"submit":case"button":default:try{i.html(g.toString())}catch(a){}}}function h(b,c,d){var e=a(c);if(void 0===b);else if(b.constructor==Object){c.length>=1&&"OPTION"==c[0].tagName&&g(c[0],b,d);for(var f in b)if(null!==f&&void 0!==f&&0!==f.length){var i=jQuery.makeArray(jQuery("."+f,c)).length>0?jQuery("."+f,c):jQuery("#"+f,c).length>0?jQuery("#"+f,c):jQuery('[name="'+f+'"]',c).length>0?jQuery('[name="'+f+'"]',c):jQuery('[rel="'+f+'"]');0!==i.length&&h(b[f],jQuery(i,c),f)}}else if(b.constructor==Array){if(1==c.length&&("select"==c.type||"select-one"==c.type||"select-multiple"==c.type||"select"==c[0].type||"select-one"==c[0].type||"select-multiple"==c[0].type))return void g(c,b,d);if(c.length>0&&"checkbox"==c[0].type)c.each(function(){g(this,b,d)});else{var j=e.children("[rel]");if(j.length>0)j.each(function(){var c=a(this);h(b[c.attr("rel")],c,d)});else{var k=jQuery.makeArray(c),l=k.length,m=a(k[l-1]).clone(!0),n=b.length,o=0;if(null===c[0]||null!==c[0]&&"OPTION"!=c[0].tagName){for(var p=0;p<l;++p){if(o<b.length){var q=e.eq(p);h(b[o],q,d)}o++}n=b.length-l}o=b.length-1;for(var r=0;r<n;++r){var s=m.clone(!0).insertAfter(k[l-1]);h(b[o],s,d),o--}}}}else{var t=c.length;if(t>0)for(var o=0;o<t;++o)g(c[o],b,d);else g(c,b,d)}}function i(a){var b;null!==a.data("loadJSON-template")&&""!=a.data("loadJSON-template")?(b=a.data("loadJSON-template"),a.html(b)):(b=a.html(),a.data("loadJSON-template",b))}Array.prototype.contains=function(a){for(var b=this.length;b--;)if(this[b]==a)return!0;return!1};var j={onLoading:function(){},onLoaded:function(){},mobile:!1},k=a.extend(j,c);return this.each(function(){var c=a(this);if(b.constructor==String)if("{"==b.charAt(0)||"["==b.charAt(0)){var d=a.parseJSON(b);i(c),k.onLoading(),h(d,this),k.onLoaded()}else{var e=c;a.ajax({url:b,success:function(a){e.loadJSON(a,k)},cache:!1,dataType:"json"})}else i(c),k.onLoading(),h(b,this),k.onLoaded()})}});