Team:SVCE Chennai/plugins/jquery.tagged

/*!

* jQuery Taggd
* A helpful plugin that helps you adding 'tags' on images.
*
* License: MIT
*/

!function(t){"use strict";var e={edit:!1,align:{x:"center",y:"center"},handlers:{},offset:{left:0,top:0},strings:{save:"✓","delete":"×"}},i={show:function(){var e=t(this),i=e.next();e.addClass("active"),i.addClass("show").find("input").focus()},hide:function(){var e=t(this);e.removeClass("active"),e.next().removeClass("show")},toggle:function(){var e=t(this).next();e.hasClass("show")?i.hide.call(this):i.show.call(this)}},n=function(n,a,s){var o=this;a.edit&&(a.handlers={click:function(){o.hide(),i.show.call(this)}}),this.element=t(n),this.options=t.extend(!0,{},e,a),this.data=s,this.initialized=!1,this.element.height()&&this.element.width()?this.initialize():this.element.on("load",o.initialize.bind(this))};n.prototype.initialize=function(){var e=this;this.initialized=!0,this.initWrapper(),this.addDOM(),this.options.edit&&this.element.on("click",function(i){var n=t(this).parent().offset(),a=(i.pageX-n.left)/e.element.width(),s=(i.pageY-n.top)/e.element.height();e.addData({x:a,y:s,text:""}),e.show(e.data.length-1)}),t(window).resize(function(){e.updateDOM()})},n.prototype.initWrapper=function(){var e=t('<div class="taggd-wrapper" />');this.element.wrap(e),this.wrapper=this.element.parent(".taggd-wrapper")},n.prototype.alterDOM=function(){var e=this;this.wrapper.find(".taggd-item-hover").each(function(){var i=t(this),n=t('<input type="text" size="16" />').val(i.text()),a=t("<button />").html(e.options.strings.save),s=t("<button />").html(e.options.strings["delete"]);a.on("click",function(){e.hide()}),s.on("click",function(){var n=i.attr("data-x"),a=i.attr("data-y");e.data=t.grep(e.data,function(t){return t.x!=n||t.y!=a}),e.addDOM(),e.element.triggerHandler("change")}),n.on("change",function(){var a=i.attr("data-x"),s=i.attr("data-y"),o=t.grep(e.data,function(t){return t.x==a&&t.y==s}).pop();o&&(o.text=n.val()),e.addDOM(),e.element.triggerHandler("change")}),i.empty().append(n,a,s)}),e.updateDOM()},n.prototype.addData=function(e){t.isArray(e)?this.data=t.merge(this.data,e):this.data.push(e),this.initialized&&(this.addDOM(),this.element.triggerHandler("change"))},n.prototype.setData=function(t){this.data=t,this.initialized&&this.addDOM()},n.prototype.clear=function(){this.initialized&&this.wrapper.find(".taggd-item, .taggd-item-hover").remove()},n.prototype.on=function(t,e){"string"==typeof t&&"function"==typeof e&&this.element.on(t,e)},n.prototype.iterateTags=function(e,i){var n;if(t.isNumeric(e))n=function(t){return e===t};else if("string"==typeof e)n=function(i,n){return t(n).is(e)};else if(t.isArray(e))n=function(i,n){var a=t(n),s=!1;return t.each(e,function(t,e){return i===t||n===e||a.is(e)?(s=!0,!1):void 0}),s};else if("object"==typeof e)n=function(i,n){var a=t(n);return a.is(e)};else if(t.isFunction(e))n=e;else{if(e)return this;n=function(){return!0}}return this.wrapper.find(".taggd-item").each(function(t,e){"function"==typeof i&&n.call(this,t,e)&&i.call(this,t,e)}),this},n.prototype.show=function(t){return this.iterateTags(t,i.show)},n.prototype.hide=function(t){return this.iterateTags(t,i.hide)},n.prototype.toggle=function(t){return this.iterateTags(t,i.toggle)},n.prototype.dispose=function(){this.clear(),this.element.unwrap(this.wrapper)},n.prototype.addDOM=function(){var e=this;this.clear(),this.element.css({height:"auto",width:"auto"});var n=this.element.height(),a=this.element.width();t.each(this.data,function(s,o){var r,h=t("<span />");o.x>1&&o.x%1===0&&o.y>1&&o.y%1===0&&(o.x=o.x/a,o.y=o.y/n),"object"==typeof o.attributes&&h.attr(o.attributes),h.attr({"data-x":o.x,"data-y":o.y}),h.css("position","absolute"),h.addClass("taggd-item"),e.wrapper.append(h),"string"==typeof o.text&&(o.text.length>0||e.options.edit)&&(r=t('<span class="taggd-item-hover" style="position: absolute;" />').html(o.text),r.attr({"data-x":o.x,"data-y":o.y}),e.wrapper.append(r)),"object"==typeof e.options.handlers&&t.each(e.options.handlers,function(t,n){var a;"string"==typeof n&&i[n]?a=i[n]:"function"==typeof n&&(a=n),h.on(t,function(t){a&&a.call(h,t,e.data[s])})})}),this.element.removeAttr("style"),this.options.edit&&this.alterDOM(),this.updateDOM()},n.prototype.updateDOM=function(){var e=this;this.wrapper.removeAttr("style").css({height:this.element.height(),width:this.element.width()}),this.wrapper.find("span").each(function(i,n){var a=t(n),s=a.attr("data-x")*e.element.width(),o=a.attr("data-y")*e.element.height();a.hasClass("taggd-item")?a.css({left:s-a.outerWidth(!0)/2,top:o-a.outerHeight(!0)/2}):a.hasClass("taggd-item-hover")&&("center"===e.options.align.x?s-=a.outerWidth(!0)/2:"right"===e.options.align.x&&(s-=a.outerWidth(!0)),"center"===e.options.align.y?o-=a.outerHeight(!0)/2:"bottom"===e.options.align.y&&(o-=a.outerHeight(!0)),a.attr("data-align",a.outerWidth(!0)),a.css({left:s+e.options.offset.left,top:o+e.options.offset.top}))})},t.fn.taggd=function(t,e){return new n(this,t,e)}}(jQuery);