All files / web/bundles/pimdatagrid/js/datagrid/action model-action.js

28.89% Statements 13/45
19.44% Branches 7/36
75% Functions 3/4
28.89% Lines 13/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129  781x                     781x                                         44002x   44002x     44002x   44002x       44002x       44002x                   3344x           3344x 3344x         3344x           3344x                                                                                                        
/* global define */
define(['underscore', 'oro/datagrid/abstract-action'],
function(_, AbstractAction) {
    'use strict';
 
    /**
     * Basic model action class.
     *
     * @export  oro/datagrid/model-action
     * @class   oro.datagrid.ModelAction
     * @extends oro.datagrid.AbstractAction
     */
    return AbstractAction.extend({
        /** @property {Backbone.Model} */
        model: null,
 
        /** @property {String} */
        link: undefined,
 
        /** @property {Boolean} */
        backUrl: false,
 
        /** @property {String} */
        backUrlParameter: 'back',
 
        /**
         * Initialize view
         *
         * @param {Object} options
         * @param {Backbone.Model} options.model Optional parameter
         * @throws {TypeError} If model is undefined
         */
        initialize: function(options) {
            options = options || {};
 
            Iif (!options.model) {
                throw new TypeError("'model' is required");
            }
            this.model = options.model;
 
            Iif (_.has(options, 'backUrl')) {
                this.backUrl = options.backUrl;
            }
 
            Iif (_.has(options, 'backUrlParameter')) {
                this.backUrlParameter = options.backUrlParameter;
            }
 
            AbstractAction.prototype.initialize.apply(this, arguments);
        },
 
        /**
         * Get action link
         *
         * @return {String}
         * @throws {TypeError} If route is undefined
         */
        getLink: function() {
            Iif (!this.link) {
                throw new TypeError("'link' is required");
            }
 
            var result;
 
            Eif (this.model.has(this.link)) {
                result = this.model.get(this.link);
            } else {
                result = this.link;
            }
 
            Iif (this.backUrl) {
                var backUrl = _.isBoolean(this.backUrl) ? window.location.href : this.backUrl;
                backUrl = encodeURIComponent(backUrl);
                result = this.addUrlParameter(result, this.backUrlParameter, backUrl);
            }
 
            return result;
        },
 
        /**
         * Add parameter to URL
         *
         * @param {String} url
         * @param {String} parameterName
         * @param {String} parameterValue
         * @return {String}
         * @protected
         */
        addUrlParameter: function(url, parameterName, parameterValue) {
            var urlhash, sourceUrl, replaceDuplicates = true;
            if(url.indexOf('#') > 0){
                var cl = url.indexOf('#');
                urlhash = url.substring(url.indexOf('#'),url.length);
            } else {
                urlhash = '';
                cl = url.length;
            }
            sourceUrl = url.substring(0,cl);
 
            var urlParts = sourceUrl.split("?");
            var newQueryString = "";
 
            if (urlParts.length > 1) {
                var parameters = urlParts[1].split("&");
                for (var i=0; (i < parameters.length); i++)
                {
                    var parameterParts = parameters[i].split("=");
                    if (!(replaceDuplicates && parameterParts[0] == parameterName))
                    {
                        if (newQueryString == "")
                            newQueryString = "?";
                        else
                            newQueryString += "&";
                        newQueryString += parameterParts[0] + "=" + (parameterParts[1] ? parameterParts[1] : '');
                    }
                }
            }
            if (newQueryString == "") {
                newQueryString = "?";
            }
            if (newQueryString !== "" && newQueryString != '?') {
                newQueryString += "&";
            }
            newQueryString += parameterName + "=" + (parameterValue ? parameterValue : '');
            return urlParts[0] + newQueryString + urlhash;
        }
    });
});