Thursday, 12 February 2026

Source Code for Calculator (MVPL Platform) Extension Javascript

 /*

============================================================

InverseExtension

============================================================


Adds multiplicative inverse button (1/x)


Rules:

• Appears ONLY in scientific profile

• Uses extension slot system

• Fully lifecycle compliant

• State stored per profile namespace


Lifecycle:

- onInit

- onProfileSwitch

- onLayoutChange

- onDestroy


State Schema:

{

  lastUsed: null

}

============================================================

*/


(function(){


const manifest = {

  name: "InverseExtension",

  version: "1.0",

  description: "Adds multiplicative inverse button",

  stateSchema: {

    lastUsed: null

  },


  onInit(api, state) {

    state.lastUsed = state.lastUsed || null;

    this.buildUI(api, state);

  },


  onProfileSwitch(api, state) {

    this.refresh(api, state);

  },


  onLayoutChange(api, state) {

    this.refresh(api, state);

  },


  onDestroy(api, state) {

    if (this.button) {

      api.removeUIComponent(this.button);

      this.button = null;

    }

  },


  buildUI(api, state) {

    if (api.getProfile() !== "scientific") return;


    if (this.button) return;


    const btn = document.createElement("button");

    btn.textContent = "1/x";


    btn.onclick = () => {

      if (api.getProfile() !== "scientific") return;


      const val = parseFloat(api.getDisplayValue());


      if (!isFinite(val) || val === 0) {

        api.setDisplayValue("Error");

        return;

      }


      const inv = 1 / val;

      state.lastUsed = val;

      api.setDisplayValue(String(inv));

    };


    api.addButton("slot-extension-1", btn);

    this.button = btn;

  },


  refresh(api, state) {

    if (api.getProfile() === "scientific") {

      this.buildUI(api, state);

    } else {

      if (this.button) {

        api.removeUIComponent(this.button);

        this.button = null;

      }

    }

  }

};


/* Register extension safely */

if (window.registerExtension) {

  window.registerExtension(manifest);

}


})();


No comments:

Post a Comment