diff --git a/ng-qtip2.coffee b/ng-qtip2.coffee index 0cd800e..02ac650 100644 --- a/ng-qtip2.coffee +++ b/ng-qtip2.coffee @@ -1,3 +1,12 @@ +str2bool = (str) -> String(str).toLowerCase() not in ['false', '0', 'null', ''] + +removeEmpties = (obj, deep = true) -> + for k, v of obj + if v? and typeof v is 'object' and deep + removeEmpties obj[k], deep + else if not v? + delete obj[k] + NgQtip2 = ($timeout, $compile, $http, $templateCache, qtipDefaults, $q) -> restrict: 'A' scope: @@ -30,18 +39,20 @@ NgQtip2 = ($timeout, $compile, $http, $templateCache, qtipDefaults, $q) -> object: '=qtipTemplateObject' link: (scope, el, attrs) -> + # these link attrs to qtip props (el.qtip(prop, val)) + watchProps = + qtipVisible: 'toggle' + qtipDisable: 'disable' + + # these link attrs to qtip options (el.qtip('option', name, val)) + watchOptions = + qtipTitle: 'content.title' + qtipClass: 'style.classes' + qtip: 'content.text' + scope.qtipOptions ?= {} - - str2bool = (str) -> String(str).toLowerCase() not in ['false', '0', 'null', ''] - scope.apiPromise = $q.defer() - scope.getQtipId = -> - el.data('hasqtip') - - scope.getQtipElement = (id = scope.getQtipId()) -> - ($ "#qtip-#{id}") - scope.closeQtip = (e, id = scope.getQtipId(), {rendered = yes} = {}) -> e?.preventDefault?() qtEl = ($ "#qtip-#{id}") @@ -49,13 +60,14 @@ NgQtip2 = ($timeout, $compile, $http, $templateCache, qtipDefaults, $q) -> qtEl.qtip().rendered = scope.qtipPersistent ? rendered return - removeEmpties = (obj, deep = true) -> - for k, v of obj - if v? and typeof v is 'object' and deep - removeEmpties obj[k], deep - else if not v? - delete obj[k] + # Generic methods + scope.getQtipId = -> + el.data('hasqtip') + scope.getQtipElement = (id = scope.getQtipId()) -> + ($ "#qtip-#{id}") + + # qTip API scope.api = (e, id = scope.getQtipId()) -> qtEl = ($ "#qtip-#{id}") return qtEl.qtip "api" @@ -74,7 +86,9 @@ NgQtip2 = ($timeout, $compile, $http, $templateCache, qtipDefaults, $q) -> before?(arguments...) fn?(arguments...) + # Main init method generateQtip = (content) -> + # Default options attrOptions = position: my: scope.qtipMy @@ -96,40 +110,36 @@ NgQtip2 = ($timeout, $compile, $http, $templateCache, qtipDefaults, $q) -> tip: scope.qtipTipStyle content: if content? then content else text: scope.qtipContent ? scope.qtip + # Clear empty values to use defaults angular.merge attrOptions.hide, scope.qtipHide if scope.qtipHide? angular.merge attrOptions.show, scope.qtipShow if scope.qtipShow? removeEmpties options removeEmpties attrOptions removeEmpties scope.qtipOptions + + # Merge final opts options = angular.merge {}, qtipDefaults, attrOptions, scope.qtipOptions + # qTip API if options.events?.render? options.events.render = scope._before(scope.resolveApiPromise, options.events.render) else options.events ?= {} options.events.render = scope.resolveApiPromise + # Create qtip ($ el).qtip options - if attrs.qtipVisible? - scope.$watch 'qtipVisible', (newVal) -> - ($ el).qtip 'toggle', newVal + # Assign watch props/options pairs + for k, v of watchProps + scope.$watch k, (newVal) -> + el.qtip v, newVal - if attrs.qtipDisable? - scope.$watch 'qtipDisable', (newVal) -> - ($ el).qtip 'disable', newVal - - if scope.qtipTitle? - scope.$watch 'qtipTitle', (newVal) -> - ($ el).qtip 'option', 'content.title', newVal - - if scope.qtipClass? - scope.$watch 'qtipClass', (cls) -> - ($ el).qtip 'option', 'style.class', cls - - scope.$watch 'qtip', (newVal, oldVal) -> - ($ el).qtip 'option', 'content.text', newVal if newVal isnt oldVal + for k, v of watchOptions + scope.$watch k, (newVal) -> + el.qtip 'option', v, newVal + # Switch for triggering init by different types of main content/position if attrs.qtipSelector $timeout -> generateQtip ($ scope.qtipSelector).html() diff --git a/ng-qtip2.js b/ng-qtip2.js index 2919d7e..33e6164 100644 --- a/ng-qtip2.js +++ b/ng-qtip2.js @@ -1,6 +1,30 @@ // Generated by CoffeeScript 1.10.0 (function() { - var NgQtip2; + var NgQtip2, removeEmpties, str2bool; + + str2bool = function(str) { + var ref; + return (ref = String(str).toLowerCase()) !== 'false' && ref !== '0' && ref !== 'null' && ref !== ''; + }; + + removeEmpties = function(obj, deep) { + var k, results, v; + if (deep == null) { + deep = true; + } + results = []; + for (k in obj) { + v = obj[k]; + if ((v != null) && typeof v === 'object' && deep) { + results.push(removeEmpties(obj[k], deep)); + } else if (v == null) { + results.push(delete obj[k]); + } else { + results.push(void 0); + } + } + return results; + }; NgQtip2 = function($timeout, $compile, $http, $templateCache, qtipDefaults, $q) { return { @@ -35,24 +59,20 @@ object: '=qtipTemplateObject' }, link: function(scope, el, attrs) { - var content, generateQtip, ref, removeEmpties, str2bool; + var content, generateQtip, ref, watchOptions, watchProps; + watchProps = { + qtipVisible: 'toggle', + qtipDisable: 'disable' + }; + watchOptions = { + qtipTitle: 'content.title', + qtipClass: 'style.classes', + qtip: 'content.text' + }; if (scope.qtipOptions == null) { scope.qtipOptions = {}; } - str2bool = function(str) { - var ref; - return (ref = String(str).toLowerCase()) !== 'false' && ref !== '0' && ref !== 'null' && ref !== ''; - }; scope.apiPromise = $q.defer(); - scope.getQtipId = function() { - return el.data('hasqtip'); - }; - scope.getQtipElement = function(id) { - if (id == null) { - id = scope.getQtipId(); - } - return $("#qtip-" + id); - }; scope.closeQtip = function(e, id, arg) { var qtEl, ref, ref1, rendered; if (id == null) { @@ -68,23 +88,14 @@ qtEl.qtip('hide'); qtEl.qtip().rendered = (ref1 = scope.qtipPersistent) != null ? ref1 : rendered; }; - removeEmpties = function(obj, deep) { - var k, results, v; - if (deep == null) { - deep = true; + scope.getQtipId = function() { + return el.data('hasqtip'); + }; + scope.getQtipElement = function(id) { + if (id == null) { + id = scope.getQtipId(); } - results = []; - for (k in obj) { - v = obj[k]; - if ((v != null) && typeof v === 'object' && deep) { - results.push(removeEmpties(obj[k], deep)); - } else if (v == null) { - results.push(delete obj[k]); - } else { - results.push(void 0); - } - } - return results; + return $("#qtip-" + id); }; scope.api = function(e, id) { var qtEl; @@ -114,7 +125,7 @@ }; }; generateQtip = function(content) { - var attrOptions, options, ref, ref1; + var attrOptions, k, options, ref, ref1, results, v; attrOptions = { position: { my: scope.qtipMy, @@ -162,31 +173,20 @@ options.events.render = scope.resolveApiPromise; } ($(el)).qtip(options); - if (attrs.qtipVisible != null) { - scope.$watch('qtipVisible', function(newVal) { - return ($(el)).qtip('toggle', newVal); + for (k in watchProps) { + v = watchProps[k]; + scope.$watch(k, function(newVal) { + return el.qtip(v, newVal); }); } - if (attrs.qtipDisable != null) { - scope.$watch('qtipDisable', function(newVal) { - return ($(el)).qtip('disable', newVal); - }); + results = []; + for (k in watchOptions) { + v = watchOptions[k]; + results.push(scope.$watch(k, function(newVal) { + return el.qtip('option', v, newVal); + })); } - if (scope.qtipTitle != null) { - scope.$watch('qtipTitle', function(newVal) { - return ($(el)).qtip('option', 'content.title', newVal); - }); - } - if (scope.qtipClass != null) { - scope.$watch('qtipClass', function(cls) { - return ($(el)).qtip('option', 'style.class', cls); - }); - } - return scope.$watch('qtip', function(newVal, oldVal) { - if (newVal !== oldVal) { - return ($(el)).qtip('option', 'content.text', newVal); - } - }); + return results; }; if (attrs.qtipSelector) { $timeout(function() {