/** * Copyright (c) Bjarki Gudlaugsson (codehugger at codehuggers.com) * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * * Neither the name of the nor the names of its contributors may be * used to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ (function(a){a.fn.categorypicker=function(j,b){var n=this;var h,f;var g;function k(q){var r=q.target.id;var p=q.target.checked;var o=[];a.each(f,function(s,t){if(r!=t[idField]){o.push(t)}});if(p){a.each(h,function(s,t){if(r==t[idField]){o.push(t)}})}f=o;f.sort(function(t,s){return t[sortField]>s[sortField]});e()}function i(){if(onInit){onInit(f)}}function e(){if(onUpdate){onUpdate(f)}}function d(p){var o=a(itemTemplate);var q=a('');q.attr("id",p[idField]);q.attr("value",p[valueField]);a.each(f,function(r,s){if(s[idField]===p[idField]){q.attr("checked",true)}});q.on("click",k);o.html(q);o.append(p[displayField]);return o}function l(){var o=a(listTemplate);a.each(h,function(p,q){o.append(d(q))});return o}function c(){var o=a(g);o.append(l());n.html(o)}function m(o){if(o===undefined){o={}}h=o.availableCategories||[];f=o.selectedCategories||[];idField=o.idField||"id";valueField=o.valueField||"name";displayField=o.displayField||valueField;sortField=o.sortField||valueField;onUpdate=o.onUpdate||null;onInit=o.onInit||null;g=o.rootTemplate||'
';listTemplate=o.listTemplate||'';itemTemplate=o.itemTemplate||'
  • ';c();i();return n}return m(j,b)}})(jQuery);