ui-jq.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. /**
  3. * 0.1.1
  4. * General-purpose jQuery wrapper. Simply pass the plugin name as the expression.
  5. *
  6. * It is possible to specify a default set of parameters for each jQuery plugin.
  7. * Under the jq key, namespace each plugin by that which will be passed to ui-jq.
  8. * Unfortunately, at this time you can only pre-define the first parameter.
  9. * @example { jq : { datepicker : { showOn:'click' } } }
  10. *
  11. * @param ui-jq {string} The $elm.[pluginName]() to call.
  12. * @param [ui-options] {mixed} Expression to be evaluated and passed as options to the function
  13. * Multiple parameters can be separated by commas
  14. * @param [ui-refresh] {expression} Watch expression and refire plugin on changes
  15. *
  16. * @example <input ui-jq="datepicker" ui-options="{showOn:'click'},secondParameter,thirdParameter" ui-refresh="iChange">
  17. */
  18. angular.module('ui.jq', ['ui.load']).
  19. value('uiJqConfig', {}).
  20. directive('uiJq', ['uiJqConfig', 'JQ_CONFIG', 'uiLoad', '$timeout', function uiJqInjectingFunction(uiJqConfig, JQ_CONFIG, uiLoad, $timeout) {
  21. return {
  22. restrict: 'A',
  23. compile: function uiJqCompilingFunction(tElm, tAttrs) {
  24. if (!angular.isFunction(tElm[tAttrs.uiJq]) && !JQ_CONFIG[tAttrs.uiJq]) {
  25. throw new Error('ui-jq: The "' + tAttrs.uiJq + '" function does not exist');
  26. }
  27. var options = uiJqConfig && uiJqConfig[tAttrs.uiJq];
  28. return function uiJqLinkingFunction(scope, elm, attrs) {
  29. function getOptions(){
  30. var linkOptions = [];
  31. // If ui-options are passed, merge (or override) them onto global defaults and pass to the jQuery method
  32. if (attrs.uiOptions) {
  33. linkOptions = scope.$eval('[' + attrs.uiOptions + ']');
  34. if (angular.isObject(options) && angular.isObject(linkOptions[0])) {
  35. linkOptions[0] = angular.extend({}, options, linkOptions[0]);
  36. }
  37. } else if (options) {
  38. linkOptions = [options];
  39. }
  40. return linkOptions;
  41. }
  42. // If change compatibility is enabled, the form input's "change" event will trigger an "input" event
  43. if (attrs.ngModel && elm.is('select,input,textarea')) {
  44. elm.bind('change', function() {
  45. elm.trigger('input');
  46. });
  47. }
  48. // Call jQuery method and pass relevant options
  49. function callPlugin() {
  50. $timeout(function() {
  51. elm[attrs.uiJq].apply(elm, getOptions());
  52. }, 0, false);
  53. }
  54. function refresh(){
  55. // If ui-refresh is used, re-fire the the method upon every change
  56. if (attrs.uiRefresh) {
  57. scope.$watch(attrs.uiRefresh, function() {
  58. callPlugin();
  59. });
  60. }
  61. }
  62. if ( JQ_CONFIG[attrs.uiJq] ) {
  63. uiLoad.load(JQ_CONFIG[attrs.uiJq]).then(function() {
  64. callPlugin();
  65. refresh();
  66. }).catch(function() {
  67. });
  68. } else {
  69. callPlugin();
  70. refresh();
  71. }
  72. };
  73. }
  74. };
  75. }]);