Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"inpage_reply.min.js","sources":["../src/inpage_reply.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * This module handles the in page replying to forum posts.\n *\n * @module     mod_forum/inpage_reply\n * @copyright  2019 Peter Dias\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine([\n    'jquery',\n    'core/templates',\n    'core/notification',\n    'mod_forum/repository',\n    'mod_forum/selectors',\n    'core_form/changechecker',\n], function(\n    $,\n    Templates,\n    Notification,\n    Repository,\n    Selectors,\n    FormChangeChecker\n) {\n\n    var DISPLAYCONSTANTS = {\n        NESTED_V2: 4,\n        THREADED: 2,\n        NESTED: 3,\n        FLAT_OLDEST_FIRST: 1,\n        FLAT_NEWEST_FIRST: -1\n    };\n\n    var EVENTS = {\n        POST_CREATED: 'mod_forum-post-created'\n    };\n\n     /**\n      * Moodle formats taken from the FORMAT_* constants declared in lib/weblib.php.\n      * @type {Object}\n      */\n    var CONTENT_FORMATS = {\n        MOODLE: 0\n    };\n    /**\n     * Show the loading icon for the submit button.\n     *\n     * @param {Object} button The submit button element\n     */\n    var showSubmitButtonLoadingIcon = function(button) {\n        var textContainer = button.find(Selectors.post.inpageSubmitBtnText);\n        var loadingIconContainer = button.find(Selectors.post.loadingIconContainer);\n        var width = button.outerWidth();\n        // Fix the width so that the button size doesn't change when we show the loading icon.\n        button.css('width', width);\n        textContainer.addClass('hidden');\n        loadingIconContainer.removeClass('hidden');\n    };\n\n    /**\n     * Hide the loading icon for the submit button.\n     *\n     * @param {Object} button The submit button element\n     */\n    var hideSubmitButtonLoadingIcon = function(button) {\n        var textContainer = button.find(Selectors.post.inpageSubmitBtnText);\n        var loadingIconContainer = button.find(Selectors.post.loadingIconContainer);\n        // Reset the width back to it's default.\n        button.css('width', '');\n        textContainer.removeClass('hidden');\n        loadingIconContainer.addClass('hidden');\n    };\n\n    /**\n     * Register the event listeners for the submit/cancel buttons of the in page reply.\n     *\n     * @param {Object} root The discussion container element.\n     */\n    var registerEventListeners = function(root) {\n        root.on('click', Selectors.post.inpageSubmitBtn, function(e) {\n            e.preventDefault();\n            var submitButton = $(e.currentTarget);\n            var allButtons = submitButton.parent().find(Selectors.post.inpageReplyButton);\n            var form = submitButton.parents(Selectors.post.inpageReplyForm).get(0);\n            var message = form.elements.post.value.trim();\n            // For now, we consider the inline reply post written using the FORMAT_MOODLE (because a textarea is displayed).\n            // In the future, other formats should be supported, letting users to use their preferred editor and format.\n            var messageformat = CONTENT_FORMATS.MOODLE;\n            // The message post will be converted from messageformat to FORMAT_HTML.\n            var topreferredformat = true;\n            var postid = form.elements.reply.value;\n            var subject = form.elements.subject.value;\n            var currentRoot = submitButton.closest(Selectors.post.post);\n            var isprivatereply = form.elements.privatereply != undefined ? form.elements.privatereply.checked : false;\n            var modeSelector = root.find(Selectors.post.modeSelect);\n            var mode = modeSelector.length ? parseInt(modeSelector.get(0).value) : null;\n            var newid;\n\n            if (message.length) {\n                showSubmitButtonLoadingIcon(submitButton);\n                allButtons.prop('disabled', true);\n\n                Repository.addDiscussionPost(postid, subject, message, messageformat, isprivatereply, topreferredformat)\n                    .then(function(context) {\n                        var message = context.messages.reduce(function(carry, message) {\n                            if (message.type == 'success') {\n                                carry += '<p>' + message.message + '</p>';\n                            }\n                            return carry;\n                        }, '');\n                        Notification.addNotification({\n                            message: message,\n                            type: \"success\"\n                        });\n\n                        return context;\n                    })\n                    .then(function(context) {\n                        form.reset();\n                        var post = context.post;\n                        newid = post.id;\n\n                        switch (mode) {\n                            case DISPLAYCONSTANTS.NESTED_V2:\n                                var capabilities = post.capabilities;\n                                var currentAuthorName = currentRoot.children()\n                                                                   .not(Selectors.post.repliesContainer)\n                                                                   .find(Selectors.post.authorName)\n                                                                   .text();\n                                post.parentauthorname = currentAuthorName;\n                                post.showactionmenu = capabilities.view ||\n                                                      capabilities.controlreadstatus ||\n                                                      capabilities.edit ||\n                                                      capabilities.split ||\n                                                      capabilities.delete ||\n                                                      capabilities.export ||\n                                                      post.urls.viewparent;\n                                return Templates.render('mod_forum/forum_discussion_nested_v2_post_reply', post);\n                            case DISPLAYCONSTANTS.THREADED:\n                                return Templates.render('mod_forum/forum_discussion_threaded_post', post);\n                            case DISPLAYCONSTANTS.NESTED:\n                                return Templates.render('mod_forum/forum_discussion_nested_post', post);\n                            default:\n                                return Templates.render('mod_forum/forum_discussion_post', post);\n                        }\n                    })\n                    .then(function(html, js) {\n                        var repliesnode = currentRoot.find(Selectors.post.repliesContainer).first();\n\n                        if (mode == DISPLAYCONSTANTS.FLAT_NEWEST_FIRST) {\n                            return Templates.prependNodeContents(repliesnode, html, js);\n                        } else {\n                            return Templates.appendNodeContents(repliesnode, html, js);\n                        }\n                    })\n                    .then(function() {\n                        submitButton.trigger(EVENTS.POST_CREATED, newid);\n                        hideSubmitButtonLoadingIcon(submitButton);\n                        allButtons.prop('disabled', false);\n\n                        // Tell formchangechecker we submitted the form.\n                        FormChangeChecker.resetFormDirtyState(submitButton[0]);\n\n                        return currentRoot.find(Selectors.post.inpageReplyContent).hide();\n                    })\n                    .then(function() {\n                        location.href = \"#p\" + newid;\n\n                        // Reload the page, say if threshold is being set by user those would get reflected through the templates.\n                        location.reload();\n                    })\n                    .catch(function(error) {\n                        hideSubmitButtonLoadingIcon(submitButton);\n                        allButtons.prop('disabled', false);\n                        return Notification.exception(error);\n                    });\n            }\n        });\n\n        root.on('click', Selectors.post.inpageCancelButton, function(e) {\n            // Tell formchangechecker to reset the form state.\n            FormChangeChecker.resetFormDirtyState(e.currentTarget);\n        });\n    };\n\n    return {\n        init: function(root) {\n            registerEventListeners(root);\n        },\n        CONTENT_FORMATS: CONTENT_FORMATS,\n        EVENTS: EVENTS\n    };\n});\n"],"names":["define","$","Templates","Notification","Repository","Selectors","FormChangeChecker","DISPLAYCONSTANTS","EVENTS","POST_CREATED","CONTENT_FORMATS","MOODLE","hideSubmitButtonLoadingIcon","button","textContainer","find","post","inpageSubmitBtnText","loadingIconContainer","css","removeClass","addClass","registerEventListeners","root","on","inpageSubmitBtn","e","preventDefault","newid","width","submitButton","currentTarget","allButtons","parent","inpageReplyButton","form","parents","inpageReplyForm","get","message","elements","value","trim","messageformat","postid","reply","subject","currentRoot","closest","isprivatereply","undefined","privatereply","checked","modeSelector","modeSelect","mode","length","parseInt","outerWidth","prop","addDiscussionPost","then","context","messages","reduce","carry","type","addNotification","reset","id","capabilities","currentAuthorName","children","not","repliesContainer","authorName","text","parentauthorname","showactionmenu","view","controlreadstatus","edit","split","delete","export","urls","viewparent","render","html","js","repliesnode","first","prependNodeContents","appendNodeContents","trigger","resetFormDirtyState","inpageReplyContent","hide","location","href","reload","catch","error","exception","inpageCancelButton","init"],"mappings":";;;;;;;AAsBAA,gCAAO,CACH,SACA,iBACA,oBACA,uBACA,sBACA,4BACD,SACCC,EACAC,UACAC,aACAC,WACAC,UACAC,uBAGIC,2BACW,EADXA,0BAEU,EAFVA,wBAGQ,EAHRA,oCAKoB,EAGpBC,OAAS,CACTC,aAAc,0BAOdC,gBAAkB,CAClBC,OAAQ,GAsBRC,4BAA8B,SAASC,YACnCC,cAAgBD,OAAOE,KAAKV,UAAUW,KAAKC,qBAC3CC,qBAAuBL,OAAOE,KAAKV,UAAUW,KAAKE,sBAEtDL,OAAOM,IAAI,QAAS,IACpBL,cAAcM,YAAY,UAC1BF,qBAAqBG,SAAS,WAQ9BC,uBAAyB,SAASC,MAClCA,KAAKC,GAAG,QAASnB,UAAUW,KAAKS,i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