こんな コードがあるとして、
const theLength="Hello world".length;
最近はこうも書ける。
const {theLength:length}="Hello world"
JavaScriptの場合、StringといえどもObjectなので Object Destructuringを使えるわけ。(IE 11 は使えません。)
こんな コードがあるとして、
const theLength="Hello world".length;
最近はこうも書ける。
const {theLength:length}="Hello world"
JavaScriptの場合、StringといえどもObjectなので Object Destructuringを使えるわけ。(IE 11 は使えません。)
>>> def make_counter(x): print('entering make_counter') while True: yield x print('incrementing x') x = x + 1 >>> counter = make_counter(2) >>> counter >>> next(counter) entering make_counter 2 >>> next(counter) incrementing x 3 >>> next(counter) incrementing x 4 >>> next(counter) incrementing x 5 >>> def fib(max): a,b = 0,1 while a < max: yield a a,b = b, a + b >>> for n in fib(1000): print(n,end = ' ') 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> list(fib(1000)) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] >>>
Python Generator の話
React というのは DOMの内容を簡単に操作しようという JavaScriptのライブラリーの一つだが、Shadow DOMという概念を導入し、JQueryのようにコマンドごとの実行でDOMを書き換えることはせず、Shadow DOMにある程度書き込んでおいてから描画する直前にまとめてDOMに書き込む。なので軽快に動作する、というのが売り。
数年前に覚えようとしたのだが、 二点腑に落ちないところがあって、結局、Vue.jsという他のLibraryを愛用(溺愛)するに至った。
その二点というのが
1.JSXという新しい概念の導入。 一見するとHTMLなのだがHTMLではなく、XML書式をJavaScriptで理解できるようにしたプリプロセッサ。この一見HTMLというのがくせもので、class= はJSの予約用語ゆえに使えずclassName=にしなければならないなど、細かいところで違っていて気持ちがわるい。(これに比べてVueのTemplateはまごうことなきHTMLの拡張)
2.コンポーネントで操作できる変数(State)を使うためにはコンポーネントをClass表記してsetState()という関数を使って管理しなければならない。
というもの。
その後 「JSXは単なるJavaScriptの関数表現」という開発チームの解説をYoutubeでみて ストンと納得できるものがあったものの、 二つ目のJavaScriptにClassが必要というのがなんとも納得いかない(個人の意見です)身としては今いち手が出せずにいた。 Class表記をするあまり、 thisの多用を行い、しかもそのContextを明示するためにClass Constructorに this.function=this.function.bind(this) を書きまくるというのがなんとも切ない。
しかしながら、 Reactの勢いは侮りがたいものがある。 特にMicrosoftのOffice開発系の方たちは Office UI GraphicsのComponentに「Reactは正義!」という感じで使いまくっている。 SharePoinitの JavaScript Frame workも Reactびいきがすごい。
で、最近React-client-appというCLIでReactのScaffoldingが簡単にできます、という記事をよく見るので、実際どれくらい簡単なんだいと試してみたら、 生成されるScaffoldingのComponentがClass表記ではなくfunction表記になっている。 あれ、これって、Stateful Componentは Class表記しなければいけないんじゃなかったっけ、と思ったが、 実はReactも進化していた。
Version 16.8から Hooksという機能が追加されて Functional ComponentでもStateの管理ができるようになっていた。 そして今まで、 Functional Component = Stateless Component, Class Componnet=Stateful Componentとなっていたものが、 どちらを使ってもよいようになっている。 Reactのサイトにも将来の開発にはFunctional Componentを使うのがおすすめと書いてある。 なので、React-client-appもDefaultで生成するコードはクラスレスになっていたわけだ。
紹介ページ https://reactjs.org/docs/hooks-intro.html にあるコードをみれば一目瞭然だが、クラス表記のようなConstructorもなければ、this. でメンバー指定をすることもない。 非常に簡単。 ただ、このHookを使ったクラスレスのステートフルコンポーネントの作り方、オフィシャルサイト以外で解説しているサイトはまだ少ない。
この
const [ var1, setVar1]=useState('this is variable one');
で気をつけなければいけないのは Var1が複数メンバーから構成されるオブジェクトだった場合、
const [obj1,setObj1]=useState({member1:"this is member 1", member2:"this is member 2"})
のように Obj1を宣言したときに、
setObj1({member2:"this is member 2 modified"})
とやってもsetState()と違って、オブジェクトの他のメンバー(Key)とマージしてくれない。上の例ではmember1が消えてしまう。
セット関数には元のstateがパラメータとして渡されているのを利用して、
setObj1(s=>({ ...s, member2:"this is member 2 modified"}))
とスプレッド表記を使ってマージしてあげる必要がある。(上のサイトにはObject.createを使うのも可と書いてある)
また、いわゆる ComponentWillMountなどのLife Cycle hookの代わりには描画毎に発火するuseEffect()を使えと書いてある。 このHookは発火を限定するために二つ目のパラメータ―で発火する条件を指定できる。 ファイルデータを読み込むなど、あるいはイベントリスナーを設定するなど、一回だけ発火させたい場合、 このパラメーターを空のアレイで渡してあげることで実現できる。(なんとなくハックっぽいが)
useEffect(()=>{Data を読み込む云々}, [])
他にも何種類かのHooksが用意されているが、 演習用にアプリを書いてみると、上の二つのHookだけで、それなりのものができる。
すでにJSXへのアレルギーはなくなっているので、クラスを使わなくてよいReactという選択は結構魅力的です。
すべてにVue.jsを使いたいのはやまやまなれど、SharePoint関連においてMicroSoft が Reactありきの開発を推しているというのはやはり大きい。
I wanted to transfer my existing script to WebPack bundle. The application is a simple page that uses Vue.js. Note that I only read Introduction part of Vue and started using it. At that time, I did not even know the concept of Vue component files . Thus my application was entirely in one JavaScript file.
I rewrote it using typescript. WebPack bundling was necessary to make it work.
I had to decipher minor (but important) detail on NPM’s Vue module that should be used along with WebPack.
If I were to use a Vue package from node_modules/ and bundle it with WebPack, then the default module that will be used is Runtime-only ES module build (vue.runtime.esm.js.)
Now this verbiage is coming from VueJs.org V2 guide
https://vuejs.org/v2/guide/installation.html#Terms
” If you need to compile templates on the client (e.g. passing a string to the template option, or mounting to an element using its in-DOM HTML as the template), you will need the compiler and thus the full build:”
This applies to ‘getting started example’ code where you use template directly on target html file, such as
<div id="app"> {{message}} </div>
According to Vue documentation, runtime only module is 30% lighter and when I write my code in such a way that it utilize component file (*.vue), then using vue-loader will precompile all the templates into render function (for those within component file) so the default settings wil be not an issue. Issue is when I create a new instance of the view .
Again, according to the guide
// this requires the compiler new Vue({ template: '{{ hi }}' }) // this does not new Vue({ render (h) { return h('div', this.hi) } })
If I use the first method but did not configure webpack to use full version of Vue module, resulting bundle file is missing template compiler and I will be staring at the blank page. In this situation, looking into source with F12 looks something like this
<body><!-- function(a,b,c,d) {.......} --></body>
Then how do I configure Webpack so that it knows to bundle the full version of vue module instead of runtime-only module? It was spelled right in the guide above.
module.exports = { // ... resolve: { //....., alias: {'vue$': 'vue/dist/vue.esm.js' } //....... }
I needed to add ‘alias’ clause to the ‘resolve’ section of module.exports. (for webpack v2 and above)
WebPack will then bundle full version of Vue module(vue.esm.js).
Since I didn’t use .vue files for this project and put everything in a single html file, I did not need to use ‘vue-loader’ either.
So this is a minimal requirement for Vue to work with WebPack.
Vue.js has a very detailed documentation which is good. All I needed was to read it through and understand the meaning of it (lol).
By the way, another way to make it work is not tell WebPack about Vue at all.
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
2. Then tell Typescript that ‘Vue’ is a global object
declare let Vue:any
Yes, use the bundler but not bundle the module. It sounds strange but it works
User がノート欄にやたら長いリンク先をそのまま張り付けて、SharePointのリストの表示が著しく損なわれるという現象が自分の管理するサイトコレクションで何件か発生している。 ユーザー教育を行ってリンクの説明を別途短文入力してください、というのは簡単だが、だまって従ってくれるような従業員ばかりではない。 そこで長いリンクを見つけたら強制的に短い文字列に置き換えてしまう、という仕掛けをJavaScriptで書いてしまえばよいのだと思いつき、 試してみた。 結果、以下のような関数を記述し、これを描画時に呼び出す形で解決した。
function shortenAnchor(text) { if (text===null) { return ""; } let testPattern = /(<a.*?href=.+?>)(.*?)(<\/a?>)/g; function replacer(match, p1, p2, p3) { if((p2.length)>30) { p2 = p2.slice(0, 25) + '...'; } return [p1, p2, p3].join(''); } return text.replace(testPattern, replacer); }
このコードで何をやっているか。 まずtestPatternを正規表現(RegExp)で記述する。 RegExpで a tag elements をグローバルにトラップする。トラップしたエレメントはタグの前の部分、中身のテキスト部分、タグの後ろの部分にグループわけされ、それぞれの文字列は$1,$2,$3として利用可能になる。( a tag element 全体のマッチは$&)。 そして、あとは String.replaceを使ってマジックをおこすわけだが、Implementationを試行錯誤している最中に 二つ目のパラメーターが実は関数でもよいのだ、というのを本日初めて知ったわけで、それを使ってうまく書くことができた。 ここに関数を使うと、以下の引数が この順番で使えるようになる。 まずは TestPatternそのもののマッチ $&, それぞれグループ分けした部分のマッチ $1,$2,$3。 よって match,p1,p2,p3として利用可能にしておく。実はこの後ろにIndexなどのパラメーターも続くのが、今回は必要がないので無視。
この replacer という関数ではこの中身の文字列$2(p2)の長さが30文字以上あった場合、最初の25文字以下を切り捨て点三つ…を最後に追加するというもの。 この関数自体を二つ目の引数として使う。 Testしてみたら一発で完動したので感動(おやじギャグです)
普通はString.replaceというと
"this is a pen".replace("this","that") //=>"that is a pen"
という簡単な使い方は知っていたし、 最初のパラメーターを正規表現にすれば $& $1 etc., などのパラメーターを使ってかなり柔軟な置き換えなどができることも知っていて使ってはいたのだが、まさか関数まで使えるとは思っていなかった。 これは素晴らしい、と思った次第
年末の休暇を利用してtypeScriptの演習を続行中。 現在まで理解できたところをとりあえず列挙。
nodejs が導入されている必要あり。
npm install typescript -g
これで tscコマンドが使えるようになる。
tsc example.ts
そして、これでexample.ts から example.jsがトランスパイルされる。
プロジェクトフォルダー内で
tsc init
とやると、コンパイラー設定用のtsconfig.jsonが作成され規定値が設定される。例えば、出力のJavaScriptのレベルが
<pre>”target”:”es5″</pre>
などと記述されている。またコードをトランスパイルしたときのエラーの出力設定などもできる。今のところ全く変更の必要のないレベルでトライアル中。
TypeScript は 基本的には JavaScriptのsuper setなのだが、 ’let’, ‘const’, ‘arrow function ()=>’, ‘ … spread operator’,’Template strings’ などes2015の書式が既にサポートされている。 なのでexample.tsではこれらの言語機能を使ってプログラムを書き、tscでトランスパイルすると、これらの表記をまだサポートしていない現行のブラウザでも動くようなJavaScriptに書き換えてくれる。上のConfig例ではes5レベルのコードに置き換わる。 つまりBabelと同じような使い方ができる、。
type safeなので 異なったタイプの変数へのアサインは論外としてもその可能性が生じそうなコードにはエラー表示が鬼のようにでる。例外処理とか、Interface定義を記述して対処すると満足して何も言わなくなってくれるので、バグの可能性のあるコードを書く可能性が低くなる。
TypeScriptのイントロがDev.office.comにあるので眺めてみた。
tsとして以下の例が載っている。
class Student { fullName: string; constructor( public firstName, public middleInitial, public lastName){ this.fullName = firstName + " "+ middleInitial + " " + lastName; } } interface Person { firstName: string; lastName : string; } function greeter(person: Person){ return "Hello, "+ person.firstName + " " + person.lastName; } let user =new Student("Jane","G.","Doe"); document.body.innerHTML=greeter(user);
これをJSにコンパイルするとこうなる(WebStormはプラグインが動いてダイナミックに自動生成する)
var Student = (function () { function Student(firstName, middleInitial, lastName) { this.firstName = firstName; this.middleInitial = middleInitial; this.lastName = lastName; this.fullName = firstName + " " + middleInitial + " " + lastName; } return Student; }()); function greeter(person) { return "Hello, " + person.firstName + " " + person.lastName; } var user = new Student("Jane", "G.", "Doe"); document.body.innerHTML = greeter(user);
うむむむむ。JSのほうがパターンに親近感があって読みやすいんですけど。でもそういうことじゃなくてTypeやクラスの定義がしっかりできて、あとあとの管理が楽、ということなんだろうな。
去年の暮れぐらいだったか、MSのお知らせでSharePoint Frameworkという新しい開発環境を使った SharePoint SiteのCustomizationが可能になりました。という連絡があった。 自分がやりたいSPサイトのCustomizationは 基本的にJavaScriptをページに埋め込むJavaScript Injectionという手法だ。 これでクライエントサイドの描画をコントロールするCSRフックとかリストやファイルのアイテムをAjaxで読み書きするREST APIなどを使ってカスタムページを作るという方法で用が足りてしまうので、あまり気にしていなかったのだが、この方法、最近、その進化が目立つModern view pageでは使えない。 これは開発するのにVisual Studioが必須であるSharePoint Add-Inでも同じ様で、 どうも今の時点でModern Look componentのCustomizationをサポートしているのはSharePoint Frameworkのみのようなのだ。
で、Dev.Office.comのDocumentationを眺めてみると、なんとこれ、 Node.JS上で全部動いているのだ。
NPMで yo, gulpと @microsoft/generator-sharepoint を導入し Project folderを作って以下を実行
yo @microsoft/sharepoint
これで必要なファイル群がフォルダーにインストールされる。 hello worldのテンプレートになっているので、これでもうnode.js 上のローカルPCで動くSharePointのページを模したWorkbench上での実行が可能(下記コマンドでDefaultブラウザが勝手に立ち上がる)
gulp trust-dev-center
gulp serve
ここまで、SharePointサイトにアクセスする必要すらない。 Step by Stepはdev.office.comに詳しいので割愛 (Build your first SharePoint client-side web part (Hello World part1))
SharePointのサイトでもすでにDevelopper siteが例えば”https://mysite.com/sites/dev”などとして設定されているなら、 ”https://mysite.com/sites/dev/_layouts/workbench.aspx” にアクセスすると、開発中のwebPart のWorkbenchがSharePoint上で作動する。
そしてこのTool Chainによる開発、Visual Studioが必要ない、というか使えない。dev.office.comのお勧めEditorはVS Code. IDEの例としてJetBrainのWebStormがあげられているなどVisual Studioへの依存性が一切ない。(C#もドットnetも使わないなら当たり前というべきか)
ただ、使われているコードが通常のJavaScriptではなく、TypeScriptなのだ。 TypeScriptのコードは実行時にはJavaScriptにばらされるのでそんなに大きなLearning Curveにはならない、と予測はしているのだが、これは学習する必要があるんだろう。
Node.jsは言語というより開発環境のベースとして使われるので、これ自体を学ぶ必要はない。
VSで使っていたNugetだとかMS buildーtoolなどのツールが、下のようにNode.JS上で動くツールにおきかわっているわけで、今わかっている範囲でまとめると
というわけで、なんと開発環境的には、2,3,4,5,6は1のNodeJSが動けば動くのでなんでもよく、VS codeもWebStormもマルチOS環境で動くので 要するに Linux上で開発できてしまうのだ。
というわけで、javaScriptをかじっている人間にはハードルが低そうな予感。 Linux環境でぼちぼちやってみようかな?
There is a time you need to construct a string (for instance, to create a html block) in your code.
In the simpliest approach, youcan constract the string variable like so
var html='<head><body>’+contentVariable+’more stuff’ + ‘</body></head>’;
this becomes very messy quickly once more variables, more nested construction is needed.
I used to keep concatinating the string, like so
var html='<head></body>’;
html += contentVariable;
html += ‘ more Stuff’;
html += ‘</body></head>’;
Then I saw a pattern in the couple of codes from internet;
var h=[‘<head><body’];
h.push(contentVariable);
h.push(‘more Stuff’);
h.push(‘</head></body>’);
var html=h.join(”);
The More concatenation needed, the more convinient the last method becomes. Key is to join the array element with null delimiter.
Our office is soon moving to SharePoint online. Currently we are using SP2007 and Microsoft does not support content migration from 2007 to 2013 or SP online. This is good and bad. Good because we can build sites from scratch, bad because there is no easy way to move our contents from SP2007 to SP online. We should be able to copy files and folders from 2007 to online but all the metadata will be lost. lists will be even tougher. we have the third party solution called ShareGate but IT guys are telling us it is faily slow we can not possibly migrate the whole sites (that is in the size of the order of Terabytes.) and would like to selectively use the tool. Document Management is tricky under the SharePoint at best.
UI has been greatly improved. It is now much easier to interact with contents with JavaScript. I am having tremendous fun with technique like CSR (client side rendering) and REST API that can be used with CSR or as a conetnt of CEWP as usual. Furthermore , attaching JavaScript code to web part is made less painful by the introduction of JsLink option and inclusion of script web part.
It is also good to have started late in the game. Internet is filled with power user tips. (I can use most of the technique meant for SharePoint 2013 with SharePoint online.)
One of my favorite technique called “calculated column” is still possible with much less hassle. Again CSR can be used for this with literally one line of code. Even better, I can forego with calculated columns at all and directly manipulate the target columns and show bar graph or traffic signal instead, using CSR’s context override.
Filter/connection of WebParts on the page is now performed on the list and not view. In another word, the filtering column do not need to be a part of the view. This is tremendous for me. I no longer need to hide a column that is used for my filter because I don’t need to include the column in the view to start with.
EasyTabs that we used and found so useful for SharePoint 2007 does not work as is. Again somebody has modified it for SharePoint 2013 and it is working. Christophe seems to have developed another tool package that includes EasyTabs functionality but my inquiry to him went unanswered so I am using this version.
There is another code called Hillibily Tabs which essentially do the same thing as EasyTabs. This one uses jQuery and jQuery UI (and CSS) to manipulate the tab, so how do I say this.. It’s flashy and probably more maintainable.
Excellent write up on CSR by Andrei Markeev is here be sure to check other articles by him.
This one by Kaptyn is also good
Many CSR examples are on Office Dev Center, uploaded by Muawiyah Shannak.
The above examples contains calculated column like effect witihout using the calculated column. However, if you insist Calculated columns to be used, here is Christoph’s explanation of how it can be done.
List Roll up
I was first thinking maybe we need to purchase third party package to do this. It turned out this is doable with home made JavaScript code with REST API. I am able to Roll up the list (read only at this time, because I only feel comfortable with “GET” potion of the API) in my sandbox, using a couple of sub-sites. I will try and see how scalable this technique is.
Minimum Download Strategy
Sooner or later, people will hit this “MDS” *issues* where the view changes back to its default look when switching the view, or code breaks because it can not find jQuery that was supposed to have been loaded. I am still learning this but found some blog entries very helpful.
However, my observation is that once you introduce your own code to the mix, then SharePoint engine with MDS seems to try render first with MDS way, then try to render again for “foreign” code. Because of this, some of the pages does load faster with MDS off. So turning this feature is not as bad as it may sound.
Most of the link here are from various blog entries. Again, it must be much easier now than two years ago to find the information I need. MSDN also have many documents I can refer to but there coverage is broader (C#, XML ,.aspx and JavaScript code). As a business side power user, I (can) only use JavaScript and find those individuals’ entries priceless. My appreciation goes to those who spent many nights to figure out and then took a time to write about it!