From ea1261f08a376c40bdeb48fcc1a1d095006e3bfe Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Tue, 2 Jun 2015 17:18:28 +0200 Subject: [PATCH] add backbone-super --- server/sonar-web/Gruntfile.coffee | 1 + .../src/main/js/apps/groups/delete-view.js | 4 +- .../src/main/js/apps/groups/form-view.js | 8 +- .../src/main/js/apps/groups/groups.js | 2 +- .../main/js/apps/groups/list-footer-view.js | 2 +- .../src/main/js/apps/groups/users-view.js | 4 +- .../main/js/apps/provisioning/delete-view.js | 4 +- .../main/js/apps/provisioning/form-view.js | 8 +- .../js/apps/provisioning/list-footer-view.js | 2 +- .../src/main/js/apps/provisioning/projects.js | 2 +- .../js/apps/users/change-password-view.js | 4 +- .../src/main/js/apps/users/deactivate-view.js | 4 +- .../src/main/js/apps/users/form-view.js | 10 +- .../src/main/js/apps/users/groups-view.js | 2 +- .../main/js/apps/users/list-footer-view.js | 2 +- .../src/main/js/apps/users/list-item-view.js | 2 +- .../sonar-web/src/main/js/apps/users/users.js | 2 +- .../js/libs/third-party/backbone-super.js | 111 ++++++++++++++++++ .../src/test/views/layouts/main.jade | 1 + 19 files changed, 144 insertions(+), 31 deletions(-) create mode 100644 server/sonar-web/src/main/js/libs/third-party/backbone-super.js diff --git a/server/sonar-web/Gruntfile.coffee b/server/sonar-web/Gruntfile.coffee index fc55da5fceb..7375822b38e 100644 --- a/server/sonar-web/Gruntfile.coffee +++ b/server/sonar-web/Gruntfile.coffee @@ -46,6 +46,7 @@ module.exports = (grunt) -> '<%= BUILD_PATH %>/js/libs/third-party/latinize.js' '<%= BUILD_PATH %>/js/libs/third-party/underscore.js' '<%= BUILD_PATH %>/js/libs/third-party/backbone.js' + '<%= BUILD_PATH %>/js/libs/third-party/backbone-super.js' '<%= BUILD_PATH %>/js/libs/third-party/backbone.marionette.js' '<%= BUILD_PATH %>/js/libs/third-party/handlebars.js' '<%= BUILD_PATH %>/js/libs/third-party/underscore.js' diff --git a/server/sonar-web/src/main/js/apps/groups/delete-view.js b/server/sonar-web/src/main/js/apps/groups/delete-view.js index 8fd83d34031..137aa768da2 100644 --- a/server/sonar-web/src/main/js/apps/groups/delete-view.js +++ b/server/sonar-web/src/main/js/apps/groups/delete-view.js @@ -6,8 +6,8 @@ define([ return ModalForm.extend({ template: Templates['groups-delete'], - onFormSubmit: function () { - ModalForm.prototype.onFormSubmit.apply(this, arguments); + onFormSubmit: function (e) { + this._super(e); this.sendRequest(); }, diff --git a/server/sonar-web/src/main/js/apps/groups/form-view.js b/server/sonar-web/src/main/js/apps/groups/form-view.js index aa872784e1e..e79ea6eec65 100644 --- a/server/sonar-web/src/main/js/apps/groups/form-view.js +++ b/server/sonar-web/src/main/js/apps/groups/form-view.js @@ -7,17 +7,17 @@ define([ template: Templates['groups-form'], onRender: function () { - ModalForm.prototype.onRender.apply(this, arguments); + this._super(); this.$('[data-toggle="tooltip"]').tooltip({ container: 'body', placement: 'bottom' }); }, onClose: function () { - ModalForm.prototype.onClose.apply(this, arguments); + this._super(); this.$('[data-toggle="tooltip"]').tooltip('destroy'); }, - onFormSubmit: function () { - ModalForm.prototype.onFormSubmit.apply(this, arguments); + onFormSubmit: function (e) { + this._super(e); this.sendRequest(); } }); diff --git a/server/sonar-web/src/main/js/apps/groups/groups.js b/server/sonar-web/src/main/js/apps/groups/groups.js index 9ddd6c9f9a1..dcfbb8c731b 100644 --- a/server/sonar-web/src/main/js/apps/groups/groups.js +++ b/server/sonar-web/src/main/js/apps/groups/groups.js @@ -19,7 +19,7 @@ define([ fetch: function (options) { var d = (options && options.data) || {}; this.q = d.q; - return Backbone.Collection.prototype.fetch.apply(this, arguments); + return this._super(options); }, fetchMore: function () { diff --git a/server/sonar-web/src/main/js/apps/groups/list-footer-view.js b/server/sonar-web/src/main/js/apps/groups/list-footer-view.js index cdad034f24a..3c0fbe198c5 100644 --- a/server/sonar-web/src/main/js/apps/groups/list-footer-view.js +++ b/server/sonar-web/src/main/js/apps/groups/list-footer-view.js @@ -23,7 +23,7 @@ define([ }, serializeData: function () { - return _.extend(Marionette.ItemView.prototype.serializeData.apply(this, arguments), { + return _.extend(this._super(), { total: this.collection.total, count: this.collection.length, more: this.collection.hasMore() diff --git a/server/sonar-web/src/main/js/apps/groups/users-view.js b/server/sonar-web/src/main/js/apps/groups/users-view.js index bd236b67040..7786d63fe44 100644 --- a/server/sonar-web/src/main/js/apps/groups/users-view.js +++ b/server/sonar-web/src/main/js/apps/groups/users-view.js @@ -8,7 +8,7 @@ define([ template: Templates['groups-users'], onRender: function () { - Modal.prototype.onRender.apply(this, arguments); + this._super(); new window.SelectList({ el: this.$('#groups-users'), width: '100%', @@ -35,7 +35,7 @@ define([ onClose: function () { this.model.collection.refresh(); - Modal.prototype.onClose.apply(this, arguments); + this._super(); } }); diff --git a/server/sonar-web/src/main/js/apps/provisioning/delete-view.js b/server/sonar-web/src/main/js/apps/provisioning/delete-view.js index 2ff38de169f..7797227aa40 100644 --- a/server/sonar-web/src/main/js/apps/provisioning/delete-view.js +++ b/server/sonar-web/src/main/js/apps/provisioning/delete-view.js @@ -6,8 +6,8 @@ define([ return ModalForm.extend({ template: Templates['provisioning-delete'], - onFormSubmit: function () { - ModalForm.prototype.onFormSubmit.apply(this, arguments); + onFormSubmit: function (e) { + this._super(e); this.sendRequest(); }, diff --git a/server/sonar-web/src/main/js/apps/provisioning/form-view.js b/server/sonar-web/src/main/js/apps/provisioning/form-view.js index ed7fe702f2c..ce359c6fa07 100644 --- a/server/sonar-web/src/main/js/apps/provisioning/form-view.js +++ b/server/sonar-web/src/main/js/apps/provisioning/form-view.js @@ -7,17 +7,17 @@ define([ template: Templates['provisioning-form'], onRender: function () { - ModalForm.prototype.onRender.apply(this, arguments); + this._super(); this.$('[data-toggle="tooltip"]').tooltip({ container: 'body', placement: 'bottom' }); }, onClose: function () { - ModalForm.prototype.onClose.apply(this, arguments); + this._super(); this.$('[data-toggle="tooltip"]').tooltip('destroy'); }, - onFormSubmit: function () { - ModalForm.prototype.onFormSubmit.apply(this, arguments); + onFormSubmit: function (e) { + this._super(e); this.sendRequest(); } diff --git a/server/sonar-web/src/main/js/apps/provisioning/list-footer-view.js b/server/sonar-web/src/main/js/apps/provisioning/list-footer-view.js index 6dc243e7b67..902b9322372 100644 --- a/server/sonar-web/src/main/js/apps/provisioning/list-footer-view.js +++ b/server/sonar-web/src/main/js/apps/provisioning/list-footer-view.js @@ -23,7 +23,7 @@ define([ }, serializeData: function () { - return _.extend(Marionette.ItemView.prototype.serializeData.apply(this, arguments), { + return _.extend(this._super(), { total: this.collection.total, count: this.collection.length, more: this.collection.hasMore() diff --git a/server/sonar-web/src/main/js/apps/provisioning/projects.js b/server/sonar-web/src/main/js/apps/provisioning/projects.js index 44f1c5b4b5a..a5c26347c59 100644 --- a/server/sonar-web/src/main/js/apps/provisioning/projects.js +++ b/server/sonar-web/src/main/js/apps/provisioning/projects.js @@ -19,7 +19,7 @@ define([ fetch: function (options) { var d = (options && options.data) || {}; this.q = d.q; - return Backbone.Collection.prototype.fetch.apply(this, arguments); + return this._super(options); }, fetchMore: function () { diff --git a/server/sonar-web/src/main/js/apps/users/change-password-view.js b/server/sonar-web/src/main/js/apps/users/change-password-view.js index cd2a892fc2b..6187333c9e6 100644 --- a/server/sonar-web/src/main/js/apps/users/change-password-view.js +++ b/server/sonar-web/src/main/js/apps/users/change-password-view.js @@ -6,8 +6,8 @@ define([ return ModalForm.extend({ template: Templates['users-change-password'], - onFormSubmit: function () { - ModalForm.prototype.onFormSubmit.apply(this, arguments); + onFormSubmit: function (e) { + this._super(e); this.sendRequest(); }, diff --git a/server/sonar-web/src/main/js/apps/users/deactivate-view.js b/server/sonar-web/src/main/js/apps/users/deactivate-view.js index cf4c4654984..37c71d4a94b 100644 --- a/server/sonar-web/src/main/js/apps/users/deactivate-view.js +++ b/server/sonar-web/src/main/js/apps/users/deactivate-view.js @@ -6,8 +6,8 @@ define([ return ModalForm.extend({ template: Templates['users-deactivate'], - onFormSubmit: function () { - ModalForm.prototype.onFormSubmit.apply(this, arguments); + onFormSubmit: function (e) { + this._super(e); this.sendRequest(); }, diff --git a/server/sonar-web/src/main/js/apps/users/form-view.js b/server/sonar-web/src/main/js/apps/users/form-view.js index 2cf2e1ac3f1..f1c7f602d14 100644 --- a/server/sonar-web/src/main/js/apps/users/form-view.js +++ b/server/sonar-web/src/main/js/apps/users/form-view.js @@ -9,23 +9,23 @@ define([ template: Templates['users-form'], events: function () { - return _.extend(ModalForm.prototype.events.apply(this, arguments), { + return _.extend(this._super(), { 'click #create-user-add-scm-account': 'onAddScmAccountClick' }); }, onRender: function () { - ModalForm.prototype.onRender.apply(this, arguments); + this._super(); this.$('[data-toggle="tooltip"]').tooltip({ container: 'body', placement: 'bottom' }); }, onClose: function () { - ModalForm.prototype.onClose.apply(this, arguments); + this._super(); this.$('[data-toggle="tooltip"]').tooltip('destroy'); }, - onFormSubmit: function () { - ModalForm.prototype.onFormSubmit.apply(this, arguments); + onFormSubmit: function (e) { + this._super(e); this.sendRequest(); }, diff --git a/server/sonar-web/src/main/js/apps/users/groups-view.js b/server/sonar-web/src/main/js/apps/users/groups-view.js index 992594b4f10..6a874070b66 100644 --- a/server/sonar-web/src/main/js/apps/users/groups-view.js +++ b/server/sonar-web/src/main/js/apps/users/groups-view.js @@ -9,7 +9,7 @@ define([ itemTemplate: Templates['users-group'], onRender: function () { - Modal.prototype.onRender.apply(this, arguments); + this._super(); new window.SelectList({ el: this.$('#users-groups'), width: '100%', diff --git a/server/sonar-web/src/main/js/apps/users/list-footer-view.js b/server/sonar-web/src/main/js/apps/users/list-footer-view.js index 968ad0990d5..cf802586354 100644 --- a/server/sonar-web/src/main/js/apps/users/list-footer-view.js +++ b/server/sonar-web/src/main/js/apps/users/list-footer-view.js @@ -23,7 +23,7 @@ define([ }, serializeData: function () { - return _.extend(Marionette.ItemView.prototype.serializeData.apply(this, arguments), { + return _.extend(this._super(), { total: this.collection.total, count: this.collection.length, more: this.collection.hasMore() diff --git a/server/sonar-web/src/main/js/apps/users/list-item-view.js b/server/sonar-web/src/main/js/apps/users/list-item-view.js index c6a81a2e71f..99aa973579b 100644 --- a/server/sonar-web/src/main/js/apps/users/list-item-view.js +++ b/server/sonar-web/src/main/js/apps/users/list-item-view.js @@ -101,7 +101,7 @@ define([ scmAccountsLimit = scmAccounts.length > this.scmLimit ? this.scmLimit - 1 : this.scmLimit, groups = this.model.get('groups'), groupsLimit = groups.length > this.groupsLimit ? this.groupsLimit - 1 : this.groupsLimit; - return _.extend(Marionette.ItemView.prototype.serializeData.apply(this, arguments), { + return _.extend(this._super(), { firstScmAccounts: _.first(scmAccounts, scmAccountsLimit), moreScmAccountsCount: scmAccounts.length - scmAccountsLimit, firstGroups: _.first(groups, groupsLimit), diff --git a/server/sonar-web/src/main/js/apps/users/users.js b/server/sonar-web/src/main/js/apps/users/users.js index adf80b669da..9c45979a9fe 100644 --- a/server/sonar-web/src/main/js/apps/users/users.js +++ b/server/sonar-web/src/main/js/apps/users/users.js @@ -19,7 +19,7 @@ define([ fetch: function (options) { var d = (options && options.data) || {}; this.q = d.q; - return Backbone.Collection.prototype.fetch.apply(this, arguments); + return this._super(options); }, fetchMore: function () { diff --git a/server/sonar-web/src/main/js/libs/third-party/backbone-super.js b/server/sonar-web/src/main/js/libs/third-party/backbone-super.js new file mode 100644 index 00000000000..9562638918e --- /dev/null +++ b/server/sonar-web/src/main/js/libs/third-party/backbone-super.js @@ -0,0 +1,111 @@ +// This is a plugin, constructed from parts of Backbone.js and John Resig's inheritance script. +// (See http://backbonejs.org, http://ejohn.org/blog/simple-javascript-inheritance/) +// No credit goes to me as I did absolutely nothing except patch these two together. +(function(root, factory) { + + // Set up Backbone appropriately for the environment. Start with AMD. + if (typeof define === 'function' && define.amd) { + define(['underscore', 'backbone'], function(_, Backbone) { + // Export global even in AMD case in case this script is loaded with + // others that may still expect a global Backbone. + factory( _, Backbone); + }); + + // Next for Node.js or CommonJS. + } else if (typeof exports !== 'undefined' && typeof require === 'function') { + var _ = require('underscore'), + Backbone = require('backbone'); + factory(_, Backbone); + + // Finally, as a browser global. + } else { + factory(root._, root.Backbone); + } + +}(this, function factory(_, Backbone) { + Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = function(protoProps, classProps) { + var child = inherits(this, protoProps, classProps); + child.extend = this.extend; + return child; + }; + var unImplementedSuper = function(method){throw "Super does not implement this method: " + method;}; + + var fnTest = /\b_super\b/; + + var makeWrapper = function(parentProto, name, fn) { + var wrapper = function() { + var tmp = this._super; + + // Add a new ._super() method that is the same method + // but on the super-class + this._super = parentProto[name] || unImplementedSuper(name); + + // The method only need to be bound temporarily, so we + // remove it when we're done executing + var ret; + try { + ret = fn.apply(this, arguments); + } finally { + this._super = tmp; + } + return ret; + }; + + //we must move properties from old function to new + for (var prop in fn) { + wrapper[prop] = fn[prop]; + delete fn[prop]; + } + + return wrapper; + }; + + var ctor = function(){}, inherits = function(parent, protoProps, staticProps) { + var child, parentProto = parent.prototype; + + // The constructor function for the new subclass is either defined by you + // (the "constructor" property in your `extend` definition), or defaulted + // by us to simply call the parent's constructor. + if (protoProps && protoProps.hasOwnProperty('constructor')) { + child = protoProps.constructor; + } else { + child = function(){ return parent.apply(this, arguments); }; + } + + // Inherit class (static) properties from parent. + _.extend(child, parent, staticProps); + + // Set the prototype chain to inherit from `parent`, without calling + // `parent`'s constructor function. + ctor.prototype = parentProto; + child.prototype = new ctor(); + + // Add prototype properties (instance properties) to the subclass, + // if supplied. + if (protoProps) { + _.extend(child.prototype, protoProps); + + // Copy the properties over onto the new prototype + for (var name in protoProps) { + // Check if we're overwriting an existing function + if (typeof protoProps[name] == "function" && fnTest.test(protoProps[name])) { + child.prototype[name] = makeWrapper(parentProto, name, protoProps[name]); + } + } + } + + // Add static properties to the constructor function, if supplied. + if (staticProps) _.extend(child, staticProps); + + // Correctly set child's `prototype.constructor`. + child.prototype.constructor = child; + + // Set a convenience property in case the parent's prototype is needed later. + child.__super__ = parentProto; + + return child; + }; + + return inherits; +})); + diff --git a/server/sonar-web/src/test/views/layouts/main.jade b/server/sonar-web/src/test/views/layouts/main.jade index 901a1042055..ccc08eb3ad8 100644 --- a/server/sonar-web/src/test/views/layouts/main.jade +++ b/server/sonar-web/src/test/views/layouts/main.jade @@ -10,6 +10,7 @@ html script(src='/js/libs/third-party/latinize.js') script(src='/js/libs/third-party/underscore.js') script(src='/js/libs/third-party/backbone.js') + script(src='/js/libs/third-party/backbone-super.js') script(src='/js/libs/third-party/backbone.marionette.js') script(src='/js/libs/third-party/handlebars.js') script(src='/js/libs/third-party/underscore.js') -- 2.39.5