SaveModalLayoutView.js 8.92 KB
Newer Older
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
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

define(['require',
    'backbone',
    'hbs!tmpl/search/save/SaveModalLayoutView_tmpl',
    'utils/Utils',
    'modules/Modal',
    'utils/UrlLinks',
    'platform',
    'models/VSearch',
27
    "collection/VSearchList",
28 29
    'utils/CommonViewFunction',
    'utils/Messages'
30
], function(require, Backbone, SaveModalLayoutViewTmpl, Utils, Modal, UrlLinks, platform, VSearch, VSearchList, CommonViewFunction, Messages) {
31 32 33 34 35 36 37 38 39 40 41


    var SaveModalLayoutView = Backbone.Marionette.LayoutView.extend({
        _viewName: 'SaveModalLayoutView',
        template: SaveModalLayoutViewTmpl,
        regions: {},
        ui: {
            saveAsName: "[data-id='saveAsName']"
        },
        templateHelpers: function() {
            return {
42 43
                selectedModel: this.selectedModel ? this.selectedModel.toJSON() : null,
                rename: this.rename
44 45 46 47 48 49 50 51
            };
        },
        events: function() {
            var events = {};
            return events;
        },
        initialize: function(options) {
            var that = this;
52
            _.extend(this, _.pick(options, 'rename', 'selectedModel', 'collection', 'getValue', 'isBasic', 'saveObj'));
53
            this.model = new VSearch();
54 55 56 57 58 59 60 61 62 63 64
            this.saveSearchCollection = new VSearchList();
            this.saveSearchCollection.url = UrlLinks.saveSearchApiUrl();
            this.saveSearchCollection.fullCollection.comparator = function(model) {
                return getModelName(model);
            }

            function getModelName(model) {
                if (model.get('name')) {
                    return model.get('name').toLowerCase();
                }
            };
65 66 67
            if (this.saveObj) {
                this.onCreateButton();
            } else {
68
                this.modal = modal = new Modal({
69
                    titleHtml: true,
70
                    title: '<span>' + (this.selectedModel && this.rename ? 'Rename' : 'Save') + (this.isBasic ? " Basic" : " Advanced") + ' Custom Filter</span>',
71 72 73 74 75
                    content: this,
                    cancelText: "Cancel",
                    okCloses: false,
                    okText: this.selectedModel ? 'Update' : 'Save',
                    allowCancel: true
76 77
                });
                this.modal.open();
78 79 80 81 82 83 84 85 86 87 88
                modal.$el.find('button.ok').attr("disabled", "true");
                this.ui.saveAsName.on('keyup', function(e) {
                    modal.$el.find('button.ok').removeAttr("disabled");
                });
                this.ui.saveAsName.on('keyup', function(e) {
                    if ((e.keyCode == 8 || e.keyCode == 32 || e.keyCode == 46) && e.currentTarget.value.trim() == "") {
                        modal.$el.find('button.ok').attr("disabled", "true");
                    }
                });
                modal.on('ok', function() {
                    modal.$el.find('button.ok').attr("disabled", "true");
89
                    that.onCreateButton();
90 91 92 93 94 95
                });
                modal.on('closeModal', function() {
                    modal.trigger('cancel');
                });
            }
        },
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        hideLoader: function() {
            this.$el.find("form").removeClass("hide");
            this.$el.find(".fontLoader").removeClass("show");
        },
        onRender: function() {
            if (this.rename == true) {
                this.hideLoader();
            } else {
                var that = this;
                this.saveSearchCollection.fetch({
                    success: function(collection, data) {
                        that.saveSearchCollection.fullCollection.reset(_.where(data, { searchType: that.isBasic ? "BASIC" : "ADVANCED" }));
                        var options = "";
                        that.saveSearchCollection.fullCollection.each(function(model) {
                            options += '<option value="' + model.get("name") + '">' + model.get("name") + '</option>';
                        })
                        that.ui.saveAsName.append(options);
                        that.ui.saveAsName.val("");
                        that.ui.saveAsName.select2({
115
                            placeholder: "Enter filter name ",
116 117 118 119 120 121 122 123
                            allowClear: false,
                            tags: true,
                            multiple: false,
                            templateResult: function(state) {
                                if (!state.id) {
                                    return state.text;
                                }
                                if (!state.element) {
124
                                    return $("<span><span class='option-title-light'>New:</span> <strong>" + _.escape(state.text) + "</strong></span>");
125
                                } else {
126
                                    return $("<span><span class='option-title-light'>Update:</span> <strong>" + _.escape(state.text) + "</strong></span>");
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                                }
                            }
                        }).on("change", function() {
                            var val = that.ui.saveAsName.val();
                            if (val.length) {
                                that.selectedModel = that.saveSearchCollection.fullCollection.find({ name: val });
                                if (that.selectedModel) {
                                    that.modal.$el.find('button.ok').text("Save As");
                                } else {
                                    that.modal.$el.find('button.ok').text("Save");
                                }
                                that.modal.$el.find('button.ok').removeAttr("disabled");
                            } else {
                                that.modal.$el.find('button.ok').attr("disabled", "true");
                                that.selectedModel = null;
                            }
                        });
                    },
                    silent: true
                });
                this.hideLoader();
            }
        },
        onCreateButton: function() {
151
            var that = this,
152 153 154 155 156 157
                obj = { name: this.ui.saveAsName.val() || null, value: this.getValue() };
            if (this.saveObj) {
                // Save search Filter
                _.extend(obj, this.saveObj);
            }
            var saveObj = CommonViewFunction.generateObjectForSaveSearchApi(obj);
158 159
            if (this.selectedModel) {
                // Update Name only.
160 161 162
                var selectedModel = this.selectedModel.toJSON();
                if (this.rename !== true) {
                    _.extend(selectedModel.searchParameters, saveObj.searchParameters);
163
                }
164 165 166
                selectedModel.name = obj.name;
                saveObj = selectedModel;
            } else {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                if (this.isBasic) {
                    saveObj['searchType'] = "BASIC";
                } else {
                    saveObj['searchType'] = "ADVANCED";
                }
            }
            this.model.urlRoot = UrlLinks.saveSearchApiUrl();
            this.model.save(saveObj, {
                type: (saveObj.guid ? 'PUT' : 'POST'),
                success: function(model, data) {
                    if (that.collection) {
                        if (saveObj.guid) {
                            var collectionRef = that.collection.find({ guid: data.guid });
                            if (collectionRef) {
                                collectionRef.set(data);
                            }
                            Utils.notifySuccess({
184
                                content: obj.name + Messages.getAbbreviationMsg(false, 'editSuccessMessage')
185 186 187 188
                            });
                        } else {
                            that.collection.add(data);
                            Utils.notifySuccess({
189
                                content: obj.name + Messages.getAbbreviationMsg(false, 'addSuccessMessage')
190 191 192 193 194 195 196 197 198
                            });
                        }
                    }
                    if (that.callback) {
                        that.callback();
                    }

                }
            });
199 200
            if (this.modal) {
                this.modal.trigger('cancel');
201 202 203 204
            }
        }
    });
    return SaveModalLayoutView;
205
});