BusinessCatalogDetailLayoutView.js 7.23 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
/**
 * 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/business_catalog/BusinessCatalogDetailLayoutView_tmpl',
    'utils/Utils',
    'models/VEntity',
24 25
    'models/VCatalog',
    'utils/Messages'
26
], function(require, Backbone, BusinessCatalogDetailLayoutViewTmpl, Utils, VEntity, VCatalog, Messages) {
27 28 29 30 31 32 33 34 35 36
    'use strict';

    var BusinessCatalogDetailLayoutView = Backbone.Marionette.LayoutView.extend(
        /** @lends BusinessCatalogDetailLayoutView */
        {
            _viewName: 'BusinessCatalogDetailLayoutView',

            template: BusinessCatalogDetailLayoutViewTmpl,

            /** Layout sub regions */
37
            regions: {},
38 39 40 41 42 43
            /** ui selector cache */
            ui: {
                title: '[data-id="title"]',
                editButton: '[data-id="editButton"]',
                description: '[data-id="description"]',
                editBox: '[data-id="editBox"]',
44
                createDate: '[data-id="createDate"]'
45 46 47 48
            },
            /** ui events hash */
            events: function() {
                var events = {};
49
                events["click " + this.ui.editButton] = 'onEditButton';
50 51 52 53 54 55 56 57
                events["click " + this.ui.cancelButton] = 'onCancelButtonClick';
                return events;
            },
            /**
             * intialize a new BusinessCatalogDetailLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
58
                _.extend(this, _.pick(options, 'url', 'collection'));
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
                this.bindEvents();
            },
            bindEvents: function() {
                var that = this;
                this.listenTo(this.collection, 'error', function(model, response) {
                    this.$('.fontLoader').hide();
                    if (response && response.responseJSON && response.responseJSON.message) {
                        Utils.notifyError({
                            content: response.responseJSON.message
                        });
                    }

                }, this);
                this.listenTo(this.collection, 'reset', function() {
                    this.$('.fontLoader').hide();
                    this.$('.hide').removeClass('hide');
                    this.model = this.collection.first();
                    var name = this.model.get('name'),
                        description = this.model.get('description'),
                        createdDate = this.model.get('creation_time');
                    if (name) {
                        this.ui.title.show();
81
                        this.termName = Utils.checkTagOrTerm(name, true);
82
                        this.ui.title.html('<span>' + this.termName.name + '</span>');
83 84 85 86 87
                    } else {
                        this.ui.title.hide();
                    }
                    if (description) {
                        this.ui.description.show();
88
                        this.ui.description.html('<span>' + _.escape(description) + '</span>');
89 90 91 92
                    } else {
                        this.ui.description.hide();
                    }
                    if (createdDate) {
93 94
                        var splitDate = createdDate.split(":");
                        this.ui.createDate.html('<strong> Date Created: </strong> ' + splitDate[0] + " " + splitDate[1] + ":" + splitDate[2] + ":" + splitDate[3] + " (GMT)");
95
                    }
96
                    Utils.hideTitleLoader(this.$('.fontLoader'), this.$('.catlogDetail'));
97 98 99 100
                }, this);
            },
            onRender: function() {
                var that = this;
101
                Utils.showTitleLoader(this.$('.page-title .fontLoader'), this.$('.catlogDetail'));
102 103 104 105
            },
            fetchCollection: function() {
                this.collection.fetch({ reset: true });
            },
106 107 108 109 110 111 112
            onEditButton: function(e) {
                var that = this;
                $(e.currentTarget).blur();
                require([
                    'views/tag/CreateTagLayoutView',
                    'modules/Modal'
                ], function(CreateTagLayoutView, Modal) {
113
                    var view = new CreateTagLayoutView({ 'termCollection': that.collection, 'descriptionData': that.model.get('description'), 'tag': _.unescape(that.termName.name) });
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                    var modal = new Modal({
                        title: 'Edit Term',
                        content: view,
                        cancelText: "Cancel",
                        okText: 'Save',
                        allowCancel: true,
                    }).open();
                    view.ui.description.on('keyup', function(e) {
                        that.textAreaChangeEvent(view, modal);
                    });
                    modal.$el.find('button.ok').prop('disabled', true);
                    modal.on('ok', function() {
                        that.onSaveDescriptionClick(view);
                    });
                    modal.on('closeModal', function() {
                        modal.trigger('cancel');
                    });
                });
            },
            textAreaChangeEvent: function(view, modal) {
                if (view.description == view.ui.description.val()) {
                    modal.$el.find('button.ok').prop('disabled', true);
                } else {
                    modal.$el.find('button.ok').prop('disabled', false);
                }
            },
            onSaveDescriptionClick: function(view) {
                view.description = view.ui.description.val();
142
                this.onSaveButton(this.collection.first().toJSON(), Messages.tag.updateTermDescriptionMessage, view);
143
                this.ui.description.show();
144
            },
145 146 147 148 149 150
            onSaveButton: function(saveObject, message, view) {
                var that = this,
                    termModel = new VCatalog();
                termModel.url = function() {
                    return that.collection.url;
                };
151
                Utils.showTitleLoader(this.$('.page-title .fontLoader'), this.$('.catlogDetail'));
152 153 154 155 156 157 158 159 160 161 162
                termModel.set({
                    "description": view.ui.description.val()
                }).save(null, {
                    type: "PUT",
                    success: function(model, response) {
                        that.collection.fetch({ reset: true });
                        Utils.notifySuccess({
                            content: message
                        });
                    }
                });
163 164 165 166 167
            }
        });
    return BusinessCatalogDetailLayoutView;

});