Skip to main content
added 66 characters in body
Source Link
Andrei
  • 2.3k
  • 1
  • 13
  • 17
// js/myModule.js

(function(angular, $, _){
  //  Your module
  angular.module('myModule', []);

  // the hack
  angular.module('crmMailing').config(function($provide){
  
    // crmMailingBlockTracking directive
    $provide.decorator('crmMailingBlockTrackingDirective', function($delegate){
        var directive = $delegate[0];
        
        // Original link function
        var link = directive.link;
        
        // compile function, returns new link function
        directive.compile = function(){
            return function(scope, element, attrs){
                // get old link functionality
                link.apply(this, arguments);
                
                // new functionality here
                scope.$parent.$watch(attrs.crmMailing, function(){
                    var createdDate = new Date(scope.mailing.created_date).getTime()/1000;
                    var nowDate = new Date().getTime()/1000;
                    
                    if((nowDate - createdDate) < 60){
                        scope.mailing.url_tracking = '0';
                        CRM.alert('Tracking Disabled');
                    }
                    
                }); 
            };
        };
        
        return $delegate;
    });
  });
})(angular, CRM.$, CRM._);
// js/myModule.js

//  Your module
angular.module('myModule', []);

// the hack
  angular.module('crmMailing').config(function($provide){
  
    // crmMailingBlockTracking directive
    $provide.decorator('crmMailingBlockTrackingDirective', function($delegate){
        var directive = $delegate[0];
        
        // Original link function
        var link = directive.link;
        
        // compile function, returns new link function
        directive.compile = function(){
            return function(scope, element, attrs){
                // get old link functionality
                link.apply(this, arguments);
                
                // new functionality here
                scope.$parent.$watch(attrs.crmMailing, function(){
                    var createdDate = new Date(scope.mailing.created_date).getTime()/1000;
                    var nowDate = new Date().getTime()/1000;
                    
                    if((nowDate - createdDate) < 60){
                        scope.mailing.url_tracking = '0';
                        CRM.alert('Tracking Disabled');
                    }
                    
                }); 
            };
        };
        
        return $delegate;
    });
  });
// js/myModule.js

(function(angular, $, _){
  // Your module
  angular.module('myModule', []);

  // the hack
  angular.module('crmMailing').config(function($provide){
  
    // crmMailingBlockTracking directive
    $provide.decorator('crmMailingBlockTrackingDirective', function($delegate){
        var directive = $delegate[0];
        
        // Original link function
        var link = directive.link;
        
        // compile function, returns new link function
        directive.compile = function(){
            return function(scope, element, attrs){
                // get old link functionality
                link.apply(this, arguments);
                
                // new functionality here
                scope.$parent.$watch(attrs.crmMailing, function(){
                    var createdDate = new Date(scope.mailing.created_date).getTime()/1000;
                    var nowDate = new Date().getTime()/1000;
                    
                    if((nowDate - createdDate) < 60){
                        scope.mailing.url_tracking = '0';
                        CRM.alert('Tracking Disabled');
                    }
                    
                }); 
            };
        };
        
        return $delegate;
    });
  });
})(angular, CRM.$, CRM._);
Source Link
Andrei
  • 2.3k
  • 1
  • 13
  • 17

Disclaimer: I'm not an angularjs expert

Again this is a bit of a hack, we are intercepting a built-in CiviCRM angular directive and extending to add behaviour.

Create a custom extensions or use an existing one and implement hook_civicrm_angularModules:

function myextension_civicrm_angularModules(&$angularModules){
        $angularModules['myModule'] = array(
                'ext' => 'myextension',
                'js' => array('js/myModule.js'),
        );
}

In your js file:

// js/myModule.js

//  Your module
angular.module('myModule', []);

// the hack
  angular.module('crmMailing').config(function($provide){
  
    // crmMailingBlockTracking directive
    $provide.decorator('crmMailingBlockTrackingDirective', function($delegate){
        var directive = $delegate[0];
        
        // Original link function
        var link = directive.link;
        
        // compile function, returns new link function
        directive.compile = function(){
            return function(scope, element, attrs){
                // get old link functionality
                link.apply(this, arguments);
                
                // new functionality here
                scope.$parent.$watch(attrs.crmMailing, function(){
                    var createdDate = new Date(scope.mailing.created_date).getTime()/1000;
                    var nowDate = new Date().getTime()/1000;
                    
                    if((nowDate - createdDate) < 60){
                        scope.mailing.url_tracking = '0';
                        CRM.alert('Tracking Disabled');
                    }
                    
                }); 
            };
        };
        
        return $delegate;
    });
  });

The angular Mailing app uses a kind of boilerplate service, crmMailingSimpleDirective for constructing directives see here more info

The above snippet will do the same as my other answer without relying on setTimeout().

Hope that helps.