From adfc5fe48ea6016c50b491ce1b1bcf8bb93b4bac Mon Sep 17 00:00:00 2001 From: Matt Lee Date: Thu, 14 Jan 2016 19:26:06 -0500 Subject: [PATCH] Subreddit Rules: Make about/rules page update cached rules Adds methods to the model and collections to generate JSON compatible with rendering the rules, and updates (or creates) the rules stored in sessionStorage on page load and on any change. --- r2/r2/public/static/js/edit-subreddit-rules.js | 29 +++++++++++++++++++++++- r2/r2/public/static/js/models/subreddit-rule.js | 30 ++++++++++++++++++++++++- r2/r2/public/static/js/report.js | 5 +++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/r2/r2/public/static/js/edit-subreddit-rules.js b/r2/r2/public/static/js/edit-subreddit-rules.js index cec194a3a..7c591d175 100644 --- a/r2/r2/public/static/js/edit-subreddit-rules.js +++ b/r2/r2/public/static/js/edit-subreddit-rules.js @@ -289,7 +289,12 @@ requires r.ui.TextCounter initialize: function(options) { this.ruleTemplate = options.ruleTemplate; this.formTemplate = options.formTemplate; - this.collection = new r.models.SubredditRuleCollection(); + var collectionOptions = { + subredditName: r.config.post_site, + subredditFullname: r.config.cur_site, + }; + this.collection = new r.models.SubredditRuleCollection(null, collectionOptions); + this.newRuleForm = new AddSubredditRuleView({ el: options.addForm, collection: this.collection, @@ -306,6 +311,10 @@ requires r.ui.TextCounter if (!this.collection.length) { this.newRuleForm.edit(); } + + r.hooks.get('new-report-form').register(function() { + this._updateRuleCache(); + }.bind(this)); }, delegateEvents: function() { @@ -318,6 +327,10 @@ requires r.ui.TextCounter this.addNewRule(model); }.bind(this)); }); + + this.listenTo(this.collection, 'sync', function() { + this._updateRuleCache(); + }); }, createSubredditRuleModel: function(el) { @@ -349,6 +362,20 @@ requires r.ui.TextCounter view.render(); this.$el.append(el); }, + + _updateRuleCache: function() { + debugger; + try { + var newRules = this.collection.toApiJSON(); + var storageKey = r.rulesSessionStorageKey; + var rulesCache = window.sessionStorage.getItem(storageKey); + rulesCache = rulesCache ? JSON.parse(rulesCache) : {}; + rulesCache[this.collection.subredditFullname] = newRules; + rulesCache = JSON.stringify(rulesCache); + window.sessionStorage.setItem(storageKey, rulesCache); + } catch (err) { + } + }, }); diff --git a/r2/r2/public/static/js/models/subreddit-rule.js b/r2/r2/public/static/js/models/subreddit-rule.js index 592b434c5..6df2f72c6 100644 --- a/r2/r2/public/static/js/models/subreddit-rule.js +++ b/r2/r2/public/static/js/models/subreddit-rule.js @@ -106,6 +106,16 @@ }; }, + toApiJSON: function() { + // same as toJSON, but strips it down to only what we'd get from the API + var data = this.toJSON(); + delete data.description_html; + if (data.kind === 'all') { + delete data.kind; + } + return data; + }, + initialize: function() { var short_name = this.get('short_name'); this._old_short_name = short_name; @@ -174,9 +184,18 @@ var SubredditRuleCollection = Backbone.Collection.extend({ model: SubredditRule, maxLength: RULES_COLLECTION_MAX_LENGTH, + subredditName: null, + subredditFullname: null, - initialize: function() { + initialize: function(models, options) { this._disabled = this.length >= this.maxLength; + + if (options && options.subredditName) { + this.subredditName = options.subredditName; + } + if (options && options.subredditFullname) { + this.subredditFullname = options.subredditFullname; + } this.on('add', function() { if (!this._disabled && this.length >= this.maxLength) { @@ -192,6 +211,15 @@ } }.bind(this)); }, + + toApiJSON: function() { + return { + sr_name: this.subredditName, + rules: this.models.map(function(model) { + return model.toApiJSON(); + }), + }; + }, }); diff --git a/r2/r2/public/static/js/report.js b/r2/r2/public/static/js/report.js index b568de2f1..ae94dce6a 100644 --- a/r2/r2/public/static/js/report.js +++ b/r2/r2/public/static/js/report.js @@ -44,6 +44,11 @@ $(function() { } finally { cachedRules = cachedRules || {}; } + + // temporary, after release this will get cleaned up and r.reports will + // be moved into this file + r.rulesSessionStorageKey = sessionStorageKey; + r.hooks.get('new-report-form').call(); } function renderFromTemplate(data, thingType) { -- 2.11.4.GIT