Rule.vue 5.7 KB
Newer Older
1
<template>
David Foucher's avatar
David Foucher committed
2
  <div id="Rule">
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    <div class="container mb-5">
      <div class="row">
        <div  class="col-md-6 col-sm-12 col-xs-12">
          <h4>{{ this.name }}</h4>
        </div>
        <div class="col-md-6 col-sm-12 col-xs-12 my-auto">
          <div v-show="!isEditMode" class="form-inline pull-right">
            <div class="form-group mx-sm-3">
              <input @click="edit" type="button" class="btn btn-outline-success form-control" value="Soumettre une modification"/>
              <!-- TODO: show gitlab link of modification if exists -->                            
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-show="!isEditMode">
      <!-- the demo root element -->
      <ul id="demo">
        <TreeItem class="item" :item="this.ruleTree"></TreeItem>
      </ul>
    </div>
    <div v-show="isEditMode">
      <!-- rule-editor ref="editor" :rawRule="ruleToEdit"></rule-editor -->
      <label for="comment" class="mb-2"><u>Résumé de la modification</u></label>
      <textarea v-model="comment"></textarea>
      <label for="content"><u>Contenu de la règle</u></label>
      <textarea v-model="content" class="mb-2"></textarea>
      <div class="form-inline pull-left">
        <div class="form-group mx-sm-3 mb-2">
          <input @click="closeEdit" type="button" class="btn btn-outline-danger form-control" value="Annuler"/>
        </div>
      </div>
      <div class="form-inline pull-right">
        <div class="form-group mx-sm-3 mb-2">
          <input @click="save" type="button" class="btn btn-outline-success form-control" value="Enregistrer"/>
        </div>
      </div>
    </div>
41
  </div>
42

43 44
</template>
<script>
45 46 47 48 49 50 51 52
  import TreeItem from './TreeItem.vue';

  function Node(name) {
    this.name = name;
    this.parent = null;
    this.children = [];
  }

David Foucher's avatar
David Foucher committed
53
  export default {
54
    name: 'Rule',
55 56 57
    components: {
      TreeItem,
    },
58
    props: ['name', 'data'],
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    data: function(){
      return {
        ruleData: this.data,
        content: '',
        comment: '',
        isEditMode: false,
      }
    },
    computed: {
      ruleTree: function() {
        return this.toTree(this.ruleData.split('\n'));
      },
      ruleToEdit: function() {
        return this.ruleData;
      },
    },
75
    methods: {
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      edit: function () {
        this.content = this.ruleToEdit;
        this.isEditMode=!this.isEditMode;
      },
      closeEdit: function () {
        this.content = this.ruleToEdit;
        this.comment = "";
        this.isEditMode=!this.isEditMode;
      },
      save: function() {
        this.ruleData = this.content
        const postData = {
          title: this.name,
          comment: this.comment,
          content: this.content,
          filename: 'trefle/config/rules/' + this.data.path
        }

        this.$http
          .post('/source/save', postData)
          .then(response => {
              var commit = {}
              commit.url = 'https://beta.pole-emploi.fr/open-source/trefle/commit/' + response.id
              commit.title = response.title
              this.rules[this.activeRuleName]['data'] = this.data
              this.rules[this.activeRuleName]['gitlab'] = {'commit': commit}
              return this.isEditMode=!this.isEditMode
          });
      },
      toTree: function (lines) { // eslint-disable-line no-unused-vars
        var root= new Node('Règle');
        var currentIndent=-1;
        var currentNode=root;
        // we scan the rules line by line
        for (var i=0;i<lines.length;i++) {
          var line=lines[i];
          if (line.match(/^\s*$/)) continue; // Empty line, skip
          var indent=line.search(/\S|$/); // number of indenting spaces
          var newNode= new Node(line.trim());
          if (indent>currentIndent) { // New child
            // Set the new node parent
            newNode.parent=currentNode;
            // attach the new node to its parent
            currentNode.children.push(newNode);
            // Set the new current node
            currentNode=newNode;
            currentIndent=indent;
          } else if (indent<currentIndent){
            // Move up in the tree
            var level=currentIndent-indent;
            for (var j=0;j<level/4;j++) {
              // up one level
              currentNode=currentNode.parent;
              currentIndent=currentIndent-4;
            }
            newNode.parent=currentNode.parent;
            currentNode.parent.children.push(newNode); // Add a sibbling
            currentNode=newNode;
          } else {
            // Add as sibbling
            newNode.parent=currentNode.parent;
            currentNode.parent.children.push(newNode);
            currentNode=newNode;
          }
        }
        return root;
142
      }
143
    },
David Foucher's avatar
David Foucher committed
144 145
  }
</script>
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
<style scoped>

  pre strong {
    font-family: 'mono';
    font-weight: bold;
  }
  pre .comment {
    font-family: 'mono';
    font-style: italic;
    color: #666;
  }
  pre .keyword {
    font-family: 'mono';
    font-weight: 600;
  }
  pre .constant {
    font-family: 'mono';
    font-weight: 600;
    text-decoration: underline dotted;
    cursor: help;
  }
  pre a,
  pre .string,
  pre .number {
    font-family: 'mono';
    font-weight: 600;
  }
  pre code {
    display: block;
    padding-left: 1em;
    font-family: mono;
  }
  a.line {
    display: none;
  }
  :scope.with-lines a.line {
    display: inline-block;
    background-color: #ddd;
    cursor: pointer;
    text-align: center;
    vertical-align: middle;
  }
  :scope.with-lines a:target, :scope.with-lines a:target + code {
    font-weight: bold;
    background-color: #ccc;
  }
  :scope.with-lines pre {
    display: grid;
    grid-template-columns: 2em auto;
  }
  .tooltip .tooltip-content:after {
    /* Why? */
    bottom: -27px;
  }
</style>