SaveSearchView.js 8.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * 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',
21 22
    'hbs!tmpl/search/save/SaveSearchView_tmpl',
    'views/search/save/SaveSearchItemView',
23 24 25 26 27
    'collection/VSearchList',
    'utils/Utils',
    'utils/UrlLinks',
    'utils/CommonViewFunction',
    'utils/Messages'
28
], function(require, Backbone, SaveSearchViewTmpl, SaveSearchItemView, VSearchList, Utils, UrlLinks, CommonViewFunction, Messages) {
29 30 31
    'use strict';

    return Backbone.Marionette.CompositeView.extend({
32
        template: SaveSearchViewTmpl,
33 34 35 36 37 38 39 40 41 42 43 44 45 46
        childView: SaveSearchItemView,
        childViewContainer: "[data-id='itemViewContent']",
        ui: {
            saveAs: "[data-id='saveAsBtn']",
            save: "[data-id='saveBtn']"
        },
        childViewOptions: function() {
            return {
                collection: this.collection,
                typeHeaders: this.typeHeaders,
                applyValue: this.applyValue,
                isBasic: this.isBasic,
                classificationDefCollection: this.classificationDefCollection,
                entityDefCollection: this.entityDefCollection,
47 48
                fetchFavioriteCollection: this.fetchCollection.bind(this),
                searchTypeObj: this.searchTypeObj
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
            };
        },
        childEvents: function() {
            return {
                "item:clicked": function() {
                    this.ui.save.attr('disabled', false);
                }
            }
        },
        events: function() {
            var events = {};
            events['click ' + this.ui.saveAs] = "saveAs";
            events['click ' + this.ui.save] = "save";
            return events;
        },
        initialize: function(options) {
            var that = this;
            _.extend(this, _.pick(options, 'collection', 'value', 'searchVent', 'typeHeaders', 'applyValue', 'getValue', 'isBasic', 'fetchCollection', 'classificationDefCollection', 'entityDefCollection'));
67 68 69 70 71 72 73 74
            this.searchTypeObj = {
                'searchType': 'dsl',
                'dslChecked': 'true'
            }
            if (this.isBasic) {
                this.searchTypeObj.dslChecked = false;
                this.searchTypeObj.searchType = 'basic';
            }
75 76 77 78 79
        },
        onRender: function() {
            this.bindEvents();
        },
        bindEvents: function() {
80
            var that = this;
81
            this.listenTo(this.collection, "add reset error remove", function(model, collection) {
82
                this.$('.fontLoader-relative').hide();
83 84
                if (this.collection && this.collection.length) {
                    this.$(".noFavoriteSearch").hide();
85
                } else {
86
                    this.$(".noFavoriteSearch").show();
87 88
                }
            }, this);
89 90 91 92 93 94 95
            $('body').on('click', '.saveSearchPopoverList_' + (this.isBasic ? 'isBasic' : 'isAdvance') + ' li', function(e) {
                that.$('.saveSearchPopover').popover('hide');
                var id = $(this).parent('ul').data('id');
                that[$(this).find('a').data('fn')]({
                    'model': that.collection.find({ 'guid': id })
                });
            });
96 97
        },
        saveAs: function(e) {
98
            var value = this.getValue();
99
            if (value && (value.type || value.tag || value.query || value.term)) {
100 101 102 103
                this.callSaveModalLayoutView({
                    'collection': this.collection,
                    'getValue': this.getValue,
                    'isBasic': this.isBasic
104 105 106 107 108 109 110 111 112 113 114 115 116 117
                });
            } else {
                Utils.notifyInfo({
                    content: Messages.search.favoriteSearch.notSelectedSearchFilter
                })
            }
        },
        save: function() {
            var that = this,
                obj = {},
                notifyObj = {
                    modal: true,
                    html: true,
                    ok: function(argument) {
118 119 120 121 122 123
                        that.callSaveModalLayoutView({
                            'saveObj': obj,
                            'collection': that.collection,
                            'getValue': that.getValue,
                            'isBasic': that.isBasic
                        })
124 125 126 127 128
                    },
                    cancel: function(argument) {}
                },
                selectedEl = this.$('.saveSearchList li.active').find('div.item');
            obj.name = selectedEl.find('a').text();
129
            obj.guid = selectedEl.data('id');
130
            if (selectedEl && selectedEl.length) {
131
                notifyObj['text'] = Messages.search.favoriteSearch.save + " <b>" + _.escape(obj.name) + "</b> ?";
132 133 134
                Utils.notifyConfirm(notifyObj);
            } else {
                Utils.notifyInfo({
135
                    content: Messages.search.favoriteSearch.notSelectedFavoriteElement
136 137 138
                })
            }
        },
139 140 141 142 143 144
        callSaveModalLayoutView: function(options) {
            require([
                'views/search/save/SaveModalLayoutView'
            ], function(SaveModalLayoutView) {
                new SaveModalLayoutView(options);
            });
145 146 147 148 149
        },
        onSearch: function(options) {
            if (options && options.model) {
                var searchParameters = options.model.toJSON().searchParameters,
                    params = CommonViewFunction.generateUrlFromSaveSearchObject({
150
                        value: {"searchParameters" : searchParameters, "uiParameters": options.model.get('uiParameters')},
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
                        classificationDefCollection: this.classificationDefCollection,
                        entityDefCollection: this.entityDefCollection
                    });
                Utils.setUrl({
                    url: '#!/search/searchResult',
                    urlParams: _.extend({}, this.searchTypeObj, params),
                    mergeBrowserUrl: false,
                    trigger: true,
                    updateTabState: true
                });
            }
        },
        onRename: function(options) {
            if (options && options.model) {
                var that = this;
                require([
                    'views/search/save/SaveModalLayoutView'
                ], function(SaveModalLayoutView) {
                    new SaveModalLayoutView({ 'selectedModel': options.model, 'collection': that.collection, 'getValue': that.getValue, 'isBasic': that.isBasic });
                });
            }
        },
        onDelete: function(options) {
            if (options && options.model) {
                var that = this;
                var notifyObj = {
                    modal: true,
                    html: true,
179
                    text: Messages.conformation.deleteMessage + "<b>" + _.escape(options.model.get('name')) + "</b>" + " ?",
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
                    ok: function(argument) {
                        that.onDeleteNotifyOk(options);
                    },
                    cancel: function(argument) {}
                }
                Utils.notifyConfirm(notifyObj);
            }
        },
        onDeleteNotifyOk: function(options) {
            var that = this;
            options.model.urlRoot = UrlLinks.saveSearchApiUrl();
            options.model.destroy({
                wait: true,
                success: function(model, data) {
                    if (that.collection) {
195
                        that.collection.remove(model);
196 197 198 199 200 201
                    }
                    Utils.notifySuccess({
                        content: options.model.get('name') + Messages.deleteSuccessMessage
                    });
                }
            });
202 203 204
        }
    });
});