Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
feat: Add keysitecore package
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Nov 5, 2018
1 parent 506d111 commit 40bda79
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/keysitecore/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Created by .ignore support plugin (hsz.mobi)
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

example.gif
3 changes: 3 additions & 0 deletions packages/keysitecore/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import KeySitecore from './src/key-sitecore';

export { KeySitecore };
13 changes: 13 additions & 0 deletions packages/keysitecore/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@node-sitecore/keysitecore",
"version": "0.0.0-PLACEHOLDER",
"description": "This plugin reset the key attributes in dom element on mounted time, when you use Vue with Sitecore.",
"main": "index.js",
"private": false,
"peerDependencies": {},
"dependencies": {},
"directories": {
"src": "./src",
"bin": "./bin"
}
}
22 changes: 22 additions & 0 deletions packages/keysitecore/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# NodeSitecore/keysitecore

> Original project: https://www.npmjs.com/package/key-sitecore-vue-plugin
In Editor Content of a siteCore project, they use jquery upon key dom attribute. Unfortunately key is also used by Vue and remove from dom element .

This plugin reset the key attributes in dom element on mounted time.

## Installation

Run npm install:
```bash
$ npm install @node-sitecore/keysitecore
```

And import KeySitecore in your Vue project:

```javascript
import * as KeySitecore from "@node-sitecore/keysitecore";

```
67 changes: 67 additions & 0 deletions packages/keysitecore/src/key-sitecore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class KeySitecore {
beforeMount() {
if (this.$el !== undefined) {
this.domObj = {};
const domObj = this.$el.getElementsByTagName('*');
let key = this.$el.getAttribute('key');

/* istanbul ignore else */
if (this.$el.id !== '' && key != null) {
this.domObj[this.$el.id] = key;
}

for (let i = 0; i < domObj.length; i += 1) {
const elem = domObj[i];
key = elem.getAttribute('key');

/* istanbul ignore else */
if (elem.id !== '' && key != null) {
/* istanbul ignore else */
if (this.domObj[elem.id] === undefined) {
this.domObj[elem.id] = key;
} else {
throw new RangeError(`The id ${elem.id} is already set`);
}
}
}
}
}

mounted() {
/* istanbul ignore else */
if (this.domObj) {
let key = this.domObj[this.$el.id];

/* istanbul ignore else */
if (key) {
this.$el.setAttribute('key', key);
}

const domObj = this.$el.getElementsByTagName('*');
const max = domObj.length;

for (let i = 0; i < max; i += 1) {
const elem = domObj[i];
key = this.domObj[elem.id];

/* istanbul ignore else */
if (key) {
elem.setAttribute('key', key);
}
}

this.domObj = {};
}
}

static install(Vue) {
const { beforeMount, mounted } = new KeySitecore();

Vue.mixin({
beforeMount,
mounted
});
}
}

module.exports = KeySitecore;
104 changes: 104 additions & 0 deletions packages/keysitecore/test/key-sitecore.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const KeySitecore = require('../src/key-sitecore');
const { Sinon, expect } = require('../../test/tools');

describe('KeySitecore', () => {
describe('install', () => {
let Vue;
before(() => {
Vue = {
mixin: Sinon.stub()
};
KeySitecore.install(Vue);
});

it('should register the mixin', () => {
Vue.mixin.should.have.been.calledWithExactly(
Sinon.match({
beforeMount: Sinon.match.func,
mounted: Sinon.match.func
})
);
});
});

describe('beforeMount()', () => {
const ctx = {};
let childElem;

before(() => {
childElem = {
id: 'main-container-2',
getAttribute: Sinon.stub().returns('keyValue2')
};

ctx.$el = {
id: 'main-container',
getElementsByTagName: Sinon.stub().returns([childElem]),
getAttribute: Sinon.stub().returns('keyValue')
};

KeySitecore.prototype.beforeMount.call(ctx);
KeySitecore.prototype.beforeMount.call({});
});

it('should $el.getElementsByTagName', () => {
ctx.$el.getElementsByTagName.should.have.been.calledWithExactly('*');
});

it('should $el.getAttribute', () => {
ctx.$el.getAttribute.should.have.been.calledWithExactly('key');
});

it('should childElem.getAttribute', () => {
childElem.getAttribute.should.have.been.calledWithExactly('key');
});

it('should store current element and child element in domObj', () => {
expect(ctx.domObj).to.deep.eq({
'main-container': 'keyValue',
'main-container-2': 'keyValue2'
});
});
});

describe('mounted()', () => {
const ctx = {};
let childElem;

before(() => {
childElem = {
id: 'main-container-2',
setAttribute: Sinon.stub()
};

ctx.$el = {
id: 'main-container',
getElementsByTagName: Sinon.stub().returns([childElem]),
setAttribute: Sinon.stub()
};

ctx.domObj = {
'main-container': 'keyValue',
'main-container-2': 'keyValue2'
};

KeySitecore.prototype.mounted.call(ctx);
});

it('should $el.getElementsByTagName', () => {
ctx.$el.getElementsByTagName.should.have.been.calledWithExactly('*');
});

it('should $el.setAttribute', () => {
ctx.$el.setAttribute.should.have.been.calledWithExactly('key', 'keyValue');
});

it('should childElem.setAttribute', () => {
childElem.setAttribute.should.have.been.calledWithExactly('key', 'keyValue2');
});

it('should reset domObj', () => {
expect(ctx.domObj).to.deep.eq({});
});
});
});

0 comments on commit 40bda79

Please sign in to comment.