fields.js 26.5 KB
Newer Older
1
(function(define, undefined) {
Usman Khalid committed
2 3
    'use strict';
    define([
4
        'gettext', 'jquery', 'underscore', 'backbone',
5
        'edx-ui-toolkit/js/utils/html-utils',
6
        'edx-ui-toolkit/js/utils/date-utils',
muhammad-ammar committed
7 8 9 10 11
        'text!templates/fields/field_readonly.underscore',
        'text!templates/fields/field_dropdown.underscore',
        'text!templates/fields/field_link.underscore',
        'text!templates/fields/field_text.underscore',
        'text!templates/fields/field_textarea.underscore',
12
        'backbone-super'
13
    ], function(gettext, $, _, Backbone, HtmlUtils, DateUtils,
muhammad-ammar committed
14 15 16 17
                 field_readonly_template,
                 field_dropdown_template,
                 field_link_template,
                 field_text_template,
18
                 field_textarea_template
muhammad-ammar committed
19
    ) {
20
        var messageRevertDelay = 6000;
Usman Khalid committed
21 22 23
        var FieldViews = {};

        FieldViews.FieldView = Backbone.View.extend({
24

Usman Khalid committed
25 26
            fieldType: 'generic',

27
            className: function() {
Usman Khalid committed
28 29 30 31 32 33
                return 'u-field' + ' u-field-' + this.fieldType + ' u-field-' + this.options.valueAttribute;
            },

            tagName: 'div',

            indicators: {
34
                'canEdit': HtmlUtils.joinHtml(
35
                    HtmlUtils.HTML('<span class="icon fa fa-pencil message-can-edit" aria-hidden="true"></span><span class="sr">'),  // eslint-disable-line max-len
36
                    gettext('Editable'),
37 38 39
                    HtmlUtils.HTML('</span>')
                ),
                'error': HtmlUtils.joinHtml(
40
                    HtmlUtils.HTML('<span class="fa fa-exclamation-triangle message-error" aria-hidden="true"></span><span class="sr">'),  // eslint-disable-line max-len
41
                    gettext('Error'),
42 43 44
                    HtmlUtils.HTML('</span>')
                ),
                'validationError': HtmlUtils.joinHtml(
45
                    HtmlUtils.HTML('<span class="fa fa-exclamation-triangle message-validation-error" aria-hidden="true"></span><span class="sr">'),  // eslint-disable-line max-len
46
                    gettext('Validation Error'),
47 48 49
                    HtmlUtils.HTML('</span>')
                ),
                'inProgress': HtmlUtils.joinHtml(
50
                    HtmlUtils.HTML('<span class="fa fa-spinner fa-pulse message-in-progress" aria-hidden="true"></span><span class="sr">'),  // eslint-disable-line max-len
51
                    gettext('In Progress'),
52 53 54
                    HtmlUtils.HTML('</span>')
                ),
                'success': HtmlUtils.joinHtml(
55
                    HtmlUtils.HTML('<span class="fa fa-check message-success" aria-hidden="true"></span><span class="sr">'),  // eslint-disable-line max-len
56
                    gettext('Success'),
57 58 59 60
                    HtmlUtils.HTML('</span>')
                ),
                'plus': HtmlUtils.joinHtml(
                    HtmlUtils.HTML('<span class="fa fa-plus placeholder" aria-hidden="true"></span><span class="sr">'),
61
                    gettext('Placeholder'),
62
                    HtmlUtils.HTML('</span>')
63
                )
Usman Khalid committed
64 65 66
            },

            messages: {
muzaffaryousaf committed
67
                'canEdit': '',
Usman Khalid committed
68 69 70 71 72 73
                'error': gettext('An error occurred. Please try again.'),
                'validationError': '',
                'inProgress': gettext('Saving'),
                'success': gettext('Your changes have been saved.')
            },

74 75 76 77 78
            constructor: function(options) {
                this.options = _.extend({}, options);
                Backbone.View.apply(this, arguments);
            },

79
            initialize: function() {
muhammad-ammar committed
80
                this.template = _.template(this.fieldTemplate || '');
Usman Khalid committed
81 82 83 84

                this.helpMessage = this.options.helpMessage || '';
                this.showMessages = _.isUndefined(this.options.showMessages) ? true : this.options.showMessages;

85
                _.bindAll(this, 'modelValue', 'modelValueIsSet', 'showNotificationMessage', 'getNotificationMessage',
86 87 88
                    'getMessage', 'title', 'showHelpMessage', 'showInProgressMessage', 'showSuccessMessage',
                    'showErrorMessage'
                );
Usman Khalid committed
89 90
            },

91
            modelValue: function() {
Usman Khalid committed
92 93 94
                return this.model.get(this.options.valueAttribute);
            },

muzaffaryousaf committed
95
            modelValueIsSet: function() {
96
                return (this.modelValue() === true);
Usman Khalid committed
97 98
            },

99
            title: function(title) {
100
                return HtmlUtils.setHtml(this.$('.u-field-title'), title);
muzaffaryousaf committed
101 102
            },

Usman Khalid committed
103 104 105 106
            getMessage: function(message_status) {
                if ((message_status + 'Message') in this) {
                    return this[message_status + 'Message'].call(this);
                } else if (this.showMessages) {
107
                    return HtmlUtils.joinHtml(this.indicators[message_status], this.messages[message_status]);
Usman Khalid committed
108 109 110 111
                }
                return this.indicators[message_status];
            },

112
            showHelpMessage: function(message) {
113 114 115 116
                if (_.isUndefined(message) || _.isNull(message)) {
                    message = this.helpMessage;
                }
                this.$('.u-field-message-notification').html('');
117
                HtmlUtils.setHtml(this.$('.u-field-message-help'), message);
118 119 120
            },

            getNotificationMessage: function() {
121
                return HtmlUtils.HTML(this.$('.u-field-message-notification').html());
122 123 124 125
            },

            showNotificationMessage: function(message) {
                this.$('.u-field-message-help').html('');
126
                HtmlUtils.setHtml(this.$('.u-field-message-notification'), message);
127 128
            },

muzaffaryousaf committed
129 130
            showCanEditMessage: function(show) {
                if (!_.isUndefined(show) && show) {
131
                    this.showNotificationMessage(this.getMessage('canEdit'));
muzaffaryousaf committed
132
                } else {
133
                    this.showNotificationMessage('');
muzaffaryousaf committed
134 135 136
                }
            },

137
            showInProgressMessage: function() {
138
                this.showNotificationMessage(this.getMessage('inProgress'));
Usman Khalid committed
139 140
            },

141
            showSuccessMessage: function() {
Usman Khalid committed
142
                var successMessage = this.getMessage('success');
143
                this.showNotificationMessage(successMessage);
Usman Khalid committed
144 145

                if (this.options.refreshPageOnSave) {
146
                    location.reload(true);
Usman Khalid committed
147 148 149 150 151 152 153
                }

                var view = this;

                var context = Date.now();
                this.lastSuccessMessageContext = context;

154
                setTimeout(function() {
155 156
                    if ((context === view.lastSuccessMessageContext) &&
                        (view.getNotificationMessage().toString() === successMessage.toString())) {
157 158 159 160 161
                        if (view.editable === 'toggle') {
                            view.showCanEditMessage(true);
                        } else {
                            view.showHelpMessage();
                        }
Usman Khalid committed
162 163 164 165
                    }
                }, messageRevertDelay);
            },

166
            showErrorMessage: function(xhr) {
Usman Khalid committed
167 168
                if (xhr.status === 400) {
                    try {
169
                        var errors = JSON.parse(xhr.responseText),
170 171
                            validationErrorMessage = errors.field_errors[this.options.valueAttribute].user_message,
                            message = HtmlUtils.joinHtml(this.indicators.validationError, validationErrorMessage);
172
                        this.showNotificationMessage(message);
Usman Khalid committed
173
                    } catch (error) {
174
                        this.showNotificationMessage(this.getMessage('error'));
Usman Khalid committed
175 176
                    }
                } else {
177
                    this.showNotificationMessage(this.getMessage('error'));
Usman Khalid committed
178 179 180 181
                }
            }
        });

muzaffaryousaf committed
182 183
        FieldViews.EditableFieldView = FieldViews.FieldView.extend({

184
            initialize: function(options) {
muhammad-ammar committed
185
                this.persistChanges = _.isUndefined(options.persistChanges) ? false : options.persistChanges;
186 187 188
                _.bindAll(this, 'saveAttributes', 'saveSucceeded', 'showDisplayMode', 'showEditMode',
                    'startEditing', 'finishEditing'
                );
muzaffaryousaf committed
189 190
                this._super(options);

191
                this.editable = _.isUndefined(this.options.editable) ? 'always' : this.options.editable;
muzaffaryousaf committed
192 193 194 195 196 197 198 199 200
                this.$el.addClass('editable-' + this.editable);

                if (this.editable === 'always') {
                    this.showEditMode(false);
                } else {
                    this.showDisplayMode(false);
                }
            },

201
            saveAttributes: function(attributes, options) {
muhammad-ammar committed
202 203 204 205 206 207
                if (this.persistChanges === true) {
                    var view = this;
                    var defaultOptions = {
                        contentType: 'application/merge-patch+json',
                        patch: true,
                        wait: true,
208
                        success: function() {
muhammad-ammar committed
209 210
                            view.saveSucceeded();
                        },
211
                        error: function(model, xhr) {
muhammad-ammar committed
212 213 214 215 216 217
                            view.showErrorMessage(xhr);
                        }
                    };
                    this.showInProgressMessage();
                    this.model.save(attributes, _.extend(defaultOptions, options));
                }
muzaffaryousaf committed
218 219
            },

220
            saveSucceeded: function() {
muzaffaryousaf committed
221 222 223
                this.showSuccessMessage();
            },

224
            updateDisplayModeClass: function() {
muzaffaryousaf committed
225 226 227 228 229 230 231
                this.$el.removeClass('mode-edit');

                this.$el.toggleClass('mode-hidden', (this.editable === 'never' && !this.modelValueIsSet()));
                this.$el.toggleClass('mode-placeholder', (this.editable === 'toggle' && !this.modelValueIsSet()));
                this.$el.toggleClass('mode-display', (this.modelValueIsSet()));
            },

232 233 234 235 236 237
            showDisplayMode: function(render) {
                this.mode = 'display';
                if (render) { this.render(); }
                this.updateDisplayModeClass();
            },

muzaffaryousaf committed
238 239 240 241 242 243 244 245 246 247 248
            showEditMode: function(render) {
                this.mode = 'edit';
                if (render) { this.render(); }

                this.$el.removeClass('mode-hidden');
                this.$el.removeClass('mode-placeholder');
                this.$el.removeClass('mode-display');

                this.$el.addClass('mode-edit');
            },

249
            startEditing: function() {
muzaffaryousaf committed
250 251 252 253 254
                if (this.editable === 'toggle' && this.mode !== 'edit') {
                    this.showEditMode(true);
                }
            },

255
            finishEditing: function() {
256
                var modelValue;
257
                if (this.persistChanges === false || this.mode !== 'edit') { return; }
258 259 260 261 262 263 264

                modelValue = this.modelValue();
                if (!(_.isUndefined(modelValue) || _.isNull(modelValue))) {
                    modelValue = modelValue.toString();
                }

                if (this.fieldValue() !== modelValue) {
muzaffaryousaf committed
265 266 267 268 269 270 271 272
                    this.saveValue();
                } else {
                    if (this.editable === 'always') {
                        this.showEditMode(true);
                    } else {
                        this.showDisplayMode(true);
                    }
                }
muhammad-ammar committed
273 274
            },

275
            highlightFieldOnError: function() {
muhammad-ammar committed
276 277 278
                this.$el.addClass('error');
            },

279
            unhighlightField: function() {
muhammad-ammar committed
280
                this.$el.removeClass('error');
muzaffaryousaf committed
281 282 283
            }
        });

Usman Khalid committed
284 285 286 287
        FieldViews.ReadonlyFieldView = FieldViews.FieldView.extend({

            fieldType: 'readonly',

muhammad-ammar committed
288
            fieldTemplate: field_readonly_template,
Usman Khalid committed
289

290
            initialize: function(options) {
Usman Khalid committed
291 292
                this._super(options);
                _.bindAll(this, 'render', 'fieldValue', 'updateValueInField');
293
                this.listenTo(this.model, 'change:' + this.options.valueAttribute, this.updateValueInField);
Usman Khalid committed
294 295
            },

296
            render: function() {
297
                HtmlUtils.setHtml(this.$el, HtmlUtils.template(this.fieldTemplate)({
Usman Khalid committed
298 299
                    id: this.options.valueAttribute,
                    title: this.options.title,
300
                    screenReaderTitle: this.options.screenReaderTitle || this.options.title,
Usman Khalid committed
301 302 303
                    value: this.modelValue(),
                    message: this.helpMessage
                }));
304
                this.delegateEvents();
Usman Khalid committed
305 306 307
                return this;
            },

308
            fieldValue: function() {
309
                return this.$('.u-field-value').text();
Usman Khalid committed
310 311
            },

312
            updateValueInField: function() {
313
                this.$('.u-field-value ').text(this.modelValue());
Usman Khalid committed
314 315 316
            }
        });

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
        FieldViews.DateFieldView = FieldViews.ReadonlyFieldView.extend({

            fieldType: 'date',

            timezoneFormattedDate: function() {
                var context;
                context = {
                    datetime: new Date(this.modelValue()),
                    language: this.options.userLanguage,
                    timezone: this.options.userTimezone,
                    format: this.options.dateFormat
                };
                return DateUtils.localize(context);
            },

            render: function() {
                HtmlUtils.setHtml(this.$el, HtmlUtils.template(this.fieldTemplate)({
                    id: this.options.valueAttribute,
                    title: this.options.title,
                    screenReaderTitle: this.options.screenReaderTitle || this.options.title,
                    value: this.timezoneFormattedDate(),
                    message: this.helpMessage
                }));
                this.delegateEvents();
                return this;
            },

            updateValueInField: function() {
                this.$('.u-field-value ').text(this.timezoneFormattedDate());
            }
        });

muzaffaryousaf committed
349
        FieldViews.TextFieldView = FieldViews.EditableFieldView.extend({
Usman Khalid committed
350 351 352

            fieldType: 'text',

muhammad-ammar committed
353
            fieldTemplate: field_text_template,
Usman Khalid committed
354 355 356 357 358

            events: {
                'change input': 'saveValue'
            },

359
            initialize: function(options) {
Usman Khalid committed
360 361
                this._super(options);
                _.bindAll(this, 'render', 'fieldValue', 'updateValueInField', 'saveValue');
362
                this.listenTo(this.model, 'change:' + this.options.valueAttribute, this.updateValueInField);
Usman Khalid committed
363 364
            },

365
            render: function() {
366
                HtmlUtils.setHtml(this.$el, HtmlUtils.template(this.fieldTemplate)({
Usman Khalid committed
367 368 369
                    id: this.options.valueAttribute,
                    title: this.options.title,
                    value: this.modelValue(),
370 371
                    message: this.helpMessage,
                    placeholder: this.options.placeholder || ''
Usman Khalid committed
372
                }));
373
                this.delegateEvents();
Usman Khalid committed
374 375 376
                return this;
            },

377
            fieldValue: function() {
Usman Khalid committed
378 379 380
                return this.$('.u-field-value input').val();
            },

381
            updateValueInField: function() {
Usman Khalid committed
382
                var value = (_.isUndefined(this.modelValue()) || _.isNull(this.modelValue())) ? '' : this.modelValue();
383
                this.$('.u-field-value input').val(value);
Usman Khalid committed
384 385
            },

386
            saveValue: function() {
Usman Khalid committed
387 388 389 390 391 392
                var attributes = {};
                attributes[this.options.valueAttribute] = this.fieldValue();
                this.saveAttributes(attributes);
            }
        });

muzaffaryousaf committed
393
        FieldViews.DropdownFieldView = FieldViews.EditableFieldView.extend({
Usman Khalid committed
394 395 396

            fieldType: 'dropdown',

muhammad-ammar committed
397
            fieldTemplate: field_dropdown_template,
Usman Khalid committed
398 399

            events: {
muzaffaryousaf committed
400 401
                'click': 'startEditing',
                'focusout select': 'finishEditing'
Usman Khalid committed
402 403
            },

404
            initialize: function(options) {
405 406
                _.bindAll(this, 'render', 'optionForValue', 'fieldValue', 'displayValue', 'updateValueInField',
                    'saveValue', 'createGroupOptions');
Usman Khalid committed
407
                this._super(options);
muzaffaryousaf committed
408

409
                this.listenTo(this.model, 'change:' + this.options.valueAttribute, this.updateValueInField);
Usman Khalid committed
410 411
            },

412
            render: function() {
413
                HtmlUtils.setHtml(this.$el, HtmlUtils.template(this.fieldTemplate)({
Usman Khalid committed
414
                    id: this.options.valueAttribute,
muzaffaryousaf committed
415
                    mode: this.mode,
416
                    editable: this.editable,
Usman Khalid committed
417
                    title: this.options.title,
418
                    screenReaderTitle: this.options.screenReaderTitle || this.options.title,
419
                    titleVisible: this.options.titleVisible !== undefined ? this.options.titleVisible : true,
muzaffaryousaf committed
420
                    iconName: this.options.iconName,
421
                    showBlankOption: (!this.options.required || !this.modelValueIsSet()),
422
                    groupOptions: this.createGroupOptions(),
423
                    message: this.helpMessage
Usman Khalid committed
424
                }));
425
                this.delegateEvents();
426
                this.updateValueInField();
muzaffaryousaf committed
427 428 429 430

                if (this.editable === 'toggle') {
                    this.showCanEditMessage(this.mode === 'display');
                }
Usman Khalid committed
431 432 433
                return this;
            },

muzaffaryousaf committed
434 435
            modelValueIsSet: function() {
                var value = this.modelValue();
436
                if (_.isUndefined(value) || _.isNull(value) || value === '') {
muzaffaryousaf committed
437 438
                    return false;
                } else {
439
                    return !(_.isUndefined(this.optionForValue(value)));
muzaffaryousaf committed
440 441 442 443
                }
            },

            optionForValue: function(value) {
444 445 446 447 448 449 450 451 452 453 454
                var options = [];
                if (_.isUndefined(this.options.groupOptions)) {
                    return _.find(this.options.options, function(option) { return option[0] === value; });
                } else {
                    _.each(this.options.groupOptions, function(groupOption) {
                        options = options.concat(groupOption.selectOptions);
                    });
                    return _.find(options, function(option) {
                        return option[0] === value;
                    });
                }
muzaffaryousaf committed
455 456
            },

457
            fieldValue: function() {
458 459
                var value;
                if (this.editable === 'never') {
460
                    value = this.modelValueIsSet() ? this.modelValue() : null;
461 462 463 464
                }
                else {
                    value = this.$('.u-field-value select').val();
                }
465
                return value === '' ? null : value;
Usman Khalid committed
466 467
            },

468
            displayValue: function(value) {
muzaffaryousaf committed
469 470 471 472 473 474 475 476
                if (value) {
                    var option = this.optionForValue(value);
                    return (option ? option[1] : '');
                } else {
                    return '';
                }
            },

477
            updateValueInField: function() {
478 479 480
                if (this.editable !== 'never') {
                    this.$('.u-field-value select').val(this.modelValue() || '');
                }
481 482 483 484 485 486

                var value = this.displayValue(this.modelValue() || '');
                if (this.modelValueIsSet() === false) {
                    value = this.options.placeholderValue || '';
                }
                this.$('.u-field-value').attr('aria-label', this.options.title);
487
                this.$('.u-field-value-readonly').text(value);
488

muzaffaryousaf committed
489
                if (this.mode === 'display') {
490
                    this.updateDisplayModeClass();
muzaffaryousaf committed
491 492 493
                }
            },

494
            saveValue: function() {
muzaffaryousaf committed
495 496 497 498 499
                var attributes = {};
                attributes[this.options.valueAttribute] = this.fieldValue();
                this.saveAttributes(attributes);
            },

500 501 502 503 504 505 506
            showDisplayMode: function(render) {
                this._super(render);
                if (this.editable === 'toggle') {
                    this.$('.u-field-value a').focus();
                }
            },

muzaffaryousaf committed
507 508 509 510 511 512 513 514 515
            showEditMode: function(render) {
                this._super(render);
                if (this.editable === 'toggle') {
                    this.$('.u-field-value select').focus();
                }
            },

            saveSucceeded: function() {
                if (this.editable === 'toggle') {
516 517 518 519 520
                    this.showDisplayMode();
                }

                if (this.options.required && this.modelValueIsSet()) {
                    this.$('option[value=""]').remove();
muzaffaryousaf committed
521
                }
522

523
                this._super();
524 525 526
            },

            disableField: function(disable) {
527 528 529
                if (this.editable !== 'never') {
                    this.$('.u-field-value select').prop('disabled', disable);
                }
530 531 532 533 534 535 536 537
            },

            createGroupOptions: function() {
                return !(_.isUndefined(this.options.groupOptions)) ? this.options.groupOptions :
                    [{
                        groupTitle: null,
                        selectOptions: this.options.options
                    }];
muzaffaryousaf committed
538 539 540 541 542 543 544
            }
        });

        FieldViews.TextareaFieldView = FieldViews.EditableFieldView.extend({

            fieldType: 'textarea',

muhammad-ammar committed
545
            fieldTemplate: field_textarea_template,
muzaffaryousaf committed
546 547

            events: {
548
                'keydown .wrapper-u-field': 'startEditing',
muzaffaryousaf committed
549 550 551
                'click .wrapper-u-field': 'startEditing',
                'click .u-field-placeholder': 'startEditing',
                'focusout textarea': 'finishEditing',
552 553
                'change textarea': 'manageTextareaContentChange',
                'keyup textarea': 'manageTextareaContentChange',
554
                'keydown textarea': 'onKeyDown',
555 556
                'paste textarea': 'manageTextareaContentChange',
                'cut textarea': 'manageTextareaContentChange'
muzaffaryousaf committed
557 558
            },

559
            initialize: function(options) {
560 561
                _.bindAll(this, 'render', 'onKeyDown', 'adjustTextareaHeight', 'manageTextareaContentChange',
                    'fieldValue', 'saveValue', 'updateView');
muzaffaryousaf committed
562
                this._super(options);
563
                this.listenTo(this.model, 'change:' + this.options.valueAttribute, this.updateView);
muzaffaryousaf committed
564 565
            },

566
            render: function() {
muzaffaryousaf committed
567 568 569 570
                var value = this.modelValue();
                if (this.mode === 'display') {
                    value = value || this.options.placeholderValue;
                }
571
                HtmlUtils.setHtml(this.$el, HtmlUtils.template(this.fieldTemplate)({
muzaffaryousaf committed
572
                    id: this.options.valueAttribute,
573
                    screenReaderTitle: this.options.screenReaderTitle || this.options.title,
muzaffaryousaf committed
574
                    mode: this.mode,
575
                    editable: this.editable,
muzaffaryousaf committed
576
                    value: value,
577
                    message: this.helpMessage,
muhammad-ammar committed
578
                    messagePosition: this.options.messagePosition || 'footer',
579 580
                    placeholderValue: this.options.placeholderValue,
                    maxCharacters: this.options.maxCharacters || ''
muzaffaryousaf committed
581
                }));
582
                this.delegateEvents();
583 584
                this.title((this.modelValue() || this.mode === 'edit') ?
                    this.options.title : HtmlUtils.joinHtml(this.indicators.plus, this.options.title));
muzaffaryousaf committed
585 586 587 588 589 590 591

                if (this.editable === 'toggle') {
                    this.showCanEditMessage(this.mode === 'display');
                }
                return this;
            },

592
            onKeyDown: function(event) {
593 594 595 596 597 598 599 600
                if (event.keyCode === 13) {
                    event.preventDefault();
                    this.finishEditing(event);
                } else {
                    this.adjustTextareaHeight();
                }
            },

601 602 603 604 605 606 607 608 609
            updateCharCount: function() {
                var curCharCount;
                // Update character count for textarea
                if (this.options.maxCharacters) {
                    curCharCount = $('#u-field-textarea-' + this.options.valueAttribute).val().length;
                    $('.u-field-footer .current-char-count').text(curCharCount);
                }
            },

610
            adjustTextareaHeight: function() {
611
                if (this.persistChanges === false) { return; }
muzaffaryousaf committed
612 613 614 615
                var textarea = this.$('textarea');
                textarea.css('height', 'auto').css('height', textarea.prop('scrollHeight') + 10);
            },

616 617 618 619 620
            manageTextareaContentChange: function() {
                this.updateCharCount();
                this.adjustTextareaHeight();
            },

muzaffaryousaf committed
621 622
            modelValue: function() {
                var value = this._super();
623
                return value ? $.trim(value) : '';
muzaffaryousaf committed
624 625
            },

626
            fieldValue: function() {
627 628 629 630 631 632
                if (this.mode === 'edit') {
                    return this.$('.u-field-value textarea').val();
                }
                else {
                    return this.$('.u-field-value .u-field-value-readonly').text();
                }
Usman Khalid committed
633 634
            },

635
            saveValue: function() {
Usman Khalid committed
636 637 638
                var attributes = {};
                attributes[this.options.valueAttribute] = this.fieldValue();
                this.saveAttributes(attributes);
muzaffaryousaf committed
639 640
            },

641
            updateView: function() {
muzaffaryousaf committed
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
                if (this.mode !== 'edit') {
                    this.showDisplayMode(true);
                }
            },

            modelValueIsSet: function() {
                return !(this.modelValue() === '');
            },

            showEditMode: function(render) {
                this._super(render);
                this.adjustTextareaHeight();
                this.$('.u-field-value textarea').focus();
            },

            saveSucceeded: function() {
                this._super();
                if (this.editable === 'toggle') {
                    this.showDisplayMode(true);
661
                    this.$('a').focus();
muzaffaryousaf committed
662
                }
Usman Khalid committed
663 664 665 666 667 668 669
            }
        });

        FieldViews.LinkFieldView = FieldViews.FieldView.extend({

            fieldType: 'link',

muhammad-ammar committed
670
            fieldTemplate: field_link_template,
Usman Khalid committed
671 672 673 674 675

            events: {
                'click a': 'linkClicked'
            },

676
            initialize: function(options) {
Usman Khalid committed
677 678 679 680
                this._super(options);
                _.bindAll(this, 'render', 'linkClicked');
            },

681
            render: function() {
682
                HtmlUtils.setHtml(this.$el, HtmlUtils.template(this.fieldTemplate)({
Usman Khalid committed
683 684
                    id: this.options.valueAttribute,
                    title: this.options.title,
685
                    screenReaderTitle: this.options.screenReaderTitle || this.options.title,
Usman Khalid committed
686 687
                    linkTitle: this.options.linkTitle,
                    linkHref: this.options.linkHref,
Usman Khalid committed
688
                    message: this.helpMessage
Usman Khalid committed
689
                }));
690
                this.delegateEvents();
Usman Khalid committed
691 692 693
                return this;
            },

694
            linkClicked: function(event) {
Usman Khalid committed
695 696 697 698 699
                event.preventDefault();
            }
        });

        return FieldViews;
700
    });
Usman Khalid committed
701
}).call(this, define || RequireJS.define);