define( [ 'backbone', 'js/views/baseview', 'underscore', 'js/models/metadata', 'js/views/abstract_editor', 'js/models/uploads', 'js/views/uploads', 'js/models/license', 'js/views/license', 'js/views/video/transcripts/utils', 'js/views/video/transcripts/metadata_videolist', 'js/views/video/translations_editor', 'edx-ui-toolkit/js/utils/html-utils' ], function(Backbone, BaseView, _, MetadataModel, AbstractEditor, FileUpload, UploadDialog, LicenseModel, LicenseView, TranscriptUtils, VideoList, VideoTranslations, HtmlUtils) { 'use strict'; var Metadata = {}; Metadata.Editor = BaseView.extend({ // Store rendered view references views: {}, // Model is CMS.Models.MetadataCollection, initialize: function() { var self = this, counter = 0, locator = self.$el.closest('[data-locator]').data('locator'), courseKey = self.$el.closest('[data-course-key]').data('course-key'), attributes = {numEntries: this.collection.length, locator: locator}; this.template = this.loadTemplate('metadata-editor'); this.$el.html(HtmlUtils.HTML(this.template(attributes)).toString()); this.collection.each( function(model) { var data = { el: self.$el.find('.metadata_entry')[counter++], courseKey: courseKey, locator: locator, model: model }, conversions = { Select: 'Option', Float: 'Number', Integer: 'Number' }, type = model.getType(); if (conversions[type]) { type = conversions[type]; } if (_.isFunction(Metadata[type])) { self.views[data.model.getFieldName()] = new Metadata[type](data); } else { // Everything else is treated as GENERIC_TYPE, which uses String editor. self.views[data.model.getFieldName()] = new Metadata.String(data); } }); }, /** * Returns just the modified metadata values, in the format used to persist to the server. */ getModifiedMetadataValues: function() { var modified_values = {}; this.collection.each( function(model) { if (model.isModified()) { modified_values[model.getFieldName()] = model.getValue(); } } ); return modified_values; }, /** * Returns a display name for the component related to this metadata. This method looks to see * if there is a metadata entry called 'display_name', and if so, it returns its value. If there * is no such entry, or if display_name does not have a value set, it returns an empty string. */ getDisplayName: function() { var displayName = ''; this.collection.each( function(model) { if (model.get('field_name') === 'display_name') { var displayNameValue = model.get('value'); // It is possible that there is no display name value set. In that case, return empty string. displayName = displayNameValue || ''; } } ); return displayName; } }); Metadata.VideoList = VideoList; Metadata.VideoTranslations = VideoTranslations; Metadata.String = AbstractEditor.extend({ events: { 'change input': 'updateModel', 'keypress .setting-input': 'showClearButton', 'click .setting-clear': 'clear' }, templateName: 'metadata-string-entry', render: function() { AbstractEditor.prototype.render.apply(this); // If the model has property `non editable` equals `true`, // the field is disabled, but user is able to clear it. if (this.model.get('non_editable')) { this.$el.find('#' + this.uniqueId) .prop('readonly', true) .addClass('is-disabled') .attr('aria-disabled', true); } }, getValueFromEditor: function() { return this.$el.find('#' + this.uniqueId).val(); }, setValueInEditor: function(value) { this.$el.find('input').val(value); } }); Metadata.VideoID = Metadata.String.extend({ // Delay between check_transcript requests requestDelay: 300, initialize: function() { Metadata.String.prototype.initialize.apply(this, arguments); this.$el.on( 'input', 'input', _.debounce(_.bind(this.inputChange, this), this.requestDelay) ); }, render: function() { Metadata.String.prototype.render.apply(this, arguments); TranscriptUtils.Storage.set('edx_video_id', this.getValueFromEditor()); }, clear: function() { this.model.setValue(''); this.inputChange(); }, getData: function() { return [{mode: 'edx_video_id', type: 'edx_video_id', video: this.getValueFromEditor()}]; }, inputChange: function() { TranscriptUtils.Storage.set('edx_video_id', this.getValueFromEditor()); Backbone.trigger('transcripts:basicTabFieldChanged'); } }); Metadata.Number = AbstractEditor.extend({ events: { 'change input': 'updateModel', 'keypress .setting-input': 'keyPressed', 'change .setting-input': 'changed', 'click .setting-clear': 'clear' }, render: function() { AbstractEditor.prototype.render.apply(this); if (!this.initialized) { var numToString = function(val) { return val.toFixed(4); }; var min = 'min'; var max = 'max'; var step = 'step'; var options = this.model.getOptions(); if (options.hasOwnProperty(min)) { this.min = Number(options[min]); this.$el.find('input').attr(min, numToString(this.min)); } if (options.hasOwnProperty(max)) { this.max = Number(options[max]); this.$el.find('input').attr(max, numToString(this.max)); } var stepValue = undefined; if (options.hasOwnProperty(step)) { // Parse step and convert to String. Polyfill doesn't like float values like ".1" (expects "0.1"). stepValue = numToString(Number(options[step])); } else if (this.isIntegerField()) { stepValue = '1'; } if (stepValue !== undefined) { this.$el.find('input').attr(step, stepValue); } // Manually runs polyfill for input number types to correct for Firefox non-support. // inputNumber will be undefined when unit test is running. if ($.fn.inputNumber) { this.$el.find('.setting-input-number').inputNumber(); } this.initialized = true; } return this; }, templateName: 'metadata-number-entry', getValueFromEditor: function() { return this.$el.find('#' + this.uniqueId).val(); }, setValueInEditor: function(value) { this.$el.find('input').val(value); }, /** * Returns true if this view is restricted to integers, as opposed to floating points values. */ isIntegerField: function() { return this.model.getType() === 'Integer'; }, keyPressed: function(e) { this.showClearButton(); // This first filtering if statement is take from polyfill to prevent // non-numeric input (for browsers that don't use polyfill because they DO have a number input type). var _ref, _ref1; if (((_ref = e.keyCode) !== 8 && _ref !== 9 && _ref !== 35 && _ref !== 36 && _ref !== 37 && _ref !== 39) && ((_ref1 = e.which) !== 45 && _ref1 !== 46 && _ref1 !== 48 && _ref1 !== 49 && _ref1 !== 50 && _ref1 !== 51 && _ref1 !== 52 && _ref1 !== 53 && _ref1 !== 54 && _ref1 !== 55 && _ref1 !== 56 && _ref1 !== 57)) { e.preventDefault(); } // For integers, prevent decimal points. if (this.isIntegerField() && e.keyCode === 46) { e.preventDefault(); } }, changed: function() { // Limit value to the range specified by min and max (necessary for browsers that aren't using polyfill). // Prevent integer/float fields value to be empty (set them to their defaults) var value = this.getValueFromEditor(); if (value) { if ((this.max !== undefined) && value > this.max) { value = this.max; } else if ((this.min != undefined) && value < this.min) { value = this.min; } this.setValueInEditor(value); this.updateModel(); } else { this.clear(); } } }); Metadata.Option = AbstractEditor.extend({ events: { 'change select': 'updateModel', 'click .setting-clear': 'clear' }, templateName: 'metadata-option-entry', getValueFromEditor: function() { var selectedText = this.$el.find('#' + this.uniqueId).find(':selected').text(); var selectedValue; _.each(this.model.getOptions(), function(modelValue) { if (modelValue === selectedText) { selectedValue = modelValue; } else if (modelValue.display_name === selectedText) { selectedValue = modelValue.value; } }); return selectedValue; }, setValueInEditor: function(value) { // Value here is the json value as used by the field. The choice may instead be showing display names. // Find the display name matching the value passed in. _.each(this.model.getOptions(), function(modelValue) { if (modelValue.value === value) { value = modelValue.display_name; } }); this.$el.find('#' + this.uniqueId + ' option').filter(function() { return $(this).text() === value; }).prop('selected', true); } }); Metadata.List = AbstractEditor.extend({ events: { 'click .setting-clear': 'clear', 'keypress .setting-input': 'showClearButton', 'change input': 'updateModel', 'input input': 'enableAdd', 'click .create-setting': 'addEntry', 'click .remove-setting': 'removeEntry' }, templateName: 'metadata-list-entry', getValueFromEditor: function() { return _.map( this.$el.find('li input'), function(ele) { return ele.value.trim(); } ).filter(_.identity); }, setValueInEditor: function(value) { var list = this.$el.find('ol'); list.empty(); _.each(value, function(ele, index) { var template = _.template( HtmlUtils.joinHtml( HtmlUtils.HTML('