| 1 | efrain | 1 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 2 | //
 | 
        
           |  |  | 3 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 4 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 5 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 6 | // (at your option) any later version.
 | 
        
           |  |  | 7 | //
 | 
        
           |  |  | 8 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 11 | // GNU General Public License for more details.
 | 
        
           |  |  | 12 | //
 | 
        
           |  |  | 13 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 14 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | /**
 | 
        
           |  |  | 17 |  * Javascript controller for the "Actions" panel at the bottom of the page.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module     mod_assign/grading_actions
 | 
        
           |  |  | 20 |  * @copyright  2016 Damyon Wiese <damyon@moodle.com>
 | 
        
           |  |  | 21 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 22 |  * @since      3.1
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 | define(['jquery', 'mod_assign/grading_events'], function($, GradingEvents) {
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 |     /**
 | 
        
           |  |  | 27 |      * GradingActions class.
 | 
        
           |  |  | 28 |      *
 | 
        
           |  |  | 29 |      * @class mod_assign/grading_actions
 | 
        
           |  |  | 30 |      * @param {String} selector The selector for the page region containing the actions panel.
 | 
        
           |  |  | 31 |      */
 | 
        
           |  |  | 32 |     var GradingActions = function(selector) {
 | 
        
           |  |  | 33 |         this._regionSelector = selector;
 | 
        
           |  |  | 34 |         this._region = $(selector);
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |         this.registerEventListeners();
 | 
        
           |  |  | 37 |     };
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /** @property {String} Selector for the page region containing the user navigation. */
 | 
        
           |  |  | 40 |     GradingActions.prototype._regionSelector = null;
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |     /** @property {Integer} Remember the last user id to prevent unnessecary reloads. */
 | 
        
           |  |  | 43 |     GradingActions.prototype._lastUserId = 0;
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 |     /** @property {JQuery} JQuery node for the page region containing the user navigation. */
 | 
        
           |  |  | 46 |     GradingActions.prototype._region = null;
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |     /**
 | 
        
           |  |  | 49 |      * Show the actions if there is valid user.
 | 
        
           |  |  | 50 |      *
 | 
        
           |  |  | 51 |      * @method _showActionsForm
 | 
        
           |  |  | 52 |      * @private
 | 
        
           |  |  | 53 |      * @param {Event} event
 | 
        
           |  |  | 54 |      * @param {Integer} userid
 | 
        
           |  |  | 55 |      */
 | 
        
           |  |  | 56 |     GradingActions.prototype._showActionsForm = function(event, userid) {
 | 
        
           |  |  | 57 |         var form = this._region.find('[data-region=grading-actions-form]');
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         if (userid != this._lastUserId && userid > 0) {
 | 
        
           |  |  | 60 |             this._lastUserId = userid;
 | 
        
           |  |  | 61 |         }
 | 
        
           |  |  | 62 |         if (userid > 0) {
 | 
        
           |  |  | 63 |             form.removeClass('hide');
 | 
        
           |  |  | 64 |         } else {
 | 
        
           |  |  | 65 |             form.addClass('hide');
 | 
        
           |  |  | 66 |         }
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |     };
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |     /**
 | 
        
           |  |  | 71 |      * Trigger the named action.
 | 
        
           |  |  | 72 |      *
 | 
        
           |  |  | 73 |      * @method _trigger
 | 
        
           |  |  | 74 |      * @private
 | 
        
           |  |  | 75 |      * @param {String} action
 | 
        
           |  |  | 76 |      */
 | 
        
           |  |  | 77 |     GradingActions.prototype._trigger = function(action) {
 | 
        
           |  |  | 78 |         $(document).trigger(action);
 | 
        
           |  |  | 79 |     };
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |     /**
 | 
        
           |  |  | 82 |      * Get the review panel element.
 | 
        
           |  |  | 83 |      *
 | 
        
           |  |  | 84 |      * @method getReviewPanelElement
 | 
        
           |  |  | 85 |      * @return {jQuery}
 | 
        
           |  |  | 86 |      */
 | 
        
           |  |  | 87 |     GradingActions.prototype.getReviewPanelElement = function() {
 | 
        
           |  |  | 88 |         return $('[data-region="review-panel"]');
 | 
        
           |  |  | 89 |     };
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |     /**
 | 
        
           |  |  | 92 |      * Check if the page has a review panel.
 | 
        
           |  |  | 93 |      *
 | 
        
           |  |  | 94 |      * @method hasReviewPanelElement
 | 
        
           |  |  | 95 |      * @return {bool}
 | 
        
           |  |  | 96 |      */
 | 
        
           |  |  | 97 |     GradingActions.prototype.hasReviewPanelElement = function() {
 | 
        
           |  |  | 98 |         return this.getReviewPanelElement().length > 0;
 | 
        
           |  |  | 99 |     };
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |     /**
 | 
        
           |  |  | 102 |      * Get the collapse grade panel button.
 | 
        
           |  |  | 103 |      *
 | 
        
           |  |  | 104 |      * @method getCollapseGradePanelButton
 | 
        
           |  |  | 105 |      * @return {jQuery}
 | 
        
           |  |  | 106 |      */
 | 
        
           |  |  | 107 |     GradingActions.prototype.getCollapseGradePanelButton = function() {
 | 
        
           |  |  | 108 |         return $('[data-region="grade-actions"] .collapse-grade-panel');
 | 
        
           |  |  | 109 |     };
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |     /**
 | 
        
           |  |  | 112 |      * Get the collapse review panel button.
 | 
        
           |  |  | 113 |      *
 | 
        
           |  |  | 114 |      * @method getCollapseReviewPanelButton
 | 
        
           |  |  | 115 |      * @return {jQuery}
 | 
        
           |  |  | 116 |      */
 | 
        
           |  |  | 117 |     GradingActions.prototype.getCollapseReviewPanelButton = function() {
 | 
        
           |  |  | 118 |         return $('[data-region="grade-actions"] .collapse-review-panel');
 | 
        
           |  |  | 119 |     };
 | 
        
           |  |  | 120 |   | 
        
           |  |  | 121 |     /**
 | 
        
           |  |  | 122 |      * Get the expand all panels button.
 | 
        
           |  |  | 123 |      *
 | 
        
           |  |  | 124 |      * @method getExpandAllPanelsButton
 | 
        
           |  |  | 125 |      * @return {jQuery}
 | 
        
           |  |  | 126 |      */
 | 
        
           |  |  | 127 |     GradingActions.prototype.getExpandAllPanelsButton = function() {
 | 
        
           |  |  | 128 |         return $('[data-region="grade-actions"] .collapse-none');
 | 
        
           |  |  | 129 |     };
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 |     /**
 | 
        
           |  |  | 132 |      * Remove the active state from all layout buttons.
 | 
        
           |  |  | 133 |      *
 | 
        
           |  |  | 134 |      * @method resetLayoutButtons
 | 
        
           |  |  | 135 |      */
 | 
        
           |  |  | 136 |     GradingActions.prototype.resetLayoutButtons = function() {
 | 
        
           |  |  | 137 |         this.getCollapseGradePanelButton().removeClass('active');
 | 
        
           |  |  | 138 |         this.getCollapseReviewPanelButton().removeClass('active');
 | 
        
           |  |  | 139 |         this.getExpandAllPanelsButton().removeClass('active');
 | 
        
           |  |  | 140 |     };
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 |     /**
 | 
        
           |  |  | 143 |      * Hide the review panel.
 | 
        
           |  |  | 144 |      *
 | 
        
           |  |  | 145 |      * @method collapseReviewPanel
 | 
        
           |  |  | 146 |      */
 | 
        
           |  |  | 147 |     GradingActions.prototype.collapseReviewPanel = function() {
 | 
        
           |  |  | 148 |         $(document).trigger(GradingEvents.COLLAPSE_REVIEW_PANEL);
 | 
        
           |  |  | 149 |         $(document).trigger(GradingEvents.EXPAND_GRADE_PANEL);
 | 
        
           |  |  | 150 |         this.resetLayoutButtons();
 | 
        
           |  |  | 151 |         this.getCollapseReviewPanelButton().addClass('active');
 | 
        
           |  |  | 152 |     };
 | 
        
           |  |  | 153 |   | 
        
           |  |  | 154 |     /**
 | 
        
           |  |  | 155 |      * Show/Hide the grade panel.
 | 
        
           |  |  | 156 |      *
 | 
        
           |  |  | 157 |      * @method collapseGradePanel
 | 
        
           |  |  | 158 |      */
 | 
        
           |  |  | 159 |     GradingActions.prototype.collapseGradePanel = function() {
 | 
        
           |  |  | 160 |         $(document).trigger(GradingEvents.COLLAPSE_GRADE_PANEL);
 | 
        
           |  |  | 161 |         $(document).trigger(GradingEvents.EXPAND_REVIEW_PANEL);
 | 
        
           |  |  | 162 |         this.resetLayoutButtons();
 | 
        
           |  |  | 163 |         this.getCollapseGradePanelButton().addClass('active');
 | 
        
           |  |  | 164 |     };
 | 
        
           |  |  | 165 |   | 
        
           |  |  | 166 |     /**
 | 
        
           |  |  | 167 |      * Return the layout to default.
 | 
        
           |  |  | 168 |      *
 | 
        
           |  |  | 169 |      * @method expandAllPanels
 | 
        
           |  |  | 170 |      */
 | 
        
           |  |  | 171 |     GradingActions.prototype.expandAllPanels = function() {
 | 
        
           |  |  | 172 |         $(document).trigger(GradingEvents.EXPAND_GRADE_PANEL);
 | 
        
           |  |  | 173 |         $(document).trigger(GradingEvents.EXPAND_REVIEW_PANEL);
 | 
        
           |  |  | 174 |         this.resetLayoutButtons();
 | 
        
           |  |  | 175 |         this.getExpandAllPanelsButton().addClass('active');
 | 
        
           |  |  | 176 |     };
 | 
        
           |  |  | 177 |   | 
        
           |  |  | 178 |     /**
 | 
        
           |  |  | 179 |      * Register event listeners for the grade panel.
 | 
        
           |  |  | 180 |      *
 | 
        
           |  |  | 181 |      * @method registerEventListeners
 | 
        
           |  |  | 182 |      */
 | 
        
           |  |  | 183 |     GradingActions.prototype.registerEventListeners = function() {
 | 
        
           |  |  | 184 |         // Don't need layout controls if there is no review panel.
 | 
        
           |  |  | 185 |         if (this.hasReviewPanelElement()) {
 | 
        
           |  |  | 186 |             var collapseReviewPanelButton = this.getCollapseReviewPanelButton();
 | 
        
           |  |  | 187 |             collapseReviewPanelButton.click(function(e) {
 | 
        
           |  |  | 188 |                 this.collapseReviewPanel();
 | 
        
           |  |  | 189 |                 e.preventDefault();
 | 
        
           |  |  | 190 |             }.bind(this));
 | 
        
           |  |  | 191 |   | 
        
           |  |  | 192 |             collapseReviewPanelButton.keydown(function(e) {
 | 
        
           |  |  | 193 |                 if (!e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey) {
 | 
        
           |  |  | 194 |                     if (e.keyCode === 13 || e.keyCode === 32) {
 | 
        
           |  |  | 195 |                         this.collapseReviewPanel();
 | 
        
           |  |  | 196 |                         e.preventDefault();
 | 
        
           |  |  | 197 |                     }
 | 
        
           |  |  | 198 |                 }
 | 
        
           |  |  | 199 |             }.bind(this));
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 |             var collapseGradePanelButton = this.getCollapseGradePanelButton();
 | 
        
           |  |  | 202 |             collapseGradePanelButton.click(function(e) {
 | 
        
           |  |  | 203 |                 this.collapseGradePanel();
 | 
        
           |  |  | 204 |                 e.preventDefault();
 | 
        
           |  |  | 205 |             }.bind(this));
 | 
        
           |  |  | 206 |   | 
        
           |  |  | 207 |             collapseGradePanelButton.keydown(function(e) {
 | 
        
           |  |  | 208 |                 if (!e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey) {
 | 
        
           |  |  | 209 |                     if (e.keyCode === 13 || e.keyCode === 32) {
 | 
        
           |  |  | 210 |                         this.collapseGradePanel();
 | 
        
           |  |  | 211 |                         e.preventDefault();
 | 
        
           |  |  | 212 |                     }
 | 
        
           |  |  | 213 |                 }
 | 
        
           |  |  | 214 |             }.bind(this));
 | 
        
           |  |  | 215 |   | 
        
           |  |  | 216 |             var expandAllPanelsButton = this.getExpandAllPanelsButton();
 | 
        
           |  |  | 217 |             expandAllPanelsButton.click(function(e) {
 | 
        
           |  |  | 218 |                 this.expandAllPanels();
 | 
        
           |  |  | 219 |                 e.preventDefault();
 | 
        
           |  |  | 220 |             }.bind(this));
 | 
        
           |  |  | 221 |   | 
        
           |  |  | 222 |             expandAllPanelsButton.keydown(function(e) {
 | 
        
           |  |  | 223 |                 if (!e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey) {
 | 
        
           |  |  | 224 |                     if (e.keyCode === 13 || e.keyCode === 32) {
 | 
        
           |  |  | 225 |                         this.expandAllPanels();
 | 
        
           |  |  | 226 |                         e.preventDefault();
 | 
        
           |  |  | 227 |                     }
 | 
        
           |  |  | 228 |                 }
 | 
        
           |  |  | 229 |             }.bind(this));
 | 
        
           |  |  | 230 |         }
 | 
        
           |  |  | 231 |   | 
        
           |  |  | 232 |         $(document).on('user-changed', this._showActionsForm.bind(this));
 | 
        
           |  |  | 233 |   | 
        
           |  |  | 234 |         this._region.find('[name="savechanges"]').on('click', this._trigger.bind(this, 'save-changes'));
 | 
        
           |  |  | 235 |         this._region.find('[name="saveandshownext"]').on('click', this._trigger.bind(this, 'save-and-show-next'));
 | 
        
           |  |  | 236 |         this._region.find('[name="resetbutton"]').on('click', this._trigger.bind(this, 'reset'));
 | 
        
           |  |  | 237 |         this._region.find('form').on('submit', function(e) {
 | 
        
           |  |  | 238 |             e.preventDefault();
 | 
        
           |  |  | 239 |         });
 | 
        
           |  |  | 240 |     };
 | 
        
           |  |  | 241 |   | 
        
           |  |  | 242 |     return GradingActions;
 | 
        
           |  |  | 243 | });
 |