Plugin Directory

Changeset 1963126


Ignore:
Timestamp:
10/25/2018 07:32:24 PM (7 years ago)
Author:
gtanyware
Message:

Version 2.1.1 Added REST functions and a demo REST server

Location:
easycoder/trunk
Files:
1 added
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • easycoder/trunk/easycoder.js

    r1960099 r1963126  
    26202620        return c + EasyCoder_Tokenise.findStrings({ original, line: tail, inComment, inQuote: false });
    26212621      }
    2622       if (!inComment && !inQuote && !c.match(/[A-z0-9_\-\ ]/)) {
     2622      if (!inComment && !inQuote && !c.match(/[A-z0-9_\-\+\*\/\-\ ]/)) {
    26232623        if (c == "'" || c == '"') {
    26242624          throw new Error(`Bad syntax in "${original}":\nStrings in EasyCoder must be enclosed in backticks.`);
  • easycoder/trunk/easycoder.php

    r1960099 r1963126  
    44    * Plugin URI: https://easycoder.software
    55    * Description: Control the appearance and behavior of your posts and pages by embedding simple English-like scripts, without the need to learn JavaScript.
    6     * Version: 2.1.0
     6    * Version: 2.1.1
    77    * Author: EasyCoder Software
     8    * Author URI: https://easycoder.software
    89    */
    910
     
    2526        $current = getcwd();
    2627        while (true) {
    27             // Get the last item - the name of the current directory
     28            // Get the last item; the name of the current directory
    2829            $slash = strrpos($current, '/');
    2930            // Something went wrong, so abandon this
     
    3839            $current = substr($current, 0, $slash);
    3940        }
    40         // Get the EC template file - uses relative addressing
    41         $text = file_get_contents($backup . '../wp-content/plugins/easycoder/easycoder-temp.js');
     41        // Get the EC template file using relative addressing
     42        $text = file_get_contents($backup . '../wp-content/plugins/easycoder/easycoder-template.js');
    4243        // Replace the "/rest/" template variables
    4344        $text = str_replace('/rest/', $restURI, $text);
     
    6061    // The EasyCoder library
    6162    function easycoder_enqueue_script() {   
    62         wp_enqueue_script('easycoder_script', plugin_dir_url( __FILE__ ) . 'easycoder-min.js', array(), '2.1.0');
     63        wp_enqueue_script('easycoder_script',
     64                plugin_dir_url( __FILE__ ) . 'easycoder-min.js', array(), '2.1.1');
    6365    }
    6466
  • easycoder/trunk/plugin-browser.js

    r1960099 r1963126  
    8888      const command = program[program.pc];
    8989      const cssId = program.value.evaluate(program, command.cssId).content;
    90       const element = document.getElementById(cssId);
    91       if (!element) {
    92         program.error.runtimeError(command.lino, 'No such element: ' + cssId);
     90      try {
     91        const element = document.getElementById(cssId);
     92        if (!element) {
     93          throw Error('No such element: ' + cssId);
     94        }
     95      } catch (err) {
     96        console.log(err.message);
     97        program.error.runtimeError(command.lino, err.message);
    9398      }
    9499      const target = program.getSymbolRecord(command.symbol);
     
    812817          compiler.next();
    813818          const styleName = compiler.getValue();
     819          var type = 'setStyle'
     820          var symbolName = '';
    814821          if (compiler.tokenIs('of')) {
    815822            compiler.next();
    816             if (compiler.isSymbol()) {
    817               const symbolName = compiler.getToken();
     823            if (compiler.getToken() === 'body') {
     824              type = 'setBodyStyle';
     825            } else if (compiler.isSymbol()) {
     826              symbolName = compiler.getToken();
     827            }
     828            compiler.next();
     829            if (compiler.tokenIs('to')) {
    818830              compiler.next();
    819               if (compiler.tokenIs('to')) {
    820                 compiler.next();
    821                 const styleValue = compiler.getValue();
    822                 if (styleValue) {
    823                   compiler.addCommand({
    824                     domain: 'browser',
    825                     keyword: 'set',
    826                     lino,
    827                     type: 'setStyle',
    828                     symbolName,
    829                     styleName,
    830                     styleValue
    831                   });
    832                   return true;
    833                 }
     831              const styleValue = compiler.getValue();
     832              if (styleValue) {
     833                compiler.addCommand({
     834                  domain: 'browser',
     835                  keyword: 'set',
     836                  lino,
     837                  type,
     838                  symbolName,
     839                  styleName,
     840                  styleValue
     841                });
     842                return true;
    834843              }
    835844            }
     
    927936          styles[command.styleName.content] = styleValue.content;
    928937          Object.assign(styleTarget.style, styles);
     938          break;
     939        case 'setBodyStyle':
     940          const bodyStyleValue = program.value.evaluate(program, command.styleValue);
     941          document.body.style[command.styleName.content] = bodyStyleValue.content;
    929942          break;
    930943        case 'setTitle':
  • easycoder/trunk/plugin-json.js

    r1960099 r1963126  
    7474        case 'set':
    7575          compiler.next();
     76          if (compiler.tokenIs('item')) {
     77            compiler.next();
     78            const name = compiler.getValue();
     79            if (compiler.tokenIs('of')) {
     80              compiler.next();
     81              if (compiler.isSymbol()) {
     82                const targetRecord = compiler.getSymbolRecord();
     83                if (targetRecord.keyword === 'variable') {
     84                  compiler.next();
     85                  if (compiler.tokenIs('to')) {
     86                    compiler.next();
     87                    const value = compiler.getValue();
     88                    compiler.addCommand({
     89                        domain: 'json',
     90                        keyword: 'json',
     91                        lino,
     92                        request: 'setItem',
     93                        target: targetRecord.name,
     94                        name,
     95                        value
     96                    });
     97                    return true;
     98                  }
     99                }
     100              }
     101            }
     102            return null;         
     103          }
    76104          if (compiler.isSymbol()) {
    77105            const targetRecord = compiler.getSymbolRecord();
    78             if (targetRecord.keyword === 'select') {
     106            if (targetRecord.keyword === 'variable') {
     107              compiler.next();
     108              if (compiler.tokenIs('to')) {
     109                compiler.next();
     110                const type = compiler.getToken();
     111                if ('["array","object"]'.includes(type)) {
     112                  compiler.next();
     113                  compiler.addCommand({
     114                      domain: 'json',
     115                      keyword: 'json',
     116                      lino,
     117                      request: 'setVariable',
     118                      target: targetRecord.name,
     119                      type
     120                  });
     121                  return true;
     122                }
     123              }
     124            } else if (targetRecord.keyword === 'select') {
    79125              compiler.next();
    80126              if (compiler.tokenIs('from')) {
     
    158204          };
    159205          break;
     206        case 'setVariable':
     207          targetRecord = program.getSymbolRecord(command.target);
     208          const content = (command.type === 'array') ? '[]' : '{}';
     209          targetRecord.value[targetRecord.index] = {
     210            type: 'constant',
     211            numeric: false,
     212            content
     213          };
     214          break;
     215        case 'setItem':
     216          targetRecord = program.getSymbolRecord(command.target);
     217          const targetValue = targetRecord.value[targetRecord.index];
     218          const targetItem = JSON.parse(program.getValue(targetValue));
     219          const itemName = program.getValue(command.name);
     220          const itemValue = program.getValue(command.value);
     221          targetItem[itemName] = itemValue;
     222          targetRecord.value[targetRecord.index] = {
     223            type: 'constant',
     224            numeric: false,
     225            content: JSON.stringify(targetItem)
     226          };
     227          break;
    160228        case 'setList':
    161229          // The source is assumed to be a JSON array
     
    226294            }
    227295          }
    228       }
    229       compiler.addWarning('Unrecognised syntax in \'rest\'');
     296          break;
     297        case 'post':
     298          compiler.next();
     299          var name = null;
     300          var value = null;
     301          if (compiler.tokenIs('to')) {
     302            compiler.next();
     303          } else {
     304            value = compiler.getValue();
     305            if (compiler.tokenIs('to')) {
     306              compiler.next();
     307            } else {
     308              break;
     309            }
     310            name = compiler.getValue();
     311            if (compiler.tokenIs('at')) {
     312              compiler.next();
     313            } else {
     314              break;
     315            }
     316          }
     317          const url = compiler.getValue();
     318          compiler.addCommand({
     319            domain: 'json',
     320            keyword: 'rest',
     321            lino,
     322            request: 'post',
     323            value,
     324            name,
     325            url
     326          });
     327          return true;
     328      }
    230329      return false;
    231330    },
     
    233332    run: (program) => {
    234333      const command = program[program.pc];
     334      program.ajaxCommand = command;
     335      const url = program.getValue(command.url);
     336      const xhttp = new XMLHttpRequest();
     337      xhttp.onreadystatechange = function () {
     338//        console.log(`readyState:${xhttp.readyState}, status:${xhttp.status}`);
     339        if (xhttp.readyState === 4) {
     340            const command = program.ajaxCommand;
     341            switch (this.status) {
     342              case 200:
     343              case 201:
     344                if (command.request === 'get') {
     345                  const targetRecord = program.getSymbolRecord(command.target);
     346                  targetRecord.value[targetRecord.index] = {
     347                    type: 'constant',
     348                    numeric: false,
     349                    content: this.responseText
     350                  };
     351                }
     352                program.run(command.pc + 1);
     353                break;
     354              case 0:
     355                break;
     356              default:
     357                const lino = command.lino + 1;
     358                const error = JSON.parse(this.responseText ? this.responseText : '{}');
     359                alert(`HTTP error ${this.status} at line ${lino}:\n${error.message}`);
     360                break;
     361            }
     362        }
     363      };
    235364      switch (command.request) {
    236365        case 'get':
    237           program.ajaxCommand = command;
    238           const xhttp = new XMLHttpRequest();
    239           xhttp.onreadystatechange = function () {
    240             if (this.readyState === 4 && this.status === 200) {
    241               const command = program.ajaxCommand;
    242               const targetRecord = program.getSymbolRecord(command.target);
    243               targetRecord.value[targetRecord.index] = {
    244                 type: 'constant',
    245                 numeric: false,
    246                 content: this.responseText
    247               };
    248               program.run(command.pc + 1);
    249             }
    250           };
    251           const url = program.getValue(command.url);
    252366          xhttp.open('GET', url, true);
    253367          xhttp.send();
     368          break;
     369        case 'post':
     370          const name = encodeURI(program.getValue(command.name));
     371          const value = encodeURI(program.getValue(command.value));
     372          xhttp.open('POST', url, true);
     373          xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
     374//          console.log(`Send 'name=${name}&value=${value}' to\n${url}`);
     375          xhttp.send(`name=${name}&value=${value}`);
     376          break;
    254377      }
    255378      return 0;
  • easycoder/trunk/readme.txt

    r1960099 r1963126  
    4747== Changelog ==
    4848
     49= 2.1.1 25-oct-2018 =
     50* Added some more REST functions and a REST server.
     51
    4952= 2.1.0 21-oct-2018 =
    5053* Change from curly braces to backticks.
  • easycoder/trunk/rest.php

    r1958621 r1963126  
    11<?php
    2     // REST server
     2  // REST server
    33
    44    // Set the error and exception handlers.
Note: See TracChangeset for help on using the changeset viewer.