license.js 5.85 KB
Newer Older
1 2 3 4 5
define([
    "js/views/baseview",
    "underscore",
    "text!templates/license-selector.underscore"
], function(BaseView, _, licenseSelectorTemplate) {
6 7 8 9
    var defaultLicenseInfo = {
        "all-rights-reserved": {
            "name": gettext("All Rights Reserved"),
            "tooltip": gettext("You reserve all rights for your work")
10
        },
11 12 13
        "creative-commons": {
            "name": gettext("Creative Commons"),
            "tooltip": gettext("You waive some rights for your work, such that others can use it too"),
14
            "url": "https://creativecommons.org/about",
15 16 17 18 19 20 21 22 23 24
            "options": {
                "ver": {
                    "name": gettext("Version"),
                    "type": "string",
                    "default": "4.0",
                },
                "BY": {
                    "name": gettext("Attribution"),
                    "type": "boolean",
                    "default": true,
25
                    "help": gettext("Allow others to copy, distribute, display and perform your copyrighted work but only if they give credit the way you request. Currently, this option is required."),
26 27 28 29 30 31
                    "disabled": true,
                },
                "NC": {
                    "name": gettext("Noncommercial"),
                    "type": "boolean",
                    "default": true,
32
                    "help": gettext("Allow others to copy, distribute, display and perform your work - and derivative works based upon it - but for noncommercial purposes only."),
33 34 35 36 37
                },
                "ND": {
                    "name": gettext("No Derivatives"),
                    "type": "boolean",
                    "default": true,
38
                    "help": gettext("Allow others to copy, distribute, display and perform only verbatim copies of your work, not derivative works based upon it. This option is incompatible with \"Share Alike\"."),
39 40 41 42 43 44
                    "conflictsWith": ["SA"]
                },
                "SA": {
                    "name": gettext("Share Alike"),
                    "type": "boolean",
                    "default": false,
45
                    "help": gettext("Allow others to distribute derivative works only under a license identical to the license that governs your work. This option is incompatible with \"No Derivatives\"."),
46 47 48 49
                    "conflictsWith": ["ND"]
                }
            },
            "option_order": ["BY", "NC", "ND", "SA"]
50 51 52
        }
    }

53 54 55 56 57
    var LicenseView = BaseView.extend({
        events: {
            "click ul.license-types li button" : "onLicenseClick",
            "click ul.license-options li": "onOptionClick"
        },
58

59 60 61
        initialize: function(options) {
            this.licenseInfo = options.licenseInfo || defaultLicenseInfo;
            this.showPreview = !!options.showPreview; // coerce to boolean
62

63 64 65 66
            // Rerender when the model changes
            this.listenTo(this.model, 'change', this.render);
            this.render();
        },
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        getDefaultOptionsForLicenseType: function(licenseType) {
            if (!this.licenseInfo[licenseType]) {
                // custom license type, no options
                return {};
            }
            if (!this.licenseInfo[licenseType].options) {
                // defined license type without options
                return {};
            }
            var defaults = {};
            _.each(this.licenseInfo[licenseType].options, function(value, key) {
                defaults[key] = value.default;
            })
            return defaults;
        },
83

84
        render: function() {
85
            this.$el.html(_.template(licenseSelectorTemplate)({
86 87 88 89 90 91 92 93
                model: this.model.attributes,
                licenseString: this.model.toString() || "",
                licenseInfo: this.licenseInfo,
                showPreview: this.showPreview,
                previewButton: false,
            }));
            return this;
        },
94

95 96 97
        onLicenseClick: function(e) {
            var $li = $(e.srcElement || e.target).closest('li');
            var licenseType = $li.data("license");
98 99 100 101 102 103 104 105 106 107 108

	    // Check that we've selected a different license type than what's currently selected
	    if (licenseType != this.model.attributes.type) {
		this.model.set({
                    "type": licenseType,
                    "options": this.getDefaultOptionsForLicenseType(licenseType)
		});
		// Fire the change event manually
		this.model.trigger("change change:type")
	    }
	    e.preventDefault();
109
        },
110

111 112 113 114
        onOptionClick: function(e) {
            var licenseType = this.model.get("type"),
                licenseOptions = $.extend({}, this.model.get("options")),
                $li = $(e.srcElement || e.target).closest('li');
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129
            var optionKey = $li.data("option")
            var licenseInfo = this.licenseInfo[licenseType];
            var optionInfo = licenseInfo.options[optionKey];
            if (optionInfo.disabled) {
                // we're done here
                return;
            }
            var currentOptionValue = licenseOptions[optionKey];
            if (optionInfo.type === "boolean") {
                // toggle current value
                currentOptionValue = !currentOptionValue;
                licenseOptions[optionKey] = currentOptionValue;
            }
            // check for conflicts
130 131 132 133 134 135 136
            if (currentOptionValue && optionInfo.conflictsWith) {
		var conflicts = optionInfo.conflictsWith;
		for (var i=0; i<conflicts.length; i++) {
		    // Uncheck all conflicts
		    licenseOptions[conflicts[i]] = false;
		    console.log(licenseOptions);
		}
137
            }
138 139 140 141 142

            this.model.set({"options": licenseOptions})
            // Backbone has trouble identifying when objects change, so we'll
            // fire the change event manually.
            this.model.trigger("change change:options")
143
            e.preventDefault();
144 145
        }

146 147
    });
    return LicenseView;
148
});