Merge pull request #947 from nupplaphil/task/di_l10n
[friendica-addons.git/.git] / advancedcontentfilter / advancedcontentfilter.js
1 $.ajaxSetup({headers: {'X-CSRF-Token': csrfToken}});
2
3 $.extend({
4         ajaxJSON: function(method, url, data) {
5                 return $.ajax({
6                         type: method.toUpperCase(),
7                         url: url,
8                         data: JSON.stringify(data),
9                         contentType: 'application/json; charset=utf-8',
10                         dataType: 'json'
11                 });
12         }
13 });
14
15 new Vue({
16         el: '#rules',
17
18         data: {
19                 showModal: false,
20                 errorMessage: '',
21                 editedIndex: null,
22                 rule: {id: '', name: '', expression: '', created: ''},
23                 rules: existingRules || [],
24                 itemUrl: '',
25                 itemJson: '',
26                 currentTheme: currentTheme,
27                 messages: messages,
28         },
29
30         watch: {
31                 showModal: function () {
32                         if (this.showModal) {
33                                 $(this.$refs.vuemodal).modal('show');
34                         } else {
35                                 $(this.$refs.vuemodal).modal('hide');
36                         }
37                 }
38         },
39
40         methods: {
41                 resetForm: function() {
42                         this.rule = {id: '', name: '', expression: '', created: ''};
43                         this.showModal = false;
44                         this.editedIndex = null;
45                 },
46
47                 addRule: function () {
48                         if (this.rule.name.trim()) {
49                                 this.errorMessage = '';
50
51                                 var self = this;
52                                 $.ajaxJSON('post', '/advancedcontentfilter/api/rules', this.rule)
53                                 .then(function (responseJSON) {
54                                         self.rules.push(responseJSON.rule);
55                                         self.resetForm();
56                                 }, function (response) {
57                                         self.errorMessage = response.responseJSON.message;
58                                 });
59                         }
60                 },
61
62                 editRule: function (rule) {
63                         this.editedIndex = this.rules.indexOf(rule);
64                         this.rule = Object.assign({}, rule);
65                         this.showModal = true;
66                 },
67
68                 saveRule: function (rule) {
69                         this.errorMessage = '';
70
71                         var self = this;
72                         $.ajaxJSON('put', '/advancedcontentfilter/api/rules/' + rule.id, rule)
73                         .then(function () {
74                                 self.rules[self.editedIndex] = rule;
75                                 self.resetForm();
76                         }, function (response) {
77                                 self.errorMessage = response.responseJSON.message;
78                         });
79                 },
80
81                 toggleActive: function (rule) {
82                         var previousValue = this.rules[this.rules.indexOf(rule)].active;
83                         var newValue = Math.abs(parseInt(rule.active) - 1);
84
85                         this.rules[this.rules.indexOf(rule)].active = newValue;
86
87                         var self = this;
88                         $.ajaxJSON('put', '/advancedcontentfilter/api/rules/' + rule.id, {'active': newValue})
89                         .fail(function (response) {
90                                 self.rules[self.rules.indexOf(rule)].active = previousValue;
91                                 console.log(response.responseJSON.message);
92                         });
93                 },
94
95                 deleteRule: function (rule) {
96                         if (confirm('Are you sure you want to delete this rule?')) {
97                                 var self = this;
98                                 $.ajaxJSON('delete', '/advancedcontentfilter/api/rules/' + rule.id)
99                                 .then(function () {
100                                         self.rules.splice(self.rules.indexOf(rule), 1);
101                                 }, function (response) {
102                                         console.log(response.responseJSON.message);
103                                 });
104                         }
105                 },
106
107                 showVariables: function () {
108                         var urlParts = this.itemUrl.split('/');
109                         var guid = urlParts[urlParts.length - 1];
110
111                         this.itemJson = '';
112
113                         var self = this;
114                         $.ajaxJSON('get', '/advancedcontentfilter/api/variables/' + guid)
115                         .then(function (responseJSON) {
116                                 self.itemJson = responseJSON.variables;
117                         }, function (response) {
118                                 self.itemJson = response.responseJSON.message;
119                         });
120
121                         return false;
122                 }
123         },
124
125         // These render functions have been compiled from templates/advancedcontentfilter.vue, check this file out for instructions
126         render: function () {
127                 with(this){return _c('div',{attrs:{"id":"rules"}},[_c('p',[_c('a',{attrs:{"href":"settings/addon"}},[_v("🔙 "+_s(messages.backtosettings))])]),_c('h1',[_v(_s(messages.title)+" "),_c('a',{staticClass:"btn btn-default btn-sm",attrs:{"href":"advancedcontentfilter/help","title":messages.help}},[_c('i',{staticClass:"fa fa-question fa-2x",attrs:{"aria-hidden":"true"}})])]),_c('div',[_v(_s(messages.intro))]),_c('h2',[_v(_s(messages.your_rules)+" "),_c('button',{staticClass:"btn btn-primary btn-sm",attrs:{"title":messages.add_a_rule},on:{"click":function($event){showModal = true}}},[_c('i',{staticClass:"fa fa-plus fa-2x",attrs:{"aria-hidden":"true"}})])]),(rules.length === 0)?_c('div',{},[_v(_s(messages.no_rules))]):_e(),_c('ul',{staticClass:"list-group"},_l((rules),function(rule){return _c('li',{staticClass:"list-group-item"},[_c('p',{staticClass:"pull-right"},[(parseInt(rule.active))?_c('button',{staticClass:"btn btn-xs btn-primary",attrs:{"type":"button","aria-label":messages.disable_this_rule,"title":messages.disable_this_rule},on:{"click":function($event){toggleActive(rule)}}},[_c('i',{staticClass:"fa fa-toggle-on",attrs:{"aria-hidden":"true"}}),_v(" "+_s(messages.enabled))]):_c('button',{staticClass:"btn btn-xs btn-default",attrs:{"type":"button","aria-label":messages.enable_this_rule,"title":messages.enable_this_rule},on:{"click":function($event){toggleActive(rule)}}},[_c('i',{staticClass:"fa fa-toggle-off",attrs:{"aria-hidden":"true"}}),_v(" "+_s(messages.disabled))]),_v(" "),_c('button',{staticClass:"btn btn-xs btn-primary",attrs:{"type":"button","aria-label":messages.edit_this_rule,"title":messages.edit_this_rule},on:{"click":function($event){editRule(rule)}}},[_c('i',{staticClass:"fa fa-pencil",attrs:{"aria-hidden":"true"}})]),_v(" "),_c('button',{staticClass:"btn btn-xs btn-default",attrs:{"type":"button","aria-label":messages.delete_this_rule,"title":messages.delete_this_rule},on:{"click":function($event){deleteRule(rule)}}},[_c('i',{staticClass:"fa fa-trash-o",attrs:{"aria-hidden":"true"}})])]),_c('h3',{staticClass:"list-group-item-heading"},[_v(_s(messages.rule)+" #"+_s(rule.id)+": "+_s(rule.name))]),(rule.expression)?_c('pre',{staticClass:"list-group-item-text"},[_v(_s(rule.expression))]):_e()])})),_c('div',{ref:"vuemodal",staticClass:"modal fade",attrs:{"tabindex":"-1","role":"dialog"}},[_c('div',{staticClass:"modal-dialog",attrs:{"role":"document"}},[_c('div',{staticClass:"modal-content"},[_c('div',{staticClass:"modal-header"},[(currentTheme === 'frio')?_c('button',{staticClass:"close",attrs:{"type":"button","data-dismiss":"modal","aria-label":messages.close},on:{"click":function($event){showModal = false}}},[_c('span',{attrs:{"aria-hidden":"true"}},[_v("×")])]):_e(),(rule.id)?_c('h3',[_v(_s(messages.edit_the_rule)+" \""+_s(rule.name)+"\"")]):_e(),(!rule.id)?_c('h3',[_v(_s(messages.add_a_rule))]):_e()]),_c('div',{staticClass:"modal-body"},[_c('form',[(errorMessage)?_c('div',{staticClass:"alert alert-danger",attrs:{"role":"alert"}},[_v(_s(errorMessage))]):_e(),_c('div',{staticClass:"form-group"},[_c('input',{directives:[{name:"model",rawName:"v-model",value:(rule.name),expression:"rule.name"}],staticClass:"form-control",attrs:{"placeholder":messages.rule_name},domProps:{"value":(rule.name)},on:{"input":function($event){if($event.target.composing)return;$set(rule, "name", $event.target.value)}}})]),_c('div',{staticClass:"form-group"},[_c('input',{directives:[{name:"model",rawName:"v-model",value:(rule.expression),expression:"rule.expression"}],staticClass:"form-control",attrs:{"placeholder":messages.rule_expression},domProps:{"value":(rule.expression)},on:{"input":function($event){if($event.target.composing)return;$set(rule, "expression", $event.target.value)}}})])])]),_c('div',{staticClass:"modal-footer"},[_c('button',{staticClass:"btn btn-default",attrs:{"type":"button","data-dismiss":"modal","aria-label":"Close"},on:{"click":function($event){resetForm()}}},[_v(_s(messages.cancel))]),(rule.id)?_c('button',{staticClass:"btn btn-primary",attrs:{"slot":"button","type":"button"},on:{"click":function($event){saveRule(rule)}},slot:"button"},[_v(_s(messages.save_this_rule))]):_e(),(!rule.id)?_c('button',{staticClass:"btn btn-primary",attrs:{"slot":"button","type":"button"},on:{"click":function($event){addRule()}},slot:"button"},[_v(_s(messages.add_a_rule))]):_e()])])])]),_c('form',{staticClass:"form-inline",on:{"submit":function($event){$event.preventDefault();showVariables()}}},[_c('fieldset',[_c('legend',[_v("Show post variables")]),_c('div',{staticClass:"form-group",staticStyle:{"width":"50%"}},[_c('label',{staticClass:"sr-only",attrs:{"for":"itemUrl"}},[_v("Post URL or item guid")]),_c('input',{directives:[{name:"model",rawName:"v-model",value:(itemUrl),expression:"itemUrl"}],staticClass:"form-control",staticStyle:{"width":"100%"},attrs:{"id":"itemUrl","placeholder":"Post URL or item guid"},domProps:{"value":(itemUrl)},on:{"input":function($event){if($event.target.composing)return;itemUrl=$event.target.value}}})]),_c('button',{staticClass:"btn btn-primary",attrs:{"type":"submit"}},[_v("Show Variables")])])]),_c('pre',{},[_v(_s(itemJson))])])}
128         },
129
130         staticRenderFns: [
131         ],
132 });