Where is the code behind <sp-attachment-button>?

yundlu316
Kilo Guru

We want to edit the way the ootb <sp-attachment-button> looks, where is this code located?  Is there a way to change some styling for that button?

1 ACCEPTED SOLUTION

Oleg
Mega Sage

The code of <sp-attachment-button> is included in https://<yourinstance>.service-now.com/scripts/js_includes_sp.jsx or https://<yourinstance>.service-now.com/scripts/app.$sp/directive.spAttachmentButton.js. It's below:

/*! RESOURCE: /scripts/app.$sp/directive.spAttachmentButton.js */
angular.module('sn.$sp').directive('spAttachmentButton', function(cabrillo, $rootScope, i18n) {
    'use strict';
    return {
        restrict: 'E',
        template: function() {
            var inputTemplate;
            if (cabrillo.isNative()) {
                inputTemplate = '<button href="#" title="" ng-click="showAttachOptions()" class="panel-button sp-attachment-add btn btn-link" aria-label=""><span class="glyphicon glyphicon-camera"></span></button>';
            } else {
                inputTemplate = '<input type="file" style="display: none" multiple="true" ng-file-select="attachmentHandler.onFileSelect($files)" class="sp-attachments-input"/>';
                inputTemplate += '<button title="" ng-click="attachmentHandler.openSelector($event)" class="panel-button sp-attachment-add btn btn-link" aria-label=""><span class="glyphicon glyphicon-paperclip"></span></button>';
            }
            return ['<span class="file-upload-input">', inputTemplate, '</span>'].join('');
        },
        controller: function($element, $scope) {
            $scope.showAttachOptions = function() {
                var handler = $scope.attachmentHandler;
                cabrillo.attachments.addFile(handler.tableName, handler.tableId, null, {
                    maxWidth: 1000,
                    maxHeight: 1000
                }).then(function(data) {
                    handler.getAttachmentList();
                    $rootScope.$broadcast("added_attachment");
                }, function() {
                    console.log('Failed to attach new file');
                });
            }
            ;
            $scope.$on('attachment_select_files', function(e) {
                $scope.$evalAsync(function() {
                    $($element).find('.sp-attachments-input').click();
                });
            });
        },
        link: function(scope, el, attr) {
            i18n.getMessage("Add attachment", function(msg) {
                el.find("button").attr("title", msg);
                el.find("button").attr("aria-label", msg);
            });
        }
    }
});

If you need to change the code you can modify it and save it as new directive under another name.

View solution in original post

14 REPLIES 14

Hi Yun,

Do you solve that problem, I am appreciate it if you share your solution

 

It's very easy. I suppose that you never created your directive before. You need just type "angular" in menu to see the module "Angular Providers". On click on it you will see many directives, services and factories. You need just click "New" button, choose "Directive" as type (because angular.module('sn.$sp').directive(...) was originally). You place spFileAttachmentButton as Name and finally copy only the code starting with function(cabrillo, $rootScope, i18n) { and till the corresponding }. You can add any name for the anonymous function to prevent warning of code editor:

find_real_file.png

after saving the code you need open the widget where you use it. You should click on "Edit" button of related list "Anguler Providers" of your widget and add new directive spFileAttachmentButton to the list. After that you can use <sp-file-attachment-button> instead of <sp-attachment-button>.

got it!  I deleted angular.module('sn.$sp').directive(...) and now it works fine, thanks!!!

Thankyou for this oleg, this helped me alot!

Hello;

 

Can we change table in this provider, as i don't want attachment to be stored in sys_attachment table i want to store attachment to custom table.

 

Please help me on this 

 

Thanks

Pramod Pandey